Menu for different scripts

Hello

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 :wink:

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()
2 Likes