You are currently viewing the new Beta Editor Docs.
Switch to the Classic Editor Docs
You are currently viewing the Classic Editor Docs.
Switch to the Beta Editor Docs

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

Anvil provides a robust Python-based database system built on top of PostgreSQL.

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

Anvil provides a robust Python-based database system built on top of PostgreSQL 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 ‘New Blank App’. Choose the Material Design theme.

Location of the Create App button

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

Navigate to “Data” in the Sidebar Menu and click “+ Add Table”.

Add Table button highlighted in the Data Service

Change the name to be square_numbers.

Changing the Python name of the table to be `square_numbers`

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

A Data Table with the New 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

A Data Table with two Number columns named base_number and squared

Make the table client-writable

In the 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

A Data Table with Add A Column dropdown open

Client-side code permission is set to ‘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

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

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

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.

The Run button in the editor

Stop the app and look back at the Data 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

The Data 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(f"{row['base_number']} squared is {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

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

The Data Tables data printed in the App Console

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 databases.

Read more about Data Tables.

Want another quickstart?

Every quickstart is on the Quickstarts page.