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/.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/.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 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 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