Created
May 19, 2022 13:29
-
-
Save asehmi/787bf724a6cf1ff1a9b707f8091be10c to your computer and use it in GitHub Desktop.
Check if an app is running under Streamlit or native Python
This file contains hidden or 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
def can_use_streamlit(): | |
""" | |
Function to check whether python code is run within streamlit | |
Requires Streamlit>=1.9.0 | |
Returns | |
------- | |
can_use_streamlit : boolean | |
True if code is run within streamlit, else False | |
""" | |
try: | |
# Streamlit < 1.9.0 | |
# from streamlit.script_run_context import get_script_run_ctx | |
# Streamlit >= 1.9.0 | |
from streamlit.scriptrunner import get_script_run_ctx | |
return get_script_run_ctx() is not None | |
except ModuleNotFoundError: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment