Data Movement · Topic 15 of 17

Streamlit Apps for Data Consumption

Build Streamlit in Snowflake apps for interactive dashboards and self-service data access: runtimes, owner's rights, sharing grants and caching.

  • 4 min
In a nutshell
  • Streamlit in Snowflake runs inside Snowflake, compute and storage managed by Snowflake, access through role based access control.
  • Runtimes: warehouse runtime and container runtime.
  • get_active_session() gives the Snowpark session.

What it is

Streamlit is an open source Python library for building web apps. Streamlit in Snowflake lets you build, deploy and share those apps inside Snowflake, so the data and the app code stay in the platform and Snowflake manages the compute and storage for each app.

A Streamlit app is a Snowflake object. Its source code and environment configuration live in that object, and role based access control decides who can open it.

Python scriptst widgets and chartsdeployed asStreamlit objectin a schemaqueries viaSnowpark sessionruns onWarehousereads tables or views

How it works

Building the app

You create and edit the app in Snowsight with a side by side editor and live preview, or deploy files from a stage or Git with CREATE STREAMLIT.

import streamlit as st
from snowflake.snowpark.context import get_active_session

session = get_active_session()
region = st.selectbox("Region", ["EU", "US", "APAC"])
df = session.sql("SELECT order_date, SUM(amount) AS total "
                 "FROM orders WHERE region = ? GROUP BY order_date",
                 params=[region]).to_pandas()
st.line_chart(df, x="ORDER_DATE", y="TOTAL")
st.dataframe(df)
  • get_active_session() returns the Snowpark session of the running app, so no credentials are written in code.
  • Apps work with Snowpark, UDFs, stored procedures and the Snowflake Native App Framework.
  • A runtime is chosen per app. A warehouse runtime uses a virtual warehouse for the app, while a container runtime runs on a compute pool.
  • Use st.cache_data to avoid rerunning the same query on every widget change, which saves warehouse credits.

Interactive dashboards

A dashboard for exploration pairs inputs with charts.

Streamlit elementRole
st.selectbox, st.multiselect, st.slider, st.date_inputFilters the user changes
st.line_chart, st.bar_chart, st.area_chart, st.mapVisuals that redraw when a filter changes
st.dataframe, st.metricDetail tables and headline numbers
st.tabs, st.sidebarLayout of pages and filter panels
st.download_buttonLets the viewer export what they see

The script reruns from top to bottom when a widget changes, and each rerun can send queries to the warehouse. Push aggregation into SQL and return small result sets, instead of pulling a full table into pandas.

Sharing the app

Sharing is a privilege question.

GRANT USAGE ON STREAMLIT analytics.apps.sales_dashboard TO ROLE analyst;

Viewers also need USAGE on the database and schema that contain the app. With the default owner's rights they do not need privileges on the tables the app reads.

Watch out

By default a Streamlit app runs with the owner's rights (restricted caller's rights for container runtime apps is in preview). Anyone who can open the app sees data the owner role can read, regardless of their own table privileges. Expose only views or aggregates the owner role should show, or apply row access policies that depend on the viewer.

Self-service data access

A self-service app lets business users ask for data without writing SQL. Common designs are:

  • A filter driven data explorer over a curated view or a secure view.
  • A request form that writes a row to a table, which a task or procedure then fulfills.
  • A data entry screen that writes back to a table through a stored procedure with validation.
  • An export page with st.download_button for a filtered extract.
Business userpicks filtersStreamlit appCurated viewgoverned columnsreturnsResultchart and tableBusiness usersubmits formStreamlit appCALLStored procedurevalidateswritesRequest table

Keep governance in Snowflake and not in the app: use views, masking and row access policies on the data the app reads, so that the rules hold if the data is reached another way.

Where Streamlit fits next to other tools

NeedBetter tool
Custom interactive app with inputs and write backStreamlit in Snowflake
Fixed charts on a query resultSnowsight dashboard
Exploratory analysis in cellsNotebooks
App to sell with its own install and dataSnowflake Native App, which can include Streamlit

Common mistakes

  • Forgetting GRANT USAGE ON STREAMLIT, so the intended role cannot open the app.
  • Assuming viewers are restricted by their own privileges. The app runs with the owner's rights.
  • Loading a whole table into pandas on each widget change instead of querying aggregates.
  • Putting credentials in the app code. The active session already provides access.
  • Putting access rules in the Python code instead of in views or policies.
  • Skipping caching, so every interaction reruns every query.

What to remember for the exam

  • Streamlit in Snowflake runs inside Snowflake, compute and storage managed by Snowflake, access through role based access control.
  • Runtimes: warehouse runtime and container runtime.
  • get_active_session() gives the Snowpark session.
  • Sharing uses GRANT USAGE ON STREAMLIT to a role. The app runs with owner's rights.
  • Use caching and SQL aggregation to control warehouse use.
  • For self-service, put governance on the data (views, policies), and use procedures for write back.

Check yourself

  • Which privilege lets a role open a Streamlit app?

    USAGE on the Streamlit object.

  • A viewer lacks privileges on a table but can see its data in a Streamlit app. Why?

    The app runs with the owner's rights, so it reads data as the owner role.

  • How do you stop a dashboard from re-querying on every widget change?

    Cache query results with st.cache_data and keep queries aggregated in SQL.

Topic 15 of 17 in Data Movement

Test yourself on Data Movement

The full Snowflake DEA-C02 question bank has 709 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Snowflake. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.