From a192462fed8daf6ea766bf1a13e24fc94d251f2e Mon Sep 17 00:00:00 2001 From: Joshua Carroll Date: Wed, 21 Jun 2023 12:04:10 -0700 Subject: [PATCH] sis-local: Update install path and improve running_in_sis() impl (#20) * Update install path and improve running_in_sis() impl * More explainable running_in_sis() --- samples/streamlit-in-snowflake/sis-local/README.md | 12 ++++++------ .../sis-local/example/README.md | 4 ++-- .../sis-local/example/sis_app.py | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/samples/streamlit-in-snowflake/sis-local/README.md b/samples/streamlit-in-snowflake/sis-local/README.md index 55ebbce..9196ef1 100644 --- a/samples/streamlit-in-snowflake/sis-local/README.md +++ b/samples/streamlit-in-snowflake/sis-local/README.md @@ -15,7 +15,7 @@ and provide a much smoother experience doing local development and porting to Si Just like Snowpark, **it requires python 3.8.** ```shell -pip install "streamlit_in_snowflake @ git+https://github.com/blackary/sis-tricks.git#subdirectory=sis-local" +pip install "streamlit_in_snowflake @ git+https://github.com/Snowflake-Labs/sf-samples.git#subdirectory=samples/streamlit-in-snowflake/sis-local" ``` In general, we recommend managing your environment with Conda, @@ -33,11 +33,11 @@ from snowflake.snowpark.exceptions import SnowparkSessionException def running_in_sis() -> bool: - try: - sess = get_active_session() - return True - except SnowparkSessionException: - return False + import snowflake.connector.connection + import inspect + # snowflake.connector.connection.SnowflakeConnection does not exist inside a Stored Proc or Streamlit. + # It is only part of the external package. So this returns true only in SiS. + return 0 == len([x for x in inspect.getmembers(snowflake.connector.connection) if x[0] == 'SnowflakeConnection']) if running_in_sis(): diff --git a/samples/streamlit-in-snowflake/sis-local/example/README.md b/samples/streamlit-in-snowflake/sis-local/example/README.md index 28c42dc..3d23735 100644 --- a/samples/streamlit-in-snowflake/sis-local/example/README.md +++ b/samples/streamlit-in-snowflake/sis-local/example/README.md @@ -5,7 +5,7 @@ 3. Run the following ```shell -pip install "streamlit_in_snowflake @ git+https://github.com/blackary/sis-tricks.git#subdirectory=sis-local" +pip install "streamlit_in_snowflake @ git+https://github.com/Snowflake-Labs/sf-samples.git#subdirectory=samples/streamlit-in-snowflake/sis-local" python -m streamlit run sis_app.py ``` @@ -28,6 +28,6 @@ conda init --all # See conda docs on what this does / how to use it conda env create -f environment.yml conda activate sis-example # Install streamlit_in_snowflake and run the example app as above -pip install -r "streamlit_in_snowflake @ git+https://github.com/blackary/sis-tricks.git#subdirectory=sis-local" +pip install "streamlit_in_snowflake @ git+https://github.com/Snowflake-Labs/sf-samples.git#subdirectory=samples/streamlit-in-snowflake/sis-local" python -m streamlit run sis_app.py ``` diff --git a/samples/streamlit-in-snowflake/sis-local/example/sis_app.py b/samples/streamlit-in-snowflake/sis-local/example/sis_app.py index 438cee9..751c245 100644 --- a/samples/streamlit-in-snowflake/sis-local/example/sis_app.py +++ b/samples/streamlit-in-snowflake/sis-local/example/sis_app.py @@ -3,11 +3,11 @@ def running_in_sis() -> bool: - try: - sess = get_active_session() - return True - except SnowparkSessionException: - return False + import snowflake.connector.connection + import inspect + # snowflake.connector.connection.SnowflakeConnection does not exist inside a Stored Proc or Streamlit. + # It is only part of the external package. So this returns true only in SiS. + return 0 == len([x for x in inspect.getmembers(snowflake.connector.connection) if x[0] == 'SnowflakeConnection']) if running_in_sis():