How do I connect Streamlit to Snowflake?

363 viewsStreamlit
0

How do I connect Streamlit to Snowflake?

Daniel Steinhold Asked question September 13, 2023
0

To connect Streamlit to Snowflake, you will need to:

  1. Create a Snowflake account and database.
  2. Install the snowflake-snowpark-python library.
  3. Add your Snowflake connection parameters to your local app secrets.
  4. Write your Streamlit app.

Here are the steps in more detail:

  1. Create a Snowflake account and database. You can do this at https://signup.snowflake.com: https://signup.snowflake.com.
  2. Install the snowflake-snowpark-python library. You can do this with the following command:
pip install snowflake-snowpark-python
  1. 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:

    1. Create a new file called secrets.toml in the same directory as your Streamlit app.
    2. Add the following lines to the file, replacing the values with your own:
[snowflake]
username = your_snowflake_username
password = your_snowflake_password
database = your_snowflake_database
  1. 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.

Daniel Steinhold Changed status to publish September 13, 2023