
Connecting Streamlit To Snowflake
Connecting Streamlit apps to Snowflake.
Install
npx skills add iusztinpaul/designing-real-world-ai-agents-workshop --skill connecting-streamlit-to-snowflake -g -a claude-code -y- Repository
- iusztinpaul/designing-real-world-ai-agents-workshop521
- Category
- Data and ML
- Updated
- 5mo ago
- License
- Apache-2.0
- Context cost
- Lightabout 1,204 tokens
- Runs scripts
- No, instructions only
What it does
Connecting Streamlit apps to Snowflake. Use when setting up database connections, managing secrets, or querying Snowflake from a Streamlit app.
Streamlit Snowflake connection
Connect your Streamlit app to Snowflake the right way.
Use st.connection
Always use st.connection("snowflake") instead of raw connectors.
import streamlit as st
conn = st.connection("snowflake")
# Query data
df = conn.query("SELECT * FROM my_table LIMIT 100")
st.dataframe(df)
Why st.connection:
- Automatic connection pooling
- Built-in caching
- Handles reconnection
- Works with st.secrets
Caller's rights connection (Streamlit 1.53+)
For apps running in Snowflake, use caller's rights to run queries with the viewer's permissions instead of the app owner's:
conn = st.connection("snowflake", type="snowflake-callers-rights")
This is useful when:
- Different users should see different data based on their Snowflake roles
- You want row-level security to apply based on the viewer
- You don't want the app to have elevated permissions
Cached queries
Use the built-in ttl parameter to cache query results:
from datetime import timedelta
conn = st.connection("snowflake")
# Cache for 10 minutes
df = conn.query("SELECT * FROM metrics", ttl=timedelta(minutes=10))
# Cache for 1 hour
df = conn.query("SELECT * FROM reference_data", ttl=3600)
Configure with st.secrets
Store credentials in .streamlit/secrets.toml (never commit this file).
CRITICAL: Derive the account and host values from the user's Snowflake CLI connection config. Run snow connection list and use the exact values. A wrong account will redirect to the wrong login page.
# .streamlit/secrets.toml
[connections.snowflake]
account = "ORGNAME-ACCTNAME" # from `snow connection list`
host = "myaccount.snowflakecomputing.com" # from `snow connection list` (include if present)
user = "your_user"
authenticator = "externalbrowser"
warehouse = "your_warehouse"
database = "your_database"
schema = "your_schema"
Add to .gitignore:
.streamlit/secrets.toml
