Location>code7788 >text

Architecture and operational mechanisms

Popularity:70 ℃/2024-10-22 11:34:30

This article focuses onStreamlitthe 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

StreamlitIt is more specific, it is for the userBS Architectureapp, and with the developer it's really more like aCS ArchitectureThe application.

Why?Streamlitmore likeCS ArchitectureAnd?

on account ofback endFunctions andforward part of sth.The UI part is all done withPythonWrite, so developStreamlitThe 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 developingStreamlitThe application will feel very intimate.

While it is ultimately in the browser that theStreamlitapplication, but developed with absolutely no front-end knowledge of HTML, CSS or JavaScript.

However.Streamlit AppAfter deployment, you need to pay attention to itBS Applicationsside of the story:

  1. Streamlit AppAfter the release is used by multiple users, the server configuration resources are considered based on the estimated number of users
  2. The user side uses the browser to use theStreamlit App, so there is no access to the server's files, directories, or operating system.
  3. If you need to communicate with any peripheral device (such as a camera), you must use theStreamlitcommands or custom components that will access these devices through the user's browser

2. Operational processes

StreamlitThe main process is simple and straightforward:

  1. The server side passes thestreamlit runcommand activation
  2. initializationAppweb page
  3. Client opens browser to access
  4. Users operate in the browser
  5. Servers process data based on user actions
  6. Update page after processing
  7. New page back to browser

Streamlit AppAfter each user action is received, the entire code is re-run and the re-rendered page is returned, the

This raises two obvious questions.

  1. 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.

  1. 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 += 1After 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 AppThe problem of not being able to save the data and state of the(computing) cachecache(math.) andstate of affairssession) 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_dataAdd a decorator on top of the function@st.cache_dataThat'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 affairssession) 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

countercountput intost.session_stateCenter.

st.session_stateUsed to share variables across each session of a user, ensuring that they remain available on re-run.

4. Summary

This piece discusses theStreamlitThe architecture and operation mechanism of the program, as a whole, is simple, straightforward and well understood.

This was followed by a presentation ofStreamlithit the nail on the head(computing) cachecachecap (a poem)state of affairssessionThey have an important role in data processing, storage and persistence.

When using it, be careful to distinguish(computing) cachecachecap (a poem)state of affairssessionof use scenarios.

(computing) cachecacheIt 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 affairssessionIt 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.