Skip to content

Instantly share code, notes, and snippets.

@asehmi
Created May 19, 2022 13:29
Show Gist options
  • Save asehmi/787bf724a6cf1ff1a9b707f8091be10c to your computer and use it in GitHub Desktop.
Save asehmi/787bf724a6cf1ff1a9b707f8091be10c to your computer and use it in GitHub Desktop.
Check if an app is running under Streamlit or native Python
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