Skip to content

Commit

Permalink
doc: add Snowflake example and reorganize directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkegley committed Sep 5, 2024
1 parent 85abfce commit 9ba7992
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
41 changes: 41 additions & 0 deletions examples/connect/snowflake/streamlit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Streamlit Example

## Start the app locally

```bash
SNOWFLAKE_ACCOUNT = "<snowflake-account-identifier>"
SNOWFLAKE_WAREHOUSE = "<snowflake-warehouse-name>"

# USER is only required when running the example locally with external browser auth
SNOWFLAKE_USER="<snowflake-username>" streamlit run app.py
```

## Deploy to Posit Connect

Validate that `rsconnect-python` is installed:

```bash
rsconnect version
```

Or install it as documented in the [installation](https://docs.posit.co/rsconnect-python/#installation) section of the documentation.

To publish, make sure `CONNECT_SERVER`, `CONNECT_API_KEY`, `SNOWFLAKE_ACCOUNT`, `SNOWFLAKE_WAREHOUSE` have valid values. Then, on a terminal session, enter the following command:

```bash
rsconnect deploy streamlit . \
--server "${CONNECT_SERVER}" \
--api-key "${CONNECT_API_KEY}" \
--environment SNOWFLAKE_ACCOUNT \
--environment SNOWFLAKE_WAREHOUSE
```

Note that the Snowflake environment variables do not need to be resolved by the shell, so they do not include the `$` prefix.

The Snowflake environment variables only need to be set once, unless a change needs to be made. If the values have not changed, you don’t need to provide them again when you publish updates to the document.

```bash
rsconnect deploy streamlit . \
--server "${CONNECT_SERVER}" \
--api-key "${CONNECT_API_KEY}"
```
43 changes: 43 additions & 0 deletions examples/connect/snowflake/streamlit/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
# mypy: ignore-errors
import os

import pandas as pd
import streamlit as st
import snowflake.connector

from posit.connect.external.snowflake import PositAuthenticator

ACCOUNT = os.getenv("SNOWFLAKE_ACCOUNT")
WAREHOUSE = os.getenv("SNOWFLAKE_WAREHOUSE")

# USER is only required when running the example locally with external browser auth
USER = os.getenv("SNOWFLAKE_USER")

# https://docs.snowflake.com/en/user-guide/sample-data-using
DATABASE = os.getenv("SNOWFLAKE_DATABASE", "snowflake_sample_data")
SCHEMA = os.getenv("SNOWFLAKE_SCHEMA", "tpch_sf1")
TABLE = os.getenv("SNOWFLAKE_TABLE", "lineitem")

session_token = st.context.headers.get("Posit-Connect-User-Session-Token")
auth = PositAuthenticator(
local_authenticator="EXTERNALBROWSER",
user_session_token=session_token)

con = snowflake.connector.connect(
user=USER,
account=ACCOUNT,
warehouse=WAREHOUSE,
database=DATABASE,
schema=SCHEMA,
authenticator=auth.authenticator(),
token=auth.token(),
)

snowflake_user = con.cursor().execute("SELECT CURRENT_USER()").fetchone()
st.write(f"Hello, {snowflake_user[0]}!")

with st.spinner("Loading data from Snowflake..."):
df = pd.read_sql_query(f"SELECT * FROM {TABLE} LIMIT 10", con)

st.dataframe(df)
3 changes: 3 additions & 0 deletions examples/connect/snowflake/streamlit/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
snowflake-connector-python==3.12.1
streamlit==1.37.0
posit-sdk>=0.4.1

0 comments on commit 9ba7992

Please sign in to comment.