Spaces:
Runtime error
Runtime error
Create new file
Browse files- appStore/multiapp.py +51 -0
appStore/multiapp.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Frameworks for running multiple Streamlit applications as a single app.
|
2 |
+
"""
|
3 |
+
import streamlit as st
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
class MultiApp:
|
7 |
+
"""Framework for combining multiple streamlit applications.
|
8 |
+
Usage:
|
9 |
+
def foo():
|
10 |
+
st.title("Hello Foo")
|
11 |
+
def bar():
|
12 |
+
st.title("Hello Bar")
|
13 |
+
app = MultiApp()
|
14 |
+
app.add_app("Foo", foo)
|
15 |
+
app.add_app("Bar", bar)
|
16 |
+
app.run()
|
17 |
+
It is also possible keep each application in a separate file.
|
18 |
+
import foo
|
19 |
+
import bar
|
20 |
+
app = MultiApp()
|
21 |
+
app.add_app("Foo", foo.app)
|
22 |
+
app.add_app("Bar", bar.app)
|
23 |
+
app.run()
|
24 |
+
"""
|
25 |
+
def __init__(self):
|
26 |
+
self.apps = []
|
27 |
+
|
28 |
+
def add_app(self, title, func):
|
29 |
+
"""Adds a new application.
|
30 |
+
Parameters
|
31 |
+
----------
|
32 |
+
func:
|
33 |
+
the python function to render this app.
|
34 |
+
title:
|
35 |
+
title of the app. Appears in the dropdown in the sidebar.
|
36 |
+
"""
|
37 |
+
self.apps.append({
|
38 |
+
"title": title,
|
39 |
+
"function": func
|
40 |
+
})
|
41 |
+
|
42 |
+
def run(self):
|
43 |
+
st.sidebar.write(format_func=lambda app: app['title'])
|
44 |
+
image = Image.open('appStore/img/giz_sdsn.jpg')
|
45 |
+
st.sidebar.image(image)
|
46 |
+
app = st.sidebar.radio(
|
47 |
+
'Go To',
|
48 |
+
self.apps,
|
49 |
+
format_func=lambda app: app['title'])
|
50 |
+
|
51 |
+
app['function']()
|