Connect Streamlit to Snowflake

This guide explains how to securely access a Snowflake database from Streamlit Cloud. It uses the snowflake-connector-python library and Streamlit's secrets management.



If you already have a database that you want to use, feel free to skip to the next step.

First, sign up for Snowflake and log into the Snowflake web interface (note down your username, password, and account identifier!):

Enter the following queries into the SQL editor in the Worksheets page to create a database and a table with some example values:


    NAME            varchar(80),
    PET             varchar(80)

INSERT INTO MYTABLE VALUES ('Mary', 'dog'), ('John', 'cat'), ('Robert', 'bird');


Before you execute the queries, first determine which Snowflake UI / web interface you're using. You can either use the classic web interface or the new web interface.

To execute the queries in the classic web interface, select All Queries and click on Run.

AWS screenshot 1

Make sure to note down the name of your warehouse, database, and schema from the Context dropdown menu on the same page:

AWS screenshot 2

To execute the queries in the new web interface, highlight or select all the queries with your mouse, and click the play button in the top right corner.

AWS screenshot 1


Be sure to highlight or select all the queries (lines 1-10) before clicking the play button.

Once you have executed the queries, you should see a preview of the table in the Results panel at the bottom of the page. Addionally, you should see your newly created database and schema by expanding the accordion on the left side of the page. Lastly, the warehouse name is displayed on the button to the left of the Share button.

AWS screenshot 2

Make sure to note down the name of your warehouse, database, and schema. ☝️

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 your Snowflake username, password, account identifier, and the name of your warehouse, database, and schema as shown below:

# .streamlit/secrets.toml

user = "xxx"
password = "xxx"
account = "xxx"
warehouse = "xxx"
database = "xxx"
schema = "xxx"

If you created the database from the previous step, the names of your database and schema are PETS and PUBLIC, respectively.



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 snowflake-connector-python package 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. Make sure to adapt query to use the name of your table.


import streamlit as st
import snowflake.connector

# Initialize connection.
# Uses st.experimental_singleton to only run once.
def init_connection():
    return snowflake.connector.connect(**st.secrets["snowflake"])

conn = init_connection()

# Perform query.
# Uses st.experimental_memo to only rerun when the query changes or after 10 min.
def run_query(query):
    with conn.cursor() as cur:
        return cur.fetchall()

rows = run_query("SELECT * from mytable;")

# Print results.
for row in rows:
    st.write(f"{row[0]} has a :{row[1]}:")

See st.experimental_memo above? Without it, Streamlit would run the query every time the app reruns (e.g. on a widget interaction). With st.experimental_memo, it only runs when the query changes or after 10 minutes (that's what ttl is for). Watch out: If your database updates more frequently, you should adapt ttl or remove caching so viewers always see the latest data. Read more about caching here.

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

Finished app screenshot

Was this page helpful?

editSuggest edits

Still have questions?

Our forums are full of helpful information and Streamlit experts.