Skip to content

About

A Streamlit in Snowflake dashboard and the server-side flow for embedding it in your own web pages

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

streamlit-snowflake-embed

A Streamlit in Snowflake dashboard, and the small backend and host page that embed it in your own website.

What this shows

  • A Streamlit dashboard that runs in Snowflake on a container runtime, and also runs on a laptop against bundled sample data, with one data-access layer choosing the source.
  • Minting a single-use embed URL on the server through Snowflake's documented generate-embed-url endpoint, with a programmatic access token or key-pair JWT.
  • A host page that requests one URL per page load and puts the app in an iframe.
  • The grants that keep it least-privilege: a dedicated minting role with USAGE and EMBED on one app, for viewers with or without a Snowflake account.
  • A mint endpoint that refuses requests unless you have plugged in your own sign-in, because anyone who can call it gets a session in the app.

Read the article

Streamlit in Snowflake, now embeddable in your own pages

Quick start

You need Python 3.11 or later (Streamlit 1.67 requires it).

Run the dashboard locally on sample data:

python -m venv .venv && source .venv/bin/activate
pip install -r requirements-dev.txt
ORDERS_DATA_SOURCE=sample streamlit run app/streamlit_app.py

Run the tests:

pytest -q

Put it in Snowflake and embed it

  1. Load the sample data into a table: sql/01_orders_table.sql.

  2. Deploy the app on a container runtime: sql/02_deploy_app.sql.

  3. Register your page's origin, create the minting service user and role, and issue a token: sql/03_embedding_setup.sql.

  4. Configure and start the host:

    cp embed_host/.env.example embed_host/.env   # then fill it in
    set -a && source embed_host/.env && set +a
    uvicorn embed_host.server:app --port 8000
    
  5. Open http://localhost:8000. PARENT_ORIGIN must be exactly the origin in your browser's address bar, and it must be registered in step 3.

ALLOW_ANONYMOUS_MINT=true lets anyone who can reach the server mint a URL. Use it only on localhost. In a real portal, replace require_viewer() in embed_host/server.py with a check against your own sign-in.

For viewers who have their own Snowflake accounts, see sql/04_viewers_with_snowflake_accounts.sql. There the URL is minted with each viewer's OAuth token and role, and the app queries as the viewer.

Repository layout

app/
  streamlit_app.py           the dashboard
  data_source.py             Snowflake or bundled sample, same columns either way
  sample_data/daily_orders.csv   synthetic data (scripts/generate_sample_data.py)
embed_host/
  snowflake_embed.py         builds and sends the mint request; PAT and key-pair JWT auth
  server.py                  FastAPI: GET / (host page), GET /api/embed-url (mint)
  static/index.html          the host page with the iframe
  .env.example               configuration the backend reads
sql/
  01_orders_table.sql        demo table and data load
  02_deploy_app.sql          compute pool and CREATE STREAMLIT on a container runtime
  03_embedding_setup.sql     allowed origins, service user, minting role, grants, token
  04_viewers_with_snowflake_accounts.sql   per-viewer model with caller's rights
tests/                       pytest suite

What is tested

CI (.github/workflows/ci.yml, Python 3.11 on ubuntu-latest) runs:

  • pytest: the dashboard through Streamlit's AppTest (it renders; region, channel and date filters change the numbers; an empty selection warns; a load failure shows a safe message), the data-source rules, the mint request (method, path, headers and body as documented, using a mocked Snowflake), key-pair JWT claims and signature, and the backend routes including the default refusal.
  • A smoke test that starts the dashboard headless and checks its health endpoint.
  • A smoke test that starts the host server and loads the page.

Not verified here, because it needs a live Snowflake account:

  • The SQL in sql/. Statements follow Snowflake's documentation but have not been executed.
  • A real call to generate-embed-url, and the app rendering inside the iframe.
  • The dashboard running inside Streamlit in Snowflake. It relies on the container runtime providing streamlit and pandas without an artifact repository.

Built by Cloudcentrix, cloud and data engineering in South Africa. https://cloudcentrix.co.za

About

A Streamlit in Snowflake dashboard and the server-side flow for embedding it in your own web pages

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages