Live Chat

We'll need to share your messages (and your email address if you're logged in) with our live chat provider, Drift. Here's their privacy policy.

If you don't want to do this, you can email us instead at contact@anvil.works.

Quickstart: Data Tables

Learn how to store data in Anvil’s hosted database system

Anvil provides a robust Python-based database system called Data Tables.

Follow this quickstart to create a database, get your app to store data in it, and read the data back.

Create an app

Log in to Anvil and click ‘create app’. Choose the Material Design theme.

Location of the Create App button

Add the Data Tables Service

In the App Browser, click the + next to Services.

App Browser showing the plus next to Services

You’ll see a list of available services and integrations. Click on Data Tables.

Services list with Data Tables highlighted

Create a table

Click ‘Add A Table’ and name it square_numbers.

Data Tables Service with Add A Table highlighted

Add columns

Click New Column and select ‘Add Number column’. Name the new column base_number.

Data Tables Service with Add A Column dropdown open

Then add another Number column named squared.

Data Tables Service with a table named square_numbers and two Number columns named base_number and squared

Make the table client-writable

In the blue bar above the columns, there are some controls relating to this table.

Use the dropdown menu next to ‘Forms’ to select ‘Can search, edit and delete’. This gives client-side code permission to read from and write to this table.

Data Tables Service with Add A Column dropdown open

(This is ‘No access’ by default because Anvil applies security best-practice by default. Click the ‘more info’ link in that dropdown menu to read more about this.)

Write to the database from Python code

Under Forms in the App Browser, select Form1.

App Browser showing where to find Form1

Click on the ‘Code’ tab to see the Python code for Form1.

The Design View for Form1 with the Code tab in the top-right highlighted

You will see a few lines of pre-written code. Your Form is represented as a class called Form1. It currently has only one method, the __init__ method.

The Code View for Form1

At the top of the file, import the random module:

import random

And at the end of the __init__ method, write these lines:

    base_number = random.randint(1, 100)
    app_tables.square_numbers.add_row(base_number=base_number, squared=base_number**2)

Run your app

Now click the ‘Run’ button at the top of the screen.

Running your app in the editor

Stop the app and look back at the Data Tables service. Your table now contains some data.

Run your app a few times to get some more data in the database.

The Data Tables Service UI showing data in the table

Read from the database

Go back to the Code View for Form1 and add these lines to the end of the __init__ method:

    for row in app_tables.square_numbers.search():
      print("%s squared is %s" % (row['base_number'], row['squared']))

Run your app again and you’ll get the contents of your table printed to the Output Panel.

The Data Tables data printed in the Output Panel

Copy the example app

Click on the button below to clone a finished version of this app into your account.

Next up

Want more depth on this subject?

Read more about Data Tables.

Want another quickstart?

Every quickstart is on the Quickstarts page.