In this article, let’s explore what Streamlit is, how this works with Snowflake, and why you and your colleagues need to use this for data storytelling.
Features change from time to time with new features being added regularly, it is recommended that you review the documentation for the latest on what specific features are included with any of the Editions.
Table of Contents
Open Table of Contents
What is Streamlit?
Streamlit is designed to simplify the creation of web applications for data science and machine learning projects. With a few lines of Python code, developers can build interactive user interfaces that facilitate data exploration and visualization. Streamlit’s declarative syntax allows for the straightforward transformation of data scripts into shareable web applications.
In the evolving landscape of data science and machine learning, the ability to rapidly develop and deploy interactive data applications is crucial. Streamlit, an open-source Python library, has emerged as a popular tool for creating such applications with minimal effort. By integrating Streamlit within Snowflake, a leading cloud-based data platform, developers can build, deploy, and share data applications directly within the Snowflake environment. This integration streamlines workflows, enhances security, and leverages Snowflake’s robust data processing capabilities.
Streamlit in Snowflake
Integrating Streamlit within Snowflake enables developers to create and host Streamlit applications directly on Snowflake’s Data Cloud. This integration eliminates the need to move data between platforms, ensuring that data remains secure and governed within Snowflake. Developers can leverage Snowflake’s compute resources to run Streamlit applications, facilitating seamless data processing and visualisation.
Advantages of Using Streamlit in Snowflake
-
Unified Data Processing: Hosting Streamlit applications within Snowflake allows data processing and visualisation to occur in the same environment, reducing latency and complexity.
-
Managed Infrastructure: Snowflake manages the underlying infrastructure for Streamlit applications, allowing developers to focus on application development without the burden of server maintenance.
-
Security and Compliance: Data remains within Snowflake’s secure environment, benefiting from robust security features and compliance certifications.
-
Scalability: Streamlit applications can scale with Snowflake’s compute resources, accommodating varying workloads and user demands.
-
Integration with Snowflake Features: Streamlit applications can seamlessly integrate with Snowflake features like Snowpark, user-defined functions (UDFs), and stored procedures, enhancing their capabilities.
Creating and Deploying Streamlit Applications in Snowflake
Features change from time to time with new features being added regularly, it is recommended that you review the documentation for the latest on what specific features are included with any of the Editions.
Prerequisites
Before creating a Streamlit application in Snowflake, ensure the following:
-
Access Permissions: Appropriate roles and privileges to create and manage Streamlit applications within your Snowflake account.
-
Virtual Warehouse: An active virtual warehouse to run the Streamlit application and execute queries.
-
Snowsight Access: Access to Snowsight, Snowflake’s web interface, for development and deployment.
Steps to Create a Streamlit Application
-
Sign in to Snowsight: Log in to your Snowflake account and navigate to Snowsight.
-
Navigate to Streamlit: In the left navigation bar, select Projects > Streamlit.
-
Create a New Streamlit App: Click on + Streamlit App to initiate a new application.
-
Configure Application Details:
-
App Title: Enter a unique name for your application.
-
App Location: Choose the database and schema where the application will reside.
-
App Warehouse: Select the virtual warehouse to run your application.
-
-
Develop the Application: Utilise the Streamlit editor in Snowsight to write your Python code. The editor provides auto-completion and documentation for Streamlit and Snowpark functions.
-
Run and Preview: Use the Run button to execute your application and preview it in the adjacent pane.
-
Manage Dependencies: If your application requires external Python packages, add them via the Packages option in the editor.
-
Save and Deploy: Once development is complete, save your application. It is now ready to be shared and accessed based on the assigned permissions.
Building Your Own Interactive Data Dashboard
Consider a scenario where you want to build an interactive dashboard to visualise sales data stored in Snowflake.
Step 1: Prepare the Data
- Create the Database, Schema, and Table: Execute the following SQL commands in your Snowflake environment to establish the required structures:
-- Create a new database named 'sales_db'
CREATE DATABASE IF NOT EXISTS sales_db;
-- Create a new schema named 'sales_schema' within 'sales_db'
CREATE SCHEMA IF NOT EXISTS sales_db.sales_schema;
-- Create a table named 'sales_data' within 'sales_schema'
CREATE TABLE IF NOT EXISTS sales_db.sales_schema.sales_data (
sale_id INT AUTOINCREMENT PRIMARY KEY,
sale_date DATE,
region STRING,
product STRING,
quantity INT,
revenue FLOAT
);
- Insert Sample Data into the Table: After creating the table, populate it with sample sales data using the following
INSERT INTO
statements:
-- Insert sample data into 'sales_data' table
INSERT INTO sales_db.sales_schema.sales_data (sale_date, region, product, quantity, revenue) VALUES
('2025-04-01', 'North', 'Product A', 10, 250.00),
('2025-04-02', 'South', 'Product B', 5, 125.00),
('2025-04-03', 'East', 'Product C', 8, 200.00),
('2025-04-04', 'West', 'Product D', 12, 300.00),
('2025-04-05', 'North', 'Product E', 7, 175.00);
Step 2: Create the Streamlit Application
In Snowsight, create a new Streamlit application named SalesDashboard.
Step 3: Develop the Application
In the Streamlit editor, write the following code:
import streamlit as st
import pandas as pd
import altair as alt
from snowflake.snowpark.context import get_active_session
from snowflake.snowpark.functions import col
# Start the Snowpark session from within Snowflake
session = get_active_session()
# Streamlit App Header
st.title("📈 Embedded Sales Dashboard in Snowflake")
st.markdown("""
This app runs directly **inside Snowflake** using **Streamlit!**
Explore regional sales trends and KPIs live from your Snowflake tables.
""")
# Query the Snowflake table using Snowpark DataFrame
df = session.table("sales_db.sales_schema.sales_data")
# Filter UI
st.sidebar.header("🔍 Filter Options")
# Get distinct values for dropdowns
regions_df = df.select("region").distinct().to_pandas()
products_df = df.select("product").distinct().to_pandas()
# Normalize column names
regions_df.columns = [col.lower() for col in regions_df.columns]
products_df.columns = [col.lower() for col in products_df.columns]
regions = regions_df["region"].tolist()
products = products_df["product"].tolist()
# Sidebar widgets
selected_region = st.sidebar.selectbox("Choose Region", regions)
selected_products = st.sidebar.multiselect("Choose Products", products, default=products)
selected_date_range = st.sidebar.date_input(
"Select Date Range",
[pd.to_datetime("2025-04-01"), pd.to_datetime("2025-04-30")]
)
# Apply filters using Snowpark expressions
filtered_df = df.filter(
(col("region") == selected_region) &
(col("product").isin(selected_products)) &
(col("sale_date").between(selected_date_range[0], selected_date_range[1]))
)
# Convert to Pandas for Streamlit display
pandas_df = filtered_df.to_pandas()
# Normalize column names
pandas_df.columns = [col.lower() for col in pandas_df.columns]
# Ensure proper datetime formatting
pandas_df['sale_date'] = pd.to_datetime(pandas_df['sale_date'])
# Show the filtered data table
st.subheader(f"📄 Sales Data: {selected_region}")
st.dataframe(pandas_df, use_container_width=True)
# KPIs
st.markdown("### 🔢 Key Metrics")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Total Revenue", f"${pandas_df['revenue'].sum():,.2f}")
with col2:
st.metric("Total Quantity Sold", f"{pandas_df['quantity'].sum()} units")
with col3:
avg_revenue = pandas_df['revenue'].mean() if not pandas_df.empty else 0
st.metric("Average Revenue per Sale", f"${avg_revenue:,.2f}")
# Charts
st.markdown("### 📊 Revenue Over Time")
if not pandas_df.empty:
line_chart = alt.Chart(pandas_df).mark_line(point=True).encode(
x='sale_date:T',
y='revenue:Q',
color='product:N',
tooltip=['sale_date:T', 'product:N', 'revenue:Q']
).properties(width=700, height=400)
st.altair_chart(line_chart, use_container_width=True)
st.markdown("### 🧭 Revenue by Product")
bar_chart = alt.Chart(pandas_df).mark_bar().encode(
x='product:N',
y='revenue:Q',
color='product:N',
tooltip=['product:N', 'revenue:Q']
).properties(width=700)
st.altair_chart(bar_chart, use_container_width=True)
else:
st.info("No data available for selected filters.")
# Debug info for developers
with st.expander("🛠 Raw Table & Schema Info"):
st.write("Snowflake Table Schema:", df.schema)
st.write("Pandas Columns:", pandas_df.columns.tolist())
Thing To Consider:
-
Permissions: Verify that your Snowflake user role has the necessary privileges to create databases, schemas, tables, and to insert data.
-
Warehouse Selection: Assign an appropriate virtual warehouse in Snowflake to handle the compute resources required for running the Streamlit application and executing queries.
Conclusion
Integrating Streamlit within Snowflake empowers developers to create interactive data applications directly within the data cloud, streamlining workflows and enhancing data-driven decision-making. By leveraging Snowflake’s robust security features and scalable infrastructure, organisations can ensure their applications are both secure and performant. Adopting best practices such as effective caching strategies, managing dependencies, and optimising resource usage will further enhance application efficiency. Embracing Streamlit in Snowflake enables teams to rapidly develop and deploy data applications, fostering a more agile and informed business environment.