Connect Streamlit to Snowflake
Introduction
This guide explains how to securely access a Snowflake database from Streamlit Community Cloud. It uses the snowflake-connector-python library and Streamlit's secrets management.
Create a Snowflake database
Note
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:
CREATE DATABASE PETS;
CREATE TABLE MYTABLE (
NAME varchar(80),
PET varchar(80)
);
INSERT INTO MYTABLE VALUES ('Mary', 'dog'), ('John', 'cat'), ('Robert', 'bird');
SELECT * FROM MYTABLE;
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.
Using the Classic Web Interface
To execute the queries in the classic web interface, select All Queries and click on Run.

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

Using the New Web Interface
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.

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

Make sure to note down the name of your warehouse, database, and schema. ☝️
Add username and password to your local app secrets
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
[snowflake]
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.
Important
Add this file to .gitignore
and don't commit it to your GitHub repo!
Copy your app secrets to the cloud
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.

Install the Snowflake Connector for Python on Streamlit Community Cloud
The Snowflake Connector for Python is available on PyPI and the installation instructions are found in the Snowflake documentation. When installing the connector, Snowflake recommends installing specific versions of its dependent libraries. The steps below will help you install the connector and its dependencies on Streamlit Community Cloud:
Determine the version of the Snowflake Connector for Python you want to install.
Determine the version of Python you want to use on Streamlit Community Cloud.
To install the connector and the dependent libraries, select the requirements file for that version of the connector and Python.
Add the raw GitHub URL of the requirements file to your
requirements.txt
file and prepend-r
to the line. For example, if you want to install version2.7.9
of the connector on Python 3.9, add the following line to yourrequirements.txt
file:-r https://raw.githubusercontent.com/snowflakedb/snowflake-connector-python/v2.7.9/tested_requirements/requirements_39.reqs
On Streamlit Community Cloud, select the appropriate version of Python for your app by clicking "Advanced settings" before you deploy the app:

That's it! You're ready to use the Snowflake Connector for Python on Streamlit Community Cloud. ❄️🎈
Tip
As the Snowflake dependencies requirements files (.reqs
) contain the pinned version of the connector, there is no need add a separate entry for the connector to requirements.txt.
Write your Streamlit app
Copy the code below to your Streamlit app and run it. Make sure to adapt query to use the name of your table.
# streamlit_app.py
import streamlit as st
import snowflake.connector
# Initialize connection.
# Uses st.cache_resource to only run once.
@st.cache_resource
def init_connection():
return snowflake.connector.connect(
**st.secrets["snowflake"], client_session_keep_alive=True
)
conn = init_connection()
# Perform query.
# Uses st.cache_data to only rerun when the query changes or after 10 min.
@st.cache_data(ttl=600)
def run_query(query):
with conn.cursor() as cur:
cur.execute(query)
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.cache_data
above? Without it, Streamlit would run the query every time the app reruns (e.g. on a widget interaction). With st.cache_data
, 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. Learn more in Caching.
If everything worked out (and you used the example table we created above), your app should look like this:
