Spaces:
Build error
Build error
Achyut Tiwari
commited on
Add files via upload
Browse files- multipage.py +65 -0
multipage.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This file is the framework for generating multiple Streamlit applications
|
| 3 |
+
through an object oriented framework.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
# Import necessary libraries
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from streamlit_option_menu import option_menu
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Define the multipage class to manage the multiple apps in our program
|
| 12 |
+
class MultiPage:
|
| 13 |
+
"""Framework for combining multiple streamlit applications."""
|
| 14 |
+
|
| 15 |
+
def __init__(self) -> None:
|
| 16 |
+
"""Constructor class to generate a list which will store all our applications as an instance variable."""
|
| 17 |
+
self.pages = []
|
| 18 |
+
|
| 19 |
+
def add_page(self, title, icon, func) -> None:
|
| 20 |
+
"""Class Method to Add pages to the project
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
title ([str]): The title of page which we are adding to the list of apps
|
| 24 |
+
|
| 25 |
+
func: Python function to render this page in Streamlit
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
self.pages.append(
|
| 29 |
+
{
|
| 30 |
+
"title": title,
|
| 31 |
+
"icon": icon,
|
| 32 |
+
"function": func
|
| 33 |
+
}
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
def run(self):
|
| 37 |
+
# Drodown to select the page to run
|
| 38 |
+
st.markdown("""
|
| 39 |
+
<style>
|
| 40 |
+
section[data-testid="stSidebar"] > div:first-of-type {
|
| 41 |
+
background-color: var(--secondary-background-color);
|
| 42 |
+
background: var(--secondary-background-color);
|
| 43 |
+
width: 250px;
|
| 44 |
+
padding: 4rem 0;
|
| 45 |
+
box-shadow: -2rem 0px 2rem 2rem rgba(0,0,0,0.16);
|
| 46 |
+
}
|
| 47 |
+
section[aria-expanded="true"] > div:nth-of-type(2) {
|
| 48 |
+
display: none;
|
| 49 |
+
}
|
| 50 |
+
.main > div:first-of-type {
|
| 51 |
+
padding: 1rem 0;
|
| 52 |
+
}
|
| 53 |
+
</style>
|
| 54 |
+
""", unsafe_allow_html=True)
|
| 55 |
+
|
| 56 |
+
with st.sidebar:
|
| 57 |
+
selected = option_menu(None, [page["title"] for page in self.pages],
|
| 58 |
+
icons=[page["icon"] for page in self.pages],
|
| 59 |
+
menu_icon="cast", default_index=0)
|
| 60 |
+
|
| 61 |
+
# Run the selected page
|
| 62 |
+
for index, item in enumerate(self.pages):
|
| 63 |
+
if item["title"] == selected:
|
| 64 |
+
self.pages[index]["function"]()
|
| 65 |
+
break
|