Connect Streamlit to Deta Base

This guide explains how to securely access and write to a Deta Base database from Streamlit Community Cloud. Deta Base is a fully-managed and fast NoSQL database with a focus on end-user simplicity. The data is stored in your own "personal cloud" on Deta Space. This guide uses the Deta Python SDK for Deta Base and Streamlit's Secrets management.

First, you need to create a Deta Space account for using Deta Base. Make sure the "Developer Mode" option is enabled when signing up. Once you have an account, sign in to Deta Space. After signing in, open the Collections app by clicking on it.

Deta Collections is a pre-installed app on Space that stores different types of data that can be connected to other apps or services.

Deta Space Canvas

Deta Space Canvas

Now click on the Get Started button and then click on the Create Collection button after giving your Collection a name.

Create a New Collection

Create a New Collection

After that, click on the Collection Settings option in the top corner, which will show the modal for creating a Data Key. Click on the Create New Data Key button, then give your key a name, and click the Generate button. Copy the key shown to your clipboard by clicking on the copy button.

Data Keys allow you to read and manipulate data within your Collections.

Generate Data Key

Generate Data Key

Be sure to store your Data Key securely. It is shown only once, and you will need it to connect to your Deta Base.

Your local Streamlit app will read secrets from a file .streamlit/secrets.toml in your app's root directory. Create this file if it doesn't exist yet and add the Data Key (from the previous step) of your Deta Base as shown below:

# .streamlit/secrets.toml data_key = "xxx"

Replace xxx above ☝️ with your Data Key from the previous step.

priority_high

Important

Add this file to .gitignore and don't commit it to your GitHub repo!

As the secrets.toml file above is not committed to GitHub, you need to pass its content to your deployed app (on Streamlit Community Cloud) separately. Go to the app dashboard and in the app's dropdown menu, click on Edit Secrets. Copy the content of secrets.toml into the text area. More information is available at Secrets management.

Secrets manager screenshot

Add the deta Python SDK for Deta Base to your requirements.txt file, preferably pinning its version (replace x.x.x with the version you want installed):

# requirements.txt deta==x.x.x

Copy the code below to your Streamlit app and run it. The example app below writes data from a Streamlit form to a Deta Base database example-db.

import streamlit as st from deta import Deta # Data to be written to Deta Base with st.form("form"): name = st.text_input("Your name") age = st.number_input("Your age") submitted = st.form_submit_button("Store in database") # Connect to Deta Base with your Data Key deta = Deta(st.secrets["data_key"]) # Create a new database "example-db" # If you need a new database, just use another name. db = deta.Base("example-db") # If the user clicked the submit button, # write the data from the form to the database. # You can store any data you want here. Just modify that dictionary below (the entries between the {}). if submitted: db.put({"name": name, "age": age}) "---" "Here's everything stored in the database:" # This reads all items from the database and displays them to your app. # db_content is a list of dictionaries. You can do everything you want with it. db_content = db.fetch().items st.write(db_content)

If everything worked out (and you used the example we created above), your app should look like this:

Finished app GIF
forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.