To connect Streamlit to Snowflake, you will need to:
- Create a Snowflake account and database.
- Install the
snowflake-snowpark-python
library. - Add your Snowflake connection parameters to your local app secrets.
- Write your Streamlit app.
Here are the steps in more detail:
- Create a Snowflake account and database. You can do this at https://signup.snowflake.com: https://signup.snowflake.com.
- Install the
snowflake-snowpark-python
library. You can do this with the following command:
pip install snowflake-snowpark-python
-
Add your Snowflake connection parameters to your local app secrets. This is a file that contains sensitive information, such as your Snowflake username, password, and database name. You can create this file using the following steps:
- Create a new file called
secrets.toml
in the same directory as your Streamlit app. - Add the following lines to the file, replacing the values with your own:
- Create a new file called
[snowflake]
username = your_snowflake_username
password = your_snowflake_password
database = your_snowflake_database
- Write your Streamlit app. In your Streamlit app, you can connect to Snowflake using the following code:
import snowflake.snowpark as sf
# Connect to Snowflake
conn = sf.connect(
user=secrets["snowflake"]["username"],
password=secrets["snowflake"]["password"],
account=secrets["snowflake"]["database"],
)
# Run a query
df = conn.sql("SELECT * FROM my_table")
# Display the results
st.dataframe(df)
For more detailed instructions, you can refer to the Streamlit documentation: https://docs.streamlit.io/knowledge-base/tutorials/databases/snowflake.