NextDrought/nextdrought
1
1"""Frameworks for running multiple Streamlit applications as a single app.2"""3import streamlit as st4 5# app_state = st.experimental_get_query_params()6# app_state = {k: v[0] if isinstance(v, list) else v for k, v in app_state.items()} # fetch the first item in each query string as we don't have multiple values for each query string key in this example7 8 9class MultiApp:10 """Framework for combining multiple streamlit applications.11 Usage:12 def foo():13 st.title("Hello Foo")14 def bar():15 st.title("Hello Bar")16 app = MultiApp()17 app.add_app("Foo", foo)18 app.add_app("Bar", bar)19 app.run()20 It is also possible keep each application in a separate file.21 import foo22 import bar23 app = MultiApp()24 app.add_app("Foo", foo.app)25 app.add_app("Bar", bar.app)26 app.run()27 """28 29 def __init__(self):30 self.apps = []31 32 def add_app(self, title, func):33 """Adds a new application.34 Parameters35 ----------36 func:37 the python function to render this app.38 title:39 title of the app. Appears in the dropdown in the sidebar.40 """41 self.apps.append({"title": title, "function": func})42 43 def run(self):44 app_state = st.experimental_get_query_params()45 app_state = {46 k: v[0] if isinstance(v, list) else v for k, v in app_state.items()47 } # fetch the first item in each query string as we don't have multiple values for each query string key in this example48 49 # st.write('before', app_state)50 51 titles = [a["title"] for a in self.apps]52 functions = [a["function"] for a in self.apps]53 default_radio = titles.index(app_state["page"]) if "page" in app_state else 054 55 st.sidebar.title("Navigation")56 57 title = st.sidebar.radio("Go To", titles, index=default_radio, key="radio")58 59 app_state["page"] = st.session_state.radio60 # st.write('after', app_state)61 62 st.experimental_set_query_params(**app_state)63 # st.experimental_set_query_params(**st.session_state.to_dict())64 functions[titles.index(title)]()65 