connect to a snowflake cluster using secret key authenticator, printing our a data frame of the first 100 rows
To upload files, please first save the app
import streamlit as st
import snowflake.connector
import pandas as pd
# Snowflake connection configuration using streamlit secrets
st.title("Snowflake Data Viewer")
# Add these to your secrets.toml:
# [snowflake]
# user = "your-username"
# account = "your-account"
# warehouse = "your-warehouse"
# database = "your-database"
# schema = "your-schema"
# private_key_path = "path/to/your/private/key.p8"
# private_key_passphrase = "your-passphrase"
try:
# Initialize Snowflake connection
conn = snowflake.connector.connect(
user=st.secrets.snowflake.user,
account=st.secrets.snowflake.account,
warehouse=st.secrets.snowflake.warehouse,
database=st.secrets.snowflake.database,
schema=st.secrets.snowflake.schema,
private_key=st.secrets.snowflake.private_key_path,
private_key_passphrase=st.secrets.snowflake.private_key_passphrase,
authenticator='snowflake'
)
# Create a cursor object
cur = conn.cursor()
# Example query - replace with your table name
query = "SELECT * FROM YOUR_TABLE LIMIT 100"
# Execute query and fetch results into a pandas DataFrame
cur.execute(query)
df = cur.fetch_pandas_all()
# Display the DataFrame
st.write("First 100 rows of data:")
st.dataframe(df)
except Exception as e:
st.error(f"Error connecting to Snowflake: {str(e)}")
st.info("Please ensure you have set up your secrets.toml file with the correct Snowflake credentials.")
finally:
# Close the cursor and connection
if 'cur' in locals():
cur.close()
if 'conn' in locals():
conn.close()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?