The Data Tables service stores tables of information for your Anvil app. It’s a full database system built on top of PostgreSQL.
If you’re not familiar with databases, it’s also similar to a spreadsheet - data is stored in tables with rows and columns.
To add Data Tables to your app, first add the Data Tables service in the App Browser.
From the Data Tables page, you can create a new, empty table, or you can add tables from any of your other apps.
Each table has its own columns. For example, if we are making a table of people for our organisation, we might have columns for name, age and employment status. Each row in the table represents an item - in this case, a person.
Here’s an example table as it looks in the app editor:
Each column has a type of value that it can contain. For example, the Name column is always a string, Age is always
a number, and Employed is a boolean value (either True or False). Any column can also be empty (
None in Python).
Click to edit any data in the table (just like a spreadsheet). To add a new row, click in the blank row at the bottom of the table, and start typing! You can add more columns with the button.
To change a table’s title, click on it (in the tabs at the top). Each table also has a Python name you can use to refer to it in your code. Editing the table’s title does not affect the Python name, and vice versa. (If multiple apps share a table, they may all have different Python names for it, but you will see the same title for the table in each app.)
If you delete your app, its tables will be deleted as well (if no other apps are using it).
All columns of a data table have fixed types. You choose the type of a column when it is created or, if it does not already exist, when a value is first set using
add_row. The available types are listed below, along with their corresponding python types.
- Text - A Python
- Number - Any Python number
- True/False - A Python boolean,
- Date - A Python
- Date and Time - A Python
- Simple Object - Can hold Python strings, numbers, dicts or lists
- Media - Binary data (a Media object)
- Link - A row (or list of rows) from another table
There are Python methods to add rows to your table, search your table, update rows and delete them. These are available in client code as well as in Server Modules (you can configure what’s aceessible using table permissions).
In Python, your table is represented by
Row Objects. These behave like Python dictionaries, allowing you
to get values out of Rows and set their values as if they are in a Python dictionary. For example:
zaphod_row = app_tables.people.get(name="Zaphod Beeblebrox") print("Zaphod is currently %s years old" % zaphod_row['age']) # It's Zaphod's birthday, update the database zaphod_row['age'] += 1
Multiple Rows are represented by
SearchIterators. You can get the entire contents of a table like this:
entire_table = app_tables.people.search()
This returns an object you can iterate over to get each Row in the table:
for row in app_tables.people.search(): print(row["age"])
The advantage of this over simply listing the Rows is that the SearchIterator is lazy - it only loads data as the data is needed, making your app more efficient (you can cast SearchIterators to lists and Rows to dicts if you want to).
For a full explanation of how to use Data Tables in Python, see Using Data Tables from Python.
Data Tables can be shared between apps. When you click the ‘+’ button in the Data Tables Service, you will be given a list of tables from your apps to add to the current app.
Modifications to the table data and schema made in one app will be available from the other apps that use it.
Table permissions are on a per-app basis: if you make a table client-writable in one app, other apps that use it will not be affected. The ‘Python name’ of a table is also per-app.
A table that is included in several apps will only be deleted when it is removed from all apps that use it.