-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: add Snowflake example and reorganize directory structure
- Loading branch information
Showing
18 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
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.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |