Streamlit & Snowflake

Johan
2 min readSep 23, 2023

Building a Streamlit app that displays data from a Snowflake table on screen. My starting point with an example: this Medium post.

The post dives extensively into the Snowflake setup, feel free to read the original post to set this up. Same goes for the setup of the Python venv, the writer does this with Conda, but I do this using the Python venv.

This is the code example, I just did a copy paste from the original post but then decided to change some things, first I swapped the TOML config for a JSON config file.

import streamlit as st
from snowflake.snowpark import Session
import json

st.title('❄️ How to connect Streamlit to a Snowflake database')

# connect to Snowflake
def create_session():
with open('../config/sf-config.json') as f:
connection_parameters = json.load(f)
session = Session.builder.configs(connection_parameters).create()
return session

session = create_session()
st.success("Connected to Snowflake!")

This is the sf-confg.json layout.

{
"account":"",
"user":"",
"password":"",
"warehouse": "",
"database": "",
"schema": ""
}

Start this script using Streamlit Run.

streamlit run sf_connect.py

From this point onwards, you can just put any Streamlit library component in the code to work with the Snowflake tables.

In my case, I added some code to display the table contents in the Streamlit app.

# Load data table
def load_data(table_name):
## Read in data table
st.write(f"Here's some example data from `{table_name}`:")
table = session.table(table_name)

## Do some computation on it
table = table.limit(100)

## Collect the results. This will run the query and download the data
table = table.collect()
return table

# Select and display data table
table_name = "DB_STREAMLIT.PUBLIC.EMPLOYEE"

## Display data table
df = load_data(table_name)

## Writing out data
for row in df:
st.write(f"{row[0]}, {row[1]}")

As you can see, 2 records are added.

That’s it for now, more to follow on Streamlit and Snowflake, stay tuned.

--

--

Johan

I am a freelance data engineer. My main focus is Snowflake, but I am always eager to learn new things. Python and AWS are my main side interests