To access data from Snowflake in Streamlit, you can use the following steps:
Install the following Python libraries:
Create a file called
streamlit_app.pyand add the following code:
import snowflake.connector as sf import streamlit as st # Get the user credentials from the secrets file with open(".streamlit/secrets.toml") as f: secret_data = f.read() credentials = toml.load(secret_data) # Connect to Snowflake conn = sf.connect( user=credentials["user"], password=credentials["password"], account=credentials["account"], db=credentials["database"], ) # Run a query df = conn.execute("SELECT * FROM my_table") # Display the results in a Streamlit table st.table(df)
- Save the file and run it in your terminal.
This code will first get the user credentials from a secrets file. Then, it will connect to Snowflake using those credentials. Finally, it will run a query and display the results in a Streamlit table.
Here are some additional resources that you may find helpful:
- Streamlit documentation on connecting to databases: https://docs.streamlit.io/knowledge-base/tutorials/databases/snowflake
- Snowflake documentation on connecting to Python: https://docs.snowflake.net/manuals/user-guide/python-connector.html