How do I access data from Snowflake in Streamlit?

64 viewsStreamlit
0

How do I access data from Snowflake in Streamlit?

Daniel Steinhold Asked question September 14, 2023
0

To access data from Snowflake in Streamlit, you can use the following steps:

  1. Install the following Python libraries:

    • snowflake-connector-python
    • streamlit
  2. Create a file called streamlit_app.py and add the following code:

Python
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)
  1. 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
Daniel Steinhold Changed status to publish September 14, 2023