This article focuses onStreamlit
the core architecture and operational mechanisms of the
The purpose is to hope that friends can first understand the overall macro of theStreamlit
, utilizing the mechanisms it provides to develop applications with more efficient performance.
1. Architecture
Streamlit
It is more specific, it is for the userBS Architectureapp, and with the developer it's really more like aCS ArchitectureThe application.
Why?Streamlit
more likeCS ArchitectureAnd?
on account ofback endFunctions andforward part of sth.The UI part is all done withPython
Write, so developStreamlit
The application feels like developing a desktop application.
If you have experience in developing CS applications such as QT, winform for .Net platform or WPF, then developingStreamlit
The application will feel very intimate.
While it is ultimately in the browser that theStreamlit
application, but developed with absolutely no front-end knowledge of HTML, CSS or JavaScript.
However.Streamlit App
After deployment, you need to pay attention to itBS Applicationsside of the story:
-
Streamlit App
After the release is used by multiple users, the server configuration resources are considered based on the estimated number of users - The user side uses the browser to use the
Streamlit App
, so there is no access to the server's files, directories, or operating system. - If you need to communicate with any peripheral device (such as a camera), you must use the
Streamlit
commands or custom components that will access these devices through the user's browser
2. Operational processes
Streamlit
The main process is simple and straightforward:
- The server side passes the
streamlit run
command activation - initialization
App
web page - Client opens browser to access
- Users operate in the browser
- Servers process data based on user actions
- Update page after processing
- New page back to browser
Streamlit App
After each user action is received, the entire code is re-run and the re-rendered page is returned, the
This raises two obvious questions.
- If the code is loaded with a large amount of data, the data will be reloaded after each user action, affecting performance
For example:
import streamlit as st
import pandas as pd
from datetime import datetime
## Data loading
def load_data():
df = ()
= pd.date_range("2024/10/01", periods=20)
df["A"] = range(20)
df["B"] = range(20)
(f"Load data time:{().strftime("%Y-%m-%d %H:%M:%S")}")
return df
# Load data
data = load_data()
date_range = (
"Date range",
min_value=datetime(2024, 10, 1),
max_value=datetime(2024, 10, 20),
value=(datetime(2024, 10, 1), datetime(2024, 10, 20)),
)
data = data[ >= date_range[0]]
data = data[ <= date_range[1]]
(data)
Each time the data is loaded, the time the data was loaded is displayed:(f "Time to load data: {().strftime("%Y-%m-%d %H:%M:%S")}")
Each moveDate rangeThe entire data is reloaded whenever it is.
- Multiple user operations directly if they are linked, the state between operations cannot be maintained
For example:
count = 0
(f "Click {count}")
if ("ADD"): # Execute when the button is clicked
count += 1
Click the button aboveADD
, the text is displayed always:Click 0
。
Because, when the button is clicked, the execution of thecount += 1
After that, it will also re-execute the entire code, thecount=0
It was also re-executed.
The next section describes how to address these two issues.
3. Cache and status
settle (a dispute)Streamlit App
The problem of not being able to save the data and state of the(computing) cache(cache
(math.) andstate of affairs(session
) Two important functions.
First, use the(computing) cacheto improve the data loading problem in the previous section.
## Data loading
@st.cache_data
def load_data().
#... Omit ...
The modification is very simple, as long as the original code in theload_data
Add a decorator on top of the function@st.cache_data
That's enough.
After the improvement, the mobileDate rangeWhen it does, the load data time stays the same, indicating that there is no repeated loading of data.
Next, use thestate of affairs(session
) to fix the problem in the previous section where the count would not increase.
if "count" not in st.session_state:
st.session_state.count = 0
(f"Click {st.session_state.count}")
if ("ADD"):
st.session_state.count += 1
countercount
put intost.session_state
Center.
st.session_state
Used to share variables across each session of a user, ensuring that they remain available on re-run.
4. Summary
This piece discusses theStreamlit
The architecture and operation mechanism of the program, as a whole, is simple, straightforward and well understood.
This was followed by a presentation ofStreamlit
hit the nail on the head(computing) cachecache
cap (a poem)state of affairssession
They have an important role in data processing, storage and persistence.
When using it, be careful to distinguish(computing) cachecache
cap (a poem)state of affairssession
of use scenarios.
(computing) cachecache
It is mainly used to improve the performance of the application by storing and reusing previously computed results to avoid repeated computations. It is concerned with data processing efficiency and resource consumption;
(indicates contrast)state of affairssession
It is mainly used to share variable and state information across a user's session. It is concerned with the persistence of user data and data consistency across event callbacks.