I made a collapsable menu for different scripts. You can also put your choice in the URL (or copy the URL after having made the choice). And it works at share.streamlit.io
Maybe it’s useful for somebody else
import streamlit as st
import importlib
def dynamic_import(module):
"""Import a module stored in a variable
Args:
module (string): The module to import
Returns:
the module you want
"""
return importlib.import_module(module)
def main():
# [n. name in menu, module name]
options = [["0. welcome","welcome"],
["1. covid dashboard","covid dashboard rcsmit"],
["2. plot hosp ic","plot hosp ic streamlit"],
["3. false positive rate covid test","calculate false positive rate covid test streamlit"]]
query_params = st.experimental_get_query_params() # reading the choice from the URL..
choice = int(query_params["choice"][0]) if "choice" in query_params else 0 # .. and make it the default value
menuchoicelist = [] # making another list with the options to be shown
for n, l in enumerate(options):
menuchoicelist.append(options[n][0])
with st.sidebar.beta_expander('MENU: Choose a script', expanded=True):
menu_choice = st.radio("",menuchoicelist, index=choice)
st.sidebar.markdown("<h1>- - - - - - - - - - - - - - - - - - </h1>", unsafe_allow_html=True)
st.experimental_set_query_params(choice=menuchoicelist.index(menu_choice)) # setting the choice in the URL
for n, l in enumerate(options):
if menu_choice == options[n][0]:
m = options[n][1].replace(" ","_") # I was too lazy to change it in the list
try:
module = dynamic_import(m)
except:
st.error(f"Module '{m}' not found")
st.stop()
try:
module.main()
except:
st.error(f"Function 'main()' in module '{m}' not found")
st.stop()
if __name__ == "__main__":
main()
This adaption reads the files in your directory. Attention, the deeplinks will change if you add new apps.
import streamlit as st
import importlib
import traceback
import os
import platform
st.set_page_config(page_title="Streamlit scripts of René Smit")
def dynamic_import(module):
"""Import a module stored in a variable
Args:
module (string): The module to import
Returns:
the module you want
"""
return importlib.import_module(module)
def main():
if platform.processor() != "":
arr = os.listdir("C:\\Users\\rcxsm\\Documents\\phyton_scripts\\streamlit_scripts")
else:
arr = os.listdir()
counter = 1
options = [["0. welcome","welcome"]]
for file in arr:
if file[-2:] =="py" and ( file != "welcome.py" and file !="menu_streamlit.py"):
menutext = f"{counter}. {file}"
menutext = menutext.replace("_"," ") # I was too lazy to change it in the list
menutext = menutext.replace(".py","") # I was too lazy to change it in the list
file_ = file.replace(".py","") # I was too lazy to change it in the list
options.append([menutext, file_])
counter +=1
query_params = st.experimental_get_query_params() # reading the choice from the URL..
choice = int(query_params["choice"][0]) if "choice" in query_params else 0 # .. and make it the default value
menuchoicelist = [options[n][0] for n, l in enumerate(options)]
with st.sidebar.beta_expander('MENU: Choose a script | scroll down for options/parameters', expanded=True):
menu_choice = st.radio("",menuchoicelist, index=choice)
st.sidebar.markdown("<h1>- - - - - - - - - - - - - - - - - - </h1>", unsafe_allow_html=True)
st.experimental_set_query_params(choice=menuchoicelist.index(menu_choice)) # setting the choice in the URL
for n, l in enumerate(options):
if menu_choice == options[n][0]:
if platform.processor() != "":
m = "C:\\Users\\rcxsm\\Documents\\phyton_scripts\\streamlit_scripts\\" + options[n][1].replace(" ","_") # I was too lazy to change it in the list
st.write (f"{m }")
else:
m = options[n][1].replace(" ","_") # I was too lazy to change it in the list
try:
module = dynamic_import(m)
except Exception as e:
st.error(f"Module '{m}' not found or error in the script\n")
st.warning(f"{e}")
st.warning(traceback.format_exc())
st.stop()
try:
module.main()
except Exception as e:
st.error(f"Function 'main()' in module '{m}' not found or error in the script")
st.warning(f"{e}")
st.warning(traceback.format_exc())
st.stop()
if __name__ == "__main__":
main()
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.