From af37605c3e3080171a37396460172e03fb4f8194 Mon Sep 17 00:00:00 2001 From: Kamesh Sampath Date: Mon, 11 Nov 2024 21:39:40 +0530 Subject: [PATCH 1/3] (fix): add GH workflow and app dir --- .github/workflows/sis.yml | 60 +++++++++++++++++++++++++++++++++++++++ app/.gitignore | 5 ++++ app/common/__init__.py | 0 app/common/data_utils.py | 18 ++++++++++++ app/environment.yml | 6 ++++ app/snowflake.yml | 20 +++++++++++++ app/streamlit_app.py | 5 ++++ config/config.toml | 5 ++++ 8 files changed, 119 insertions(+) create mode 100644 .github/workflows/sis.yml create mode 100644 app/.gitignore create mode 100644 app/common/__init__.py create mode 100644 app/common/data_utils.py create mode 100644 app/environment.yml create mode 100644 app/snowflake.yml create mode 100644 app/streamlit_app.py create mode 100644 config/config.toml diff --git a/.github/workflows/sis.yml b/.github/workflows/sis.yml new file mode 100644 index 0000000..35911fc --- /dev/null +++ b/.github/workflows/sis.yml @@ -0,0 +1,60 @@ +name: Deploy to Streamlit in Snowflake +on: + # manual trigger + workflow_dispatch: + # TODO other triggers + # does build and deploy app on push + # push: + # paths: + # - "data/*.csv" + # - "app/**" + # - "streamlit_app.py" +env: + SNOWFLAKE_DEFAULT_CONNECTION_NAME: "workflow" + SNOWFLAKE_CONNECTIONS_WORKFLOW_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }} + SNOWFLAKE_CONNECTIONS_WORKFLOW_USER: ${{ secrets.SNOWFLAKE_USER }} + SNOWFLAKE_CONNECTIONS_WORKFLOW_PRIVATE_KEY_PASSPHRASE: + ${{ secrets.PRIVATE_KEY_PASSPHRASE }} + SNOWFLAKE_CONNECTIONS_WORKFLOW_PRIVATE_KEY_RAW: + ${{ secrets.PRIVATE_KEY_RAW }} + # update it as per your application settings + STREAMLIT_APP_NAME: my_app + STREAMLIT_APP_WH: my_wh + STREAMLIT_APP_DB: my_app_db + STREAMLIT_APP_SCHEMA: apps + STREAMLIT_APP_DATA_SCHEMA: data +jobs: + Deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ env.GITHUB_REF_NAME }} + - uses: Snowflake-Labs/snowflake-cli-action@v1.5 + with: + cli-version: "latest" + default-config-file-path: + ${{ github.workspace }}/config/config.toml + - name: Check Version and Verify Connection + env: + PRIVATE_KEY_PASSPHRASE: ${{ secrets.PRIVATE_KEY_PASSPHRASE }} + run: | + snow --version + snow connection test + echo "Using branch $GITHUB_REF_NAME" + + - name: Deploy Streamlit App + env: + PRIVATE_KEY_PASSPHRASE: ${{ secrets.PRIVATE_KEY_PASSPHRASE }} + run: | + snow streamlit deploy --replace \ + --database $STREAMLIT_APP_DB --schema $STREAMLIT_APP_SCHEMA + working-directory: app + + - name: Get App URL + env: + PRIVATE_KEY_PASSPHRASE: ${{ secrets.PRIVATE_KEY_PASSPHRASE }} + run: | + snow streamlit get-url $STREAMLIT_APP_NAME \ + --database $STREAMLIT_APP_DB --schema $STREAMLIT_APP_SCHEMA + working-directory: app diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..a266984 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1,5 @@ +.packages/ +.venv/ +app.zip +__pycache__ +TODO.md \ No newline at end of file diff --git a/app/common/__init__.py b/app/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/common/data_utils.py b/app/common/data_utils.py new file mode 100644 index 0000000..8fec9ba --- /dev/null +++ b/app/common/data_utils.py @@ -0,0 +1,18 @@ +from snowflake.snowpark.session import Session +from snowflake.snowpark.table import Table +import pandas as pd +import os + +import streamlit as st +import logging + +logger = logging.getLogger("truck_analyis") + + +@st.cache_resource(show_spinner=True) +def get_active_session() -> Session: + """Create or get new Snowflake Session""" + conn = st.connection( + os.getenv("SNOWFLAKE_CONNECTION_NAME", "snowflake"), type="snowflake" + ) + return conn.session() diff --git a/app/environment.yml b/app/environment.yml new file mode 100644 index 0000000..cc6d68d --- /dev/null +++ b/app/environment.yml @@ -0,0 +1,6 @@ +name: sf_env +channels: + - snowflake +dependencies: + - streamlit=1.35.0 + - snowflake-snowpark-python diff --git a/app/snowflake.yml b/app/snowflake.yml new file mode 100644 index 0000000..44a72d9 --- /dev/null +++ b/app/snowflake.yml @@ -0,0 +1,20 @@ +definition_version: "2" +env: + STREAMLIT_APP_NAME: my_app + STREAMLIT_APP_DB: my_app_db + STREAMLIT_APP_SCHEMA: apps + STREAMLIT_APP_WH: my_wh +entities: + todos_app: + type: streamlit + identifier: + name: <% ctx.env.STREAMLIT_APP_NAME %> + database: <% ctx.env.STREAMLIT_APP_DB %> + schema: <% ctx.env.STREAMLIT_APP_SCHEMA %> + main_file: streamlit_app.py + query_warehouse: <% ctx.env.STREAMLIT_APP_WH %> + stage: <% ctx.env.STREAMLIT_APP_NAME %> + artifacts: + - streamlit_app.py + - environment.yml + - common/data_utils.py diff --git a/app/streamlit_app.py b/app/streamlit_app.py new file mode 100644 index 0000000..b2757f1 --- /dev/null +++ b/app/streamlit_app.py @@ -0,0 +1,5 @@ +import streamlit as st + +st.title("🎈 App Name") + +st.write("Hello world!") diff --git a/config/config.toml b/config/config.toml new file mode 100644 index 0000000..6adfa71 --- /dev/null +++ b/config/config.toml @@ -0,0 +1,5 @@ +default_connection_name = "workflow" + +[connections] +[connections.workflow] +authenticator="SNOWFLAKE_JWT" \ No newline at end of file From 790d14ba02fc3e146f9d8786465c26987b53b033 Mon Sep 17 00:00:00 2001 From: Kamesh Sampath Date: Mon, 11 Nov 2024 21:40:06 +0530 Subject: [PATCH 2/3] (chore): bump streamlit version --- requirements.txt | 2 +- streamlit_app.py | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) mode change 100644 => 120000 streamlit_app.py diff --git a/requirements.txt b/requirements.txt index ed95611..89a9c39 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -streamlit>=1.26.0 +streamlit=1.35.0 diff --git a/streamlit_app.py b/streamlit_app.py deleted file mode 100644 index 3a8699f..0000000 --- a/streamlit_app.py +++ /dev/null @@ -1,5 +0,0 @@ -import streamlit as st - -st.title('🎈 App Name') - -st.write('Hello world!') diff --git a/streamlit_app.py b/streamlit_app.py new file mode 120000 index 0000000..3546be4 --- /dev/null +++ b/streamlit_app.py @@ -0,0 +1 @@ +app/streamlit_app.py \ No newline at end of file From 18d148281cb90169f0e4b289386981103137beb4 Mon Sep 17 00:00:00 2001 From: Kamesh Sampath Date: Mon, 11 Nov 2024 21:40:33 +0530 Subject: [PATCH 3/3] (chore): formatter, standard ignores --- .editorconfig | 12 ++++++++++++ .gitignore | 2 ++ README.md | 5 +++++ 3 files changed, 19 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..c1322dc --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline = false \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fabc31 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.direnv +**/.envrc \ No newline at end of file diff --git a/README.md b/README.md index 2406f95..d6f2c31 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,8 @@ This is filler text, please replace this with a explanatory text about further r - Resource 1 - Resource 2 - Resource 3 + + +## GitHub Workflow + +The [GitHub workflow](./.github/workflows/sis.yml) allows you to deploy this application to Snowflake. \ No newline at end of file