Connect Streamlit to Snowflake

This guide explains how to securely access a Snowflake database from Streamlit. It uses st.experimental_connection, the Snowpark Python library and Streamlit's Secrets management. The below example code will only work on Streamlit version >= 1.22, when st.experimental_connection was added.

Skip to the bottom for information about connecting using Snowflake Connector for Python.

push_pin

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. The examples below use Snowsight. You can also use Classic Console Worksheets or any other means of running Snowflake SQL statements.

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

AWS screenshot 1
priority_high

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. Additionally, 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. ☝️

You can find the instructions and prerequisites for installing snowflake-snowpark-python in the Snowpark Developer Guide.

pip install "snowflake-snowpark-python[pandas]"

Particular prerequisites to highlight:

  • Currently, only python 3.8 is supported.
  • Ensure you have the correct pyarrow version installed for your version of snowflake-snowpark-python. When in doubt, try uninstalling pyarrow before installing snowflake-snowpark-python.

Your local Streamlit app will read secrets from a file .streamlit/secrets.toml in your app’s root directory. Learn more about Streamlit secrets management here. 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

[connections.snowpark]
account = "xxx"
user = "xxx"
password = "xxx"
role = "xxx"
warehouse = "xxx"
database = "xxx"
schema = "xxx"
client_session_keep_alive = true

If you created the database from the previous step, the names of your database and schema are PETS and PUBLIC, respectively. Streamlit will also use Snowflake config and credentials from a SnowSQL config file if available.

priority_high

Important

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

Copy the code below to your Streamlit app and run it. Make sure to adapt the query to use the name of your table.

# streamlit_app.py

import streamlit as st

# Initialize connection.
conn = st.experimental_connection('snowpark')

# Perform query.
df = conn.query('SELECT * from mytable;', ttl=600)

# Print results.
for row in df.itertuples():
    st.write(f"{row.NAME} has a :{row.PET}:")

See st.experimental_connection above? This handles secrets retrieval, setup, query caching and retries. By default, query() results are cached without expiring. In this case, we set ttl=600 to ensure the query result is cached for no longer than 10 minutes. You can also set ttl=0 to disable caching. Learn more in Caching.

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

Finished app screenshot

The same SnowparkConnection used above also provides access to the Snowpark Session for DataFrame-style operations that run natively inside Snowflake. Using this approach, you can rewrite the app above as follows:

# streamlit_app.py

import streamlit as st

# Initialize connection.
conn = st.experimental_connection('snowpark')

# Load the table as a dataframe using the Snowpark Session.
@st.cache_data
def load_table():
    with conn.safe_session() as session:
        return session.table('mytable').to_pandas()

df = load_table()

# Print results.
for row in df.itertuples():
    st.write(f"{row.NAME} has a :{row.PET}:")

This example uses with conn.safe_session() to provide thread safety. conn.session also works directly, but does not guarantee thread safety. If everything worked out (and you used the example table we created above), your app should look the same as the screenshot from the first example above.

In some cases, you may prefer to use the Snowflake Connector for Python instead of Snowpark Python. Streamlit supports this natively through the SQLConnection and the snowflake-sqlalchemy library.

pip install snowflake-sqlalchemy

Installing snowflake-sqlalchemy will also install all necessary dependencies.

Configuring credentials follows the SQLConnection format which is slightly different. See the Snowflake SQLAlchemy Configuration Parameters documentation for more details.

# .streamlit/secrets.toml

[connections.snowflake]
url = "snowflake://<user_login_name>:<password>@<account_identifier>/<database_name>/<schema_name>?warehouse=<warehouse_name>&role=<role_name>"

Alternatively, specify connection parameters like authenticator or key pair authentication using create_engine_kwargs, as shown below.

# .streamlit/secrets.toml

[connections.snowflake]
url = "snowflake://<user_login_name>@<account_identifier>/"

[connections.snowflake.create_engine_kwargs.connect_args]
authenticator = "externalbrowser"
warehouse = "xxx"
role = "xxx"
client_session_keep_alive = true

Initializing and using the connection in your app is similar. Note that SQLConnection.query() supports extra arguments like params and chunksize which may be useful for more advanced apps.

# streamlit_app.py

import streamlit as st

# Initialize connection.
conn = st.experimental_connection('snowflake', type='sql')

# Perform query.
df = conn.query('SELECT * from mytable;', ttl=600)

# Print results.
for row in df.itertuples():
    st.write(f"{row.name} has a :{row.pet}:")

If everything worked out (and you used the example table we created above), your app should look the same as the screenshot from the first example above.

This tutorial assumes a local Streamlit app, however you can also connect to Snowflake from apps hosted in Community Cloud. The main additional steps are:

forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.

Was this page helpful?

editEdit this page on GitHub