Connect Streamlit to Deta Base

This guide explains how to securely access and write to a Deta Base database from Streamlit Cloud. Deta Base is a fully-managed, fast, scalable and secure NoSQL database with a focus on end-user simplicity. This guide uses the deta Python SDK for Deta Base and Streamlit's secrets management.

First, you need to sign up for Deta Base. Once you have an account, sign in to Deta. When you sign in, Deta will create a default project and show you the project's Project Key and Project ID. Note down your Project Key and Project ID.

Deta sign up

Sign up for Deta

Deta sign in

Sign in to Deta

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

Click to see your Project Key

Click to see your Project Key

Securely store your Project Key

Securely store your Project Key

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 Project Key (from the previous step) of your Deta Base as shown below:

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

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



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

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 Project Key
deta = Deta(st.secrets["deta_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

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

Finished app GIF

Was this page helpful?

editSuggest edits

Still have questions?

Our forums are full of helpful information and Streamlit experts.