Chapter 3:
Write server-side Python

Anvil’s Server Modules are a full server-side Python environment. Server Modules cannot be edited or seen by the user, so we can trust them to do what we tell them. This is why we’ll use a Server Module to write to the Data Table.

Step 1: Create a Server Module

Create a Server Module by selecting the App Browser app icon in the Sidebar Menu and clicking ‘+ Add Server Module’.

Adding a Server Module

Adding a Server Module

Step 2: Store data using a server function

We’ll write a server function to add a new row to the ‘feedback’ Data Table we just created. We’ll use the add_row method to do this.

Add this function to your Server Module:

@anvil.server.callable
def add_feedback(name, email, feedback):
  app_tables.feedback.add_row(
    name=name, 
    email=email, 
    feedback=feedback, 
    created=datetime.now()
  )

The @anvil.server.callable decorator allows us to call this function from client code.

We’re using the datetime library to set the ‘created’ column to the current date and time, so you’ll also need to import the datetime class at the top of your Server Module:

from datetime import datetime

In Chapter 4, we’ll link the server up with the UI.

Chapter complete

Great! We now have a server function that will add a new row to our Data Table when it’s called.

Let’s go ahead and link it up with our UI. On to Chapter 4!