Streamlit
Not only does it make creating single-page apps a snap, but it also supports the building ofmulti-page application, greatly enriching the user experience and the possibilities for data exploration.
As weStreamlit App
After the gradual increase in the number of functions, it is inconvenient to display too much information on a single page.
Multiple pages allow functionally related sections to be organized together into logical multiple pages, allowing users to easily interact with different functional modules.
From the code side, multi-page applications split different functional modules into separate pages, each of which can have its own code logic and data flow.
This helps to modularize the code, making the code structure clearer and easier to manage.
In terms of operational performance, multi-page applications can speed up page loading because the user only needs to load the content of the current desired page, not the entire content of the entire application.
In addition, for complex applications, multi-page applications make it easier to iterate and extend functionality.
As the application grows, new pages and functional modules can be added incrementally without the need for extensive modifications to existing pages.
This post focuses on building aStreamlit
The basic knowledge needed to master the multi-page applications of the
1. Documentation structure for multi-page applications
existStreamlit
The layout of files and folders in a multi-page application is critical to the organization, management and maintenance of the project.
Here is a recommended layout:
my_app/
├── # The main application file, responsible for starting the application and configuring routes.
├── pages/
│ ├── __init__.py # Optional for treating the pages folder as a Python package
│ ├──
│ ├──
│ └── ... # Other page files
├── session_state.py # Session State management class file
└── # Common functions
When extending the functionality, thepages
folder to add a newpy fileReady to go.
included among thesesession_state.py
cap (a poem)Not required, when the application of
session
Management becomes complicated, or when there are many common functions that need to be managed in a separate file.
For simple multi-page applications, you generally only need the above,
cap (a poem)
That's enough.
2. Navigation in multi-page applications
existStreamlit
In this case, use the, can help us easily create dynamic navigation menus.
For example, with,
cap (a poem)
As an example, create a multi-page application.
#
import streamlit as st
page1 = ("pages/", title="Page 1")
page2 = ("pages/", title="Page 2")
pg = ([page1, page2])
()
#
import streamlit as st
("This is page 1")
#
import streamlit as st
("This is page 2")
pass (a bill or inspection etc)streamlit run
Once launched, a simple multi-page application with navigation is complete.
The menu in the sidebar allows you to switch pages freely.
In addition to passinggenerated menu to switch pages.
Streamlit
It is also provided in thest.switch_page
Methods.
It is possible to navigate to other pages within a page.
For example, it is possible to add thecap (a poem)
Add a button to navigate to each other in the
#
import streamlit as st
("Here's the page. 1")
if ("GoTo Page 2"):
st.switch_page("pages/")
#
import streamlit as st
("Here's the page. 2")
if ("GoTo Page 1"):
st.switch_page("pages/")
3. Sharing data between multiple pages
Finally, it describes how to share data directly on different pages, so that the functions of different pages can be linked together.
There are several options for sharing data between multiple pages in Streamlit.
The first option is to useglobal variable,
However, there are some problems with this approach, such as data inconsistency and difficulty in debugging when accessing concurrently.
Therefore, the generalnot recommendedUse global variables to share data.
The second program isUsing external storage, such as keeping shared data in files or databases. This solution is suitable for application scenarios that require relatively large applications, or persistent storage.
If your application is small and doesn't require persistent storage, it seems a bit clunky to use this solution.
The last option isSession State
This isStreamlit
provides a mechanism that is particularly well suited for passing and saving state data between different pages.
The following mockup is constructed to demonstrate how to share data between different pages.
first inin which we can select the dataset.
after thatin which the analysis will automatically start based on the dataset we select.
#
import streamlit as st
("This is page 1")
if ("GoTo Page 2").
st.switch_page("pages/")
datalist = ("", "Demographic Data", "Environmental Data", "Transaction Data")
if "dataset" is not in st.session_state:
option = (
"Please select a dataset",
datalist.
)
else: option = ("Please select dataset", datalist, )
option = st.session_state.dataset
option = (
"Please select a dataset.",
datalist,
index=(option),
)
if option == "".
("No dataset is currently selected.")
else: ("You are currently selecting: [", option, "]")
("Your current selection is: [", option, "]")
st.session_state.dataset = option
Save the name of the selected dataset to the
Session State
Center.
#
import streamlit as st
("Here's the page. 2")
if ("GoTo Page 1"):
st.switch_page("pages/")
if "dataset" not in st.session_state or st.session_state.dataset == "":
("No dataset is currently selected")
else:
("Start analyzing the dataset: 【", st.session_state.dataset, "】")
direct from
Session State
The name of the dataset to read in.