Location>code7788 >text

『Play Streamlit』--Interactive Components

Popularity:956 ℃/2024-11-19 08:42:36

Interaction Class ComponentsexistWebcritical in applications, they allow users to interact with the application in real time and can significantly enhance the user experience.

Instead of just passively receiving information, users can actively enter data, make choices or trigger events to engage more deeply with the application.

In addition, for certain complex tasks or operations, theInteraction Class ComponentsIt can be broken down into a series of simple steps or choices, thus reducing the cognitive burden and learning costs for the user

. This allows users to accomplish these tasks more easily and improves the ease of use of the application.

StreamlitThere are a lot of interaction class components in this article, this article introduces the most commonly used ones, which are theWebA few of the most commonly seen components on a page.

  • st.text_input: Allows the user to enter text and is used to collect information about the strings entered by the user.
  • : Provides a clickable button that the user clicks to trigger a specific action or event.
  • : Displays a drop-down list that allows the user to select one of the preset options.
  • : Provides a drop-down multi-select function that allows the user to select more than one of the preset options.
  • : Displays a radio button group that allows the user to select one of several options.
  • : Provide checkboxes to allow the user to select or deselect specific options.

1. Overview of components

1.1. st.text_input

Used to enter plain text or a password, similar to the HTML<input type="text">

The parameters of the core are:

name (of a thing) typology clarification
label str Label in front of the input box
key str The key that uniquely identifies this input box and can be used to reference it in callbacks
value str Initial value of the input box
type str Input type, either "default" or "password".

1.2.

Provides a button to be used to set off a specific event, similar to the HTML<button>

The parameters of the core are:

name (of a thing) typology clarification
label str Text on buttons
key str The key that uniquely identifies this button
help str Help text next to the button

1.3.

drop-down selection box, similar to the HTML<select>

The parameters of the core are:

name (of a thing) typology clarification
label str The label in front of the dropdown box
key str The key that uniquely identifies this drop-down box
options list List of options in the drop-down box
index int Index of the initial selection

1.4.

A drop-down checkbox that can be multi-selected, similar to an HTML<select multiple>

The parameters of the core are:

name (of a thing) typology clarification
label str Label in front of the multi-select box
key str The key that uniquely identifies this checkbox
options list List of options in the multi-select box
default list List of options selected by default

1.5.

radio button group, similar to the HTML<input type="radio">

The parameters of the core are:

name (of a thing) typology clarification
label str Labels in front of radio button groups
key str The key that uniquely identifies this radio button group
options list List of options in the radio button group
index int Index of the initial selection

1.6.

checkbox, which is similar to the HTML<input type="checkbox">

The parameters of the core are:

name (of a thing) typology clarification
label str Label next to the checkbox
key str The key that uniquely identifies this checkbox
value bool Initial state of the checkbox (checked or unchecked)

2. Examples of component use

Here's a simplified example from a real-world scenario to see how to use theStreamlitThe interaction class component of the

2.1 Example of a "user preference survey"

This example simulates a real user survey scenario that

pass (a bill or inspection etc)Streamlitinteractive component that allows users to easily enter and select information and submit it to the application for processing and display.

import streamlit as st

# Title
("User Preference Survey")

# text_input_box: collect username
username = st.text_input("Please enter your name:")

# drop-down radio box: select gender
gender = ("Please select your gender:", ["Male", "Female", "Other"])

# Drop down multiple choice box: select hobbies
hobbies = (
    "Please select your hobbies:", ["Reading", "Sports", "Traveling", "Music", "Movies"])
)

# radio button group: select favorite color
favorite_color = ("Please select your favorite color: ", ["red", "blue", "green", "yellow"])

# Checkbox: whether to accept to receive pushes or not
accept_push = ("Do you agree to receive push messages?")

# Button: submit survey
if ("Submit Survey").
    # Collect all input and display it
    user_info = {
        "name": username,
        "Gender": gender, "Interests": "", "Hobbies": "", "Hobbies": "".
        "hobbies": ", ".join(hobbies),
        "Favorite_color": favorite_color,
        "Agree to receive pushes": "Yes" if accept_push else "No",
    }

    ("Your survey information is as follows:")
    (user_info)

2.2 Example of a "Data Analysis Project Dashboard"

This example simulates a dashboard for a data analysis project.

pass (a bill or inspection etc)Streamlits interactive components, users can easily interact with the data, select the analysis they are interested in, and view and download the results.

import streamlit as st
import pandas as pd
import numpy as np

# Assume a data set
data = (
    {
        "Date": pd.date_range(start="2023-01-01", periods=100, freq="D"),
        "category": (["A", "B", "C"], 100),
        "Sales": (100, 1000, 100),
        "Profit": (10, 100, 100),
    }
)

# Title
("Data Analytics Project Dashboard")

# Text input box: enter project name
project_name = st.text_input("Please enter a project name:")

# drop-down radio box: select the analysis category
analysis_category = ("Please select analysis category: ", data["category"].unique())

# Drop-down checkbox: select the columns to display
display_columns = ("Please select the columns to display:", )
selected_data = data[display_columns]

# Radio button group: select the summarization method
agg = ("Please select the summarization method: ", ["sum", "average", "max", "min"])
agg_dict = {
    "Sum": "sum",.
    "mean": "mean",
    "max": "max",
    "min": "min",
}

# Checkbox: whether to summarize by category
group_by_category = ("Is it summarized by category?")

# Button: perform analysis
if ("Perform analysis").
    # Perform analysis based on user selection
    if group_by_category.
        grouped_data = (
            selected_data.groupby("category")
            .agg({col: agg_dict[agg] for col in selected_data.columns if col ! = "Category"})
            .reset_index()
        )
    else.
        grouped_data = (
            selected_data.agg({col: agg_dict[agg] for col in selected_data.columns}))
            .to_frame()
            .T
        )

    # Display the results of the analysis
    ("Analysis results:")
    (grouped_data)

3. Summary

In short, these interactive components enable users to interact with the application through text input, selection, ticking, etc., so that data can be dynamically displayed and analyzed according to user needs.

They greatly enhance application flexibility and user experience, making tasks such as data analysis and data visualization more intuitive and convenient.