Python应用开发——30天学习Streamlit Python包进行APP的构建(12)

news2025/2/24 15:34:23

st.checkbox

显示复选框部件。

Function signature[source]

st.checkbox(label, value=False, key=None, help=None, on_change=None, args=None, kwargs=None, *, disabled=False, label_visibility="visible")

Returns

(bool)

Whether or not the checkbox is checked.

Parameters

label (str)

A short label explaining to the user what this checkbox is for. The label can optionally contain Markdown and supports the following elements: Bold, Italics, Strikethroughs, Inline Code, Emojis, and Links.

This also supports:

  • Emoji shortcodes, such as :+1: and :sunglasses:. For a list of all supported codes, see https://share.streamlit.io/streamlit/emoji-shortcodes.
  • LaTeX expressions, by wrapping them in "$" or "$$" (the "$$" must be on their own lines). Supported LaTeX functions are listed at Supported Functions · KaTeX.
  • Colored text and background colors for text, using the syntax :color[text to be colored] and :color-background[text to be colored], respectively. color must be replaced with any of the following supported colors: blue, green, orange, red, violet, gray/grey, rainbow. For example, you can use :orange[your text here] or :blue-background[your text here].

Unsupported elements are unwrapped so only their children (text contents) render. Display unsupported elements as literal characters by backslash-escaping them. E.g. 1\. Not an ordered list.

For accessibility reasons, you should never set an empty label (label="") but hide it with label_visibility if needed. In the future, we may disallow empty labels by raising an exception.

value (bool)

Preselect the checkbox when it first renders. This will be cast to bool internally.

key (str or int)

An optional string or integer to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key.

help (str)

An optional tooltip that gets displayed next to the checkbox.

on_change (callable)

An optional callback invoked when this checkbox's value changes.

args (tuple)

An optional tuple of args to pass to the callback.

kwargs (dict)

An optional dict of kwargs to pass to the callback.

disabled (bool)

An optional boolean, which disables the checkbox if set to True. The default is False.

label_visibility ("visible", "hidden", or "collapsed")

The visibility of the label. If "hidden", the label doesn't show but there is still empty space for it (equivalent to label=""). If "collapsed", both the label and the space are removed. Default is "visible".

代码

import streamlit as st

agree = st.checkbox("I agree")

if agree:
    st.write("Great!")

 这段代码使用了Streamlit库来创建一个简单的交互式应用程序。首先,它导入了Streamlit库,并创建了一个名为agree的复选框,用于让用户选择是否同意某个条件。然后,它使用if语句来检查用户是否勾选了agree复选框,如果用户勾选了该复选框,就会在应用程序中显示文本"Great!"。

st.color_picker 

显示颜色选择器部件。 

Function signature[source]

st.color_picker(label, value=None, key=None, help=None, on_change=None, args=None, kwargs=None, *, disabled=False, label_visibility="visible")

Returns

(str)

The selected color as a hex string.

Parameters

label (str)

A short label explaining to the user what this input is for. The label can optionally contain Markdown and supports the following elements: Bold, Italics, Strikethroughs, Inline Code, Emojis, and Links.

This also supports:

  • Emoji shortcodes, such as :+1: and :sunglasses:. For a list of all supported codes, see https://share.streamlit.io/streamlit/emoji-shortcodes.
  • LaTeX expressions, by wrapping them in "$" or "$$" (the "$$" must be on their own lines). Supported LaTeX functions are listed at Supported Functions · KaTeX.
  • Colored text and background colors for text, using the syntax :color[text to be colored] and :color-background[text to be colored], respectively. color must be replaced with any of the following supported colors: blue, green, orange, red, violet, gray/grey, rainbow. For example, you can use :orange[your text here] or :blue-background[your text here].

Unsupported elements are unwrapped so only their children (text contents) render. Display unsupported elements as literal characters by backslash-escaping them. E.g. 1\. Not an ordered list.

For accessibility reasons, you should never set an empty label (label="") but hide it with label_visibility if needed. In the future, we may disallow empty labels by raising an exception.

value (str)

The hex value of this widget when it first renders. If None, defaults to black.

key (str or int)

An optional string or integer to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key.

help (str)

An optional tooltip that gets displayed next to the color picker.

on_change (callable)

An optional callback invoked when this color_picker's value changes.

args (tuple)

An optional tuple of args to pass to the callback.

kwargs (dict)

An optional dict of kwargs to pass to the callback.

disabled (bool)

An optional boolean, which disables the color picker if set to True. The default is False. This argument can only be supplied by keyword.

label_visibility ("visible", "hidden", or "collapsed")

The visibility of the label. If "hidden", the label doesn't show but there is still empty space for it above the widget (equivalent to label=""). If "collapsed", both the label and the space are removed. Default is "visible".

代码

import streamlit as st

color = st.color_picker("Pick A Color", "#00f900")
st.write("The current color is", color)

 这段代码使用了Streamlit库来创建一个简单的应用程序。首先,它导入了Streamlit库并将其重命名为st。然后,它使用st.color_picker函数创建了一个颜色选择器,用户可以在应用程序中选择颜色。函数的第一个参数是一个文本字符串,用作颜色选择器的标签,第二个参数是一个默认颜色值。接下来,代码使用st.write函数将当前选择的颜色显示在应用程序中。

st.multiselect 

显示多选 widget。

多选窗口小部件一开始是空的。

Function signature[source]

st.multiselect(label, options, default=None, format_func=special_internal_function, key=None, help=None, on_change=None, args=None, kwargs=None, *, max_selections=None, placeholder="Choose an option", disabled=False, label_visibility="visible")

Returns

(list)

A list with the selected options

Parameters

label (str)

A short label explaining to the user what this select widget is for. The label can optionally contain Markdown and supports the following elements: Bold, Italics, Strikethroughs, Inline Code, Emojis, and Links.

This also supports:

  • Emoji shortcodes, such as :+1: and :sunglasses:. For a list of all supported codes, see https://share.streamlit.io/streamlit/emoji-shortcodes.
  • LaTeX expressions, by wrapping them in "$" or "$$" (the "$$" must be on their own lines). Supported LaTeX functions are listed at Supported Functions · KaTeX.
  • Colored text and background colors for text, using the syntax :color[text to be colored] and :color-background[text to be colored], respectively. color must be replaced with any of the following supported colors: blue, green, orange, red, violet, gray/grey, rainbow. For example, you can use :orange[your text here] or :blue-background[your text here].

Unsupported elements are unwrapped so only their children (text contents) render. Display unsupported elements as literal characters by backslash-escaping them. E.g. 1\. Not an ordered list.

For accessibility reasons, you should never set an empty label (label="") but hide it with label_visibility if needed. In the future, we may disallow empty labels by raising an exception.

options (Iterable)

Labels for the select options in an Iterable. For example, this can be a list, numpy.ndarray, pandas.Series, pandas.DataFrame, or pandas.Index. For pandas.DataFrame, the first column is used. Each label will be cast to str internally by default.

default (Iterable of V, V, or None)

List of default values. Can also be a single value.

format_func (function)

Function to modify the display of selectbox options. It receives the raw option as an argument and should output the label to be shown for that option. This has no impact on the return value of the multiselect.

key (str or int)

An optional string or integer to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key.

help (str)

An optional tooltip that gets displayed next to the multiselect.

on_change (callable)

An optional callback invoked when this multiselect's value changes.

args (tuple)

An optional tuple of args to pass to the callback.

kwargs (dict)

An optional dict of kwargs to pass to the callback.

max_selections (int)

The max selections that can be selected at a time.

placeholder (str)

A string to display when no options are selected. Defaults to 'Choose an option'.

disabled (bool)

An optional boolean, which disables the multiselect widget if set to True. The default is False. This argument can only be supplied by keyword.

label_visibility ("visible", "hidden", or "collapsed")

The visibility of the label. If "hidden", the label doesn't show but there is still empty space for it above the widget (equivalent to label=""). If "collapsed", both the label and the space are removed. Default is "visible".

代码

这段代码使用了Streamlit库来创建一个交互式应用程序。首先,使用`multiselect`函数创建了一个多选框,让用户从一个包含绿色、黄色、红色和蓝色的选项中选择自己喜欢的颜色。初始时,默认选中了黄色和红色两个选项。

接着,使用`write`函数将用户选择的颜色显示在应用程序中。当用户选择完颜色后,选中的颜色将会在屏幕上显示出来。

import streamlit as st

options = st.multiselect(
    "What are your favorite colors",
    ["Green", "Yellow", "Red", "Blue"],
    ["Yellow", "Red"])

st.write("You selected:", options)

st.radio 

显示单选按钮 widget。 

Function signature[source]

st.radio(label, options, index=0, format_func=special_internal_function, key=None, help=None, on_change=None, args=None, kwargs=None, *, disabled=False, horizontal=False, captions=None, label_visibility="visible")

Returns

(any)

The selected option or None if no option is selected.

Parameters

label (str)

A short label explaining to the user what this radio group is for. The label can optionally contain Markdown and supports the following elements: Bold, Italics, Strikethroughs, Inline Code, Emojis, and Links.

This also supports:

  • Emoji shortcodes, such as :+1: and :sunglasses:. For a list of all supported codes, see https://share.streamlit.io/streamlit/emoji-shortcodes.
  • LaTeX expressions, by wrapping them in "$" or "$$" (the "$$" must be on their own lines). Supported LaTeX functions are listed at Supported Functions · KaTeX.
  • Colored text and background colors for text, using the syntax :color[text to be colored] and :color-background[text to be colored], respectively. color must be replaced with any of the following supported colors: blue, green, orange, red, violet, gray/grey, rainbow. For example, you can use :orange[your text here] or :blue-background[your text here].

Unsupported elements are unwrapped so only their children (text contents) render. Display unsupported elements as literal characters by backslash-escaping them. E.g. 1\. Not an ordered list.

For accessibility reasons, you should never set an empty label (label="") but hide it with label_visibility if needed. In the future, we may disallow empty labels by raising an exception.

options (Iterable)

Labels for the select options in an Iterable. For example, this can be a list, numpy.ndarray, pandas.Series, pandas.DataFrame, or pandas.Index. For pandas.DataFrame, the first column is used.

Labels can include markdown as described in the label parameter and will be cast to str internally by default.

index (int or None)

The index of the preselected option on first render. If None, will initialize empty and return None until the user selects an option. Defaults to 0 (the first option).

format_func (function)

Function to modify the display of radio options. It receives the raw option as an argument and should output the label to be shown for that option. This has no impact on the return value of the radio.

key (str or int)

An optional string or integer to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key.

help (str)

An optional tooltip that gets displayed next to the radio.

on_change (callable)

An optional callback invoked when this radio's value changes.

args (tuple)

An optional tuple of args to pass to the callback.

kwargs (dict)

An optional dict of kwargs to pass to the callback.

disabled (bool)

An optional boolean, which disables the radio button if set to True. The default is False.

horizontal (bool)

An optional boolean, which orients the radio group horizontally. The default is false (vertical buttons).

captions (iterable of str or None)

A list of captions to show below each radio button. If None (default), no captions are shown.

label_visibility ("visible", "hidden", or "collapsed")

The visibility of the label. If "hidden", the label doesn't show but there is still empty space for it above the widget (equivalent to label=""). If "collapsed", both the label and the space are removed. Default is "visible".

代码

import streamlit as st

genre = st.radio(
    "What's your favorite movie genre",
    [":rainbow[Comedy]", "***Drama***", "Documentary :movie_camera:"],
    captions = ["Laugh out loud.", "Get the popcorn.", "Never stop learning."])

if genre == ":rainbow[Comedy]":
    st.write("You selected comedy.")
else:
    st.write("You didn't select comedy.")

这段代码使用了Streamlit库来创建一个交互式应用程序。首先,它导入了Streamlit库。然后,它使用st.radio函数创建了一个单选框,让用户选择他们喜欢的电影类型。单选框有三个选项,分别是喜剧、戏剧和纪录片。每个选项都有一个标签,用于描述这种类型电影的特点。

接下来,代码检查用户选择的电影类型,并根据选择的结果显示不同的消息。如果用户选择了喜剧,那么会显示"您选择了喜剧。",否则会显示"您没有选择喜剧。"。

要初始化一个空的无线电部件,请使用 "无 "作为索引值:

import streamlit as st

genre = st.radio(
    "What's your favorite movie genre",
    [":rainbow[Comedy]", "***Drama***", "Documentary :movie_camera:"],
    index=None,
)

st.write("You selected:", genre)

这段代码使用了Streamlit库来创建一个简单的交互式应用程序。首先,它导入了Streamlit库。然后,它使用`st.radio`函数创建了一个单选按钮,用于让用户选择他们最喜欢的电影类型。单选按钮的标签是"What's your favorite movie genre",选项包括":rainbow[Comedy]"、"***Drama***"和"Documentary :movie_camera:"。最后,它使用`st.write`函数将用户选择的电影类型显示在屏幕上。

部件可以使用 label_visibility 参数自定义隐藏标签的方式。如果参数为 "hidden"(隐藏),则标签不会显示,但在部件上方仍有空位(相当于 label="")。如果为 "collapsed"(折叠),标签和空格都会被移除。默认为 "可见"。单选按钮也可以使用 disabled 参数禁用,并使用 horizontal 参数水平定向:

import streamlit as st

# 在会话状态中存储部件的初始值
if "visibility" not in st.session_state:
    st.session_state.visibility = "visible"
    st.session_state.disabled = False
    st.session_state.horizontal = False

col1, col2 = st.columns(2)

with col1:
    st.checkbox("Disable radio widget", key="disabled")
    st.checkbox("Orient radio options horizontally", key="horizontal")

with col2:
    st.radio(
        "Set label visibility 👇",
        ["visible", "hidden", "collapsed"],
        key="visibility",
        label_visibility=st.session_state.visibility,
        disabled=st.session_state.disabled,
        horizontal=st.session_state.horizontal,
    )

st.selectbox

Function signature[source]

st.selectbox(label, options, index=0, format_func=special_internal_function, key=None, help=None, on_change=None, args=None, kwargs=None, *, placeholder="Choose an option", disabled=False, label_visibility="visible")

Returns

(any)

The selected option or None if no option is selected.

Parameters

label (str)

A short label explaining to the user what this select widget is for. The label can optionally contain Markdown and supports the following elements: Bold, Italics, Strikethroughs, Inline Code, Emojis, and Links.

This also supports:

  • Emoji shortcodes, such as :+1: and :sunglasses:. For a list of all supported codes, see https://share.streamlit.io/streamlit/emoji-shortcodes.
  • LaTeX expressions, by wrapping them in "$" or "$$" (the "$$" must be on their own lines). Supported LaTeX functions are listed at Supported Functions · KaTeX.
  • Colored text and background colors for text, using the syntax :color[text to be colored] and :color-background[text to be colored], respectively. color must be replaced with any of the following supported colors: blue, green, orange, red, violet, gray/grey, rainbow. For example, you can use :orange[your text here] or :blue-background[your text here].

Unsupported elements are unwrapped so only their children (text contents) render. Display unsupported elements as literal characters by backslash-escaping them. E.g. 1\. Not an ordered list.

For accessibility reasons, you should never set an empty label (label="") but hide it with label_visibility if needed. In the future, we may disallow empty labels by raising an exception.

options (Iterable)

Labels for the select options in an Iterable. For example, this can be a list, numpy.ndarray, pandas.Series, pandas.DataFrame, or pandas.Index. For pandas.DataFrame, the first column is used. Each label will be cast to str internally by default.

index (int)

The index of the preselected option on first render. If None, will initialize empty and return None until the user selects an option. Defaults to 0 (the first option).

format_func (function)

Function to modify the display of the labels. It receives the option as an argument and its output will be cast to str.

key (str or int)

An optional string or integer to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key.

help (str)

An optional tooltip that gets displayed next to the selectbox.

on_change (callable)

An optional callback invoked when this selectbox's value changes.

args (tuple)

An optional tuple of args to pass to the callback.

kwargs (dict)

An optional dict of kwargs to pass to the callback.

placeholder (str)

A string to display when no options are selected. Defaults to "Choose an option".

disabled (bool)

An optional boolean, which disables the selectbox if set to True. The default is False.

label_visibility ("visible", "hidden", or "collapsed")

The visibility of the label. If "hidden", the label doesn't show but there is still empty space for it above the widget (equivalent to label=""). If "collapsed", both the label and the space are removed. Default is "visible".

代码

import streamlit as st

option = st.selectbox(
    "How would you like to be contacted?",
    ("Email", "Home phone", "Mobile phone"))

st.write("You selected:", option)

要初始化一个空的选择框,请使用 "无 "作为索引值:

import streamlit as st

option = st.selectbox(
   "How would you like to be contacted?",
   ("Email", "Home phone", "Mobile phone"),
   index=None,
   placeholder="Select contact method...",
)

st.write("You selected:", option)

这段代码使用了Streamlit库来创建一个交互式应用程序。首先,它创建了一个下拉框(selectbox),让用户选择他们希望如何被联系。下拉框中有三个选项:"Email"、"Home phone"和"Mobile phone"。用户可以在这三个选项中选择一个作为他们的联系方式。如果用户没有进行选择,下拉框会显示占位符"Select contact method..."。

然后,代码使用st.write函数来显示用户选择的联系方式。例如,如果用户选择了"Email",那么st.write函数会显示"You selected: Email"。这里index =none代表默认不指定任何选项,这里会有一个提示,让你选择相应的选项。

选择部件可以使用 label_visibility 参数自定义隐藏标签的方式。如果参数为 "hidden"(隐藏),则标签不显示,但在 widget 上方仍有空位(相当于 label="")。如果为 "collapsed"(折叠),标签和空格都会被移除。默认为 "可见"。也可以使用 disabled 参数禁用选择部件:

import streamlit as st

# Store the initial value of widgets in session state
if "visibility" not in st.session_state:
    st.session_state.visibility = "visible"
    st.session_state.disabled = False

col1, col2 = st.columns(2)

with col1:
    st.checkbox("Disable selectbox widget", key="disabled")
    st.radio(
        "Set selectbox label visibility 👉",
        key="visibility",
        options=["visible", "hidden", "collapsed"],
    )

with col2:
    option = st.selectbox(
        "How would you like to be contacted?",
        ("Email", "Home phone", "Mobile phone"),
        label_visibility=st.session_state.visibility,
        disabled=st.session_state.disabled,
    )

st.select_slider 

显示滑块 widget,以便从列表中选择项目。

通过传递一个双元素元组或列表作为值,还可以显示一个范围滑块。

st.select_slider 和 st.slider 的区别在于,select_slider 可接受任何数据类型,并接受可迭代的选项集;而 st.slider 仅接受数字或日期/时间数据,并接受范围作为输入。

Function signature[source]

st.select_slider(label, options=(), value=None, format_func=special_internal_function, key=None, help=None, on_change=None, args=None, kwargs=None, *, disabled=False, label_visibility="visible")

Returns

(any value or tuple of any value)

The current value of the slider widget. The return type will match the data type of the value parameter.

Parameters

label (str)

A short label explaining to the user what this slider is for. The label can optionally contain Markdown and supports the following elements: Bold, Italics, Strikethroughs, Inline Code, Emojis, and Links.

This also supports:

  • Emoji shortcodes, such as :+1: and :sunglasses:. For a list of all supported codes, see https://share.streamlit.io/streamlit/emoji-shortcodes.
  • LaTeX expressions, by wrapping them in "$" or "$$" (the "$$" must be on their own lines). Supported LaTeX functions are listed at Supported Functions · KaTeX.
  • Colored text and background colors for text, using the syntax :color[text to be colored] and :color-background[text to be colored], respectively. color must be replaced with any of the following supported colors: blue, green, orange, red, violet, gray/grey, rainbow. For example, you can use :orange[your text here] or :blue-background[your text here].

Unsupported elements are unwrapped so only their children (text contents) render. Display unsupported elements as literal characters by backslash-escaping them. E.g. 1\. Not an ordered list.

For accessibility reasons, you should never set an empty label (label="") but hide it with label_visibility if needed. In the future, we may disallow empty labels by raising an exception.

options (Iterable)

Labels for the select options in an Iterable. For example, this can be a list, numpy.ndarray, pandas.Series, pandas.DataFrame, or pandas.Index. For pandas.DataFrame, the first column is used. Each label will be cast to str internally by default.

value (a supported type or a tuple/list of supported types or None)

The value of the slider when it first renders. If a tuple/list of two values is passed here, then a range slider with those lower and upper bounds is rendered. For example, if set to (1, 10) the slider will have a selectable range between 1 and 10. Defaults to first option.

format_func (function)

Function to modify the display of the labels from the options. argument. It receives the option as an argument and its output will be cast to str.

key (str or int)

An optional string or integer to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key.

help (str)

An optional tooltip that gets displayed next to the select slider.

on_change (callable)

An optional callback invoked when this select_slider's value changes.

args (tuple)

An optional tuple of args to pass to the callback.

kwargs (dict)

An optional dict of kwargs to pass to the callback.

disabled (bool)

An optional boolean, which disables the select slider if set to True. The default is False.

label_visibility ("visible", "hidden", or "collapsed")

The visibility of the label. If "hidden", the label doesn't show but there is still empty space for it above the widget (equivalent to label=""). If "collapsed", both the label and the space are removed. Default is "visible".

代码

import streamlit as st

color = st.select_slider(
    "Select a color of the rainbow",
    options=["red", "orange", "yellow", "green", "blue", "indigo", "violet"])
st.write("My favorite color is", color)

这段代码使用了Streamlit库来创建一个滑动条,让用户选择彩虹的颜色。用户可以从红、橙、黄、绿、蓝、靛、紫中选择一个颜色。然后代码会显示用户选择的颜色,并输出“我的最喜欢的颜色是”加上用户选择的颜色。下面是一个范围选择滑块的示例:

import streamlit as st

start_color, end_color = st.select_slider(
    "Select a range of color wavelength",
    options=["red", "orange", "yellow", "green", "blue", "indigo", "violet"],
    value=("red", "blue"))
st.write("You selected wavelengths between", start_color, "and", end_color)

这段代码使用了Streamlit库来创建一个交互式的滑块,让用户选择颜色的范围。用户可以通过拖动滑块来选择两个颜色之间的范围。代码中首先导入了Streamlit库,然后使用select_slider函数创建了一个滑块,让用户从红、橙、黄、绿、蓝、靛、紫这些选项中选择颜色的范围,默认选择了红色到蓝色的范围。最后,使用write函数将用户选择的颜色范围输出到界面上。

st.toggle

Function signature[source]

st.toggle(label, value=False, key=None, help=None, on_change=None, args=None, kwargs=None, *, disabled=False, label_visibility="visible")

Returns

(bool)

Whether or not the toggle is checked.

Parameters

label (str)

A short label explaining to the user what this toggle is for. The label can optionally contain Markdown and supports the following elements: Bold, Italics, Strikethroughs, Inline Code, Emojis, and Links.

This also supports:

  • Emoji shortcodes, such as :+1: and :sunglasses:. For a list of all supported codes, see https://share.streamlit.io/streamlit/emoji-shortcodes.
  • LaTeX expressions, by wrapping them in "$" or "$$" (the "$$" must be on their own lines). Supported LaTeX functions are listed at Supported Functions · KaTeX.
  • Colored text and background colors for text, using the syntax :color[text to be colored] and :color-background[text to be colored], respectively. color must be replaced with any of the following supported colors: blue, green, orange, red, violet, gray/grey, rainbow. For example, you can use :orange[your text here] or :blue-background[your text here].

Unsupported elements are unwrapped so only their children (text contents) render. Display unsupported elements as literal characters by backslash-escaping them. E.g. 1\. Not an ordered list.

For accessibility reasons, you should never set an empty label (label="") but hide it with label_visibility if needed. In the future, we may disallow empty labels by raising an exception.

value (bool)

Preselect the toggle when it first renders. This will be cast to bool internally.

key (str or int)

An optional string or integer to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key.

help (str)

An optional tooltip that gets displayed next to the toggle.

on_change (callable)

An optional callback invoked when this toggle's value changes.

args (tuple)

An optional tuple of args to pass to the callback.

kwargs (dict)

An optional dict of kwargs to pass to the callback.

disabled (bool)

An optional boolean, which disables the toggle if set to True. The default is False.

label_visibility ("visible", "hidden", or "collapsed")

The visibility of the label. If "hidden", the label doesn't show but there is still empty space for it (equivalent to label=""). If "collapsed", both the label and the space are removed. Default is "visible".

代码

import streamlit as st

on = st.toggle("Activate feature")

if on:
    st.write("Feature activated!")

这段代码使用了Streamlit库来创建一个简单的交互式应用程序。首先,它导入了Streamlit库并将其命名为st。然后,它创建了一个开关(toggle)组件,用于激活或关闭某个功能。用户可以通过单击开关来改变状态。接下来,代码使用if语句来检查开关的状态。如果开关被打开(on为True),则会显示一条消息“Feature activated!”。如果开关被关闭(on为False),则不会显示任何消息。 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1878537.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

认识100种电路之放大电路

在电子技术的广袤世界中,放大电路犹如一颗璀璨的明珠,发挥着至关重要的作用。那么,为什么电路需要放大?放大的原理又是什么?实现放大又需要用到哪些元器件以及数量如何呢?接着往下看,会解开你的…

leetCode.93. 复原 IP 地址

leetCode.93. 复原 IP 地址 题目思路&#xff1a; 代码 // 前导零的判断方法&#xff1a;如果第一个数是0&#xff0c;且第二个数还有数据&#xff0c;那就是前导0&#xff0c;要排除的 // 注意跟单个 0 区分开 class Solution { public:vector<string> res;vector<…

redis实战-缓存雪崩问题及解决方案

定义理解 缓存雪崩是指在同一时间段&#xff0c;大量缓存的key同时失效&#xff0c;或者Redis服务宕机&#xff0c;导致大量请求到达数据库&#xff0c;带来巨大压力 和缓存击穿的区别&#xff1a; 缓存雪崩是由于缓存中的大量数据同时失效或缓存服务器故障引起的&#xff1b…

ESP32-C3模组上跑通MQTT(6)—— tcp例程(1)

接前一篇文章:ESP32-C3模组上跑通MQTT(5) 《ESP32-C3 物联网工程开发实战》 一分钟了解MQTT协议 ESP32 MQTT API指南-CSDN博客 ESP-IDF MQTT 示例入门_mqtt outbox-CSDN博客 ESP32用自签CA进行MQTT的TLS双向认证通信_esp32 mqtt ssl-CSDN博客 特此致谢! 本回开始正式讲…

python进阶函数

目录 函数多返回值函数多种传参方式匿名函数 函数多返回值 问&#xff1a;如果一个函数如些两个return&#xff08;如下所示&#xff09;&#xff0c;程序如何执行&#xff1f; def return_num():return 1return 2result return_num() print(result)答&#xff1a;只执行了第…

玩游戏就能学习亚马逊云科技AWS技术并通过热门技术认证考试??

亚马逊AWS限时活动&#xff0c;玩免费游戏Cloud Quest Practitioner送AWS云从业证书考试25%折扣券(价值171元)&#xff0c;玩游戏的同时还能学知识一举两得。Cloud Quest是AWS出的一款3D角色扮演游戏/虚拟城市建造形式的实验课程(游戏画面有点像天际线)&#xff0c;大家通过完成…

uniapp启动页面鉴权页面闪烁问题

在使用uni-app开发app 打包完成后如果没有token&#xff0c;那么就在onLaunch生命周期里面判断用户是否登录并跳转至登录页。 但是在app中页面会先进入首页然后再跳转至登录页&#xff0c;十分影响体验。 处理方法&#xff1a; 使用plus.navigator.closeSplashscreen() 官网…

《HelloGitHub》第 99 期

兴趣是最好的老师&#xff0c;HelloGitHub 让你对编程感兴趣&#xff01; 简介 HelloGitHub 分享 GitHub 上有趣、入门级的开源项目。 github.com/521xueweihan/HelloGitHub 这里有实战项目、入门教程、黑科技、开源书籍、大厂开源项目等&#xff0c;涵盖多种编程语言 Python、…

网络基础:路由路由协议

路由是指在计算机网络中选择路径来传输数据包的过程和机制&#xff1b;它包括路径选择、数据包转发、以及维持网络连接所需的各种协议和算法&#xff0c;路由的目标是确保数据包能够高效且可靠地从源设备传输到目标设备&#xff1b;常见的能够实现路由功能网络设备有&#xff1…

期末重现题型--错题集

看书里的定义&#xff1a;链表是一种常见而重要的动态存储分布的数据结构。它由若干个同一结构类型的“结点”依次串联而成的。

【理解】关于正点原子i.MX6ULL LCD计算式的理解

文章目录 1 描述2 疑问3 理解 1 描述 在《【正点原子】I.MX6U嵌入式Linux驱动开发指南V1.81.pdf》&#xff0c;P560页&#xff0c;第二十四章 RGBLCD显示实验中提到&#xff0c;LCD屏幕显示一行所需要的时间&#xff1a; t H S P W H B P H O Z V A L H F P ① t HSPW …

Leetcode - 133双周赛

目录 一&#xff0c;3190. 使所有元素都可以被 3 整除的最少操作数 二&#xff0c;3191. 使二进制数组全部等于 1 的最少操作次数 I 三&#xff0c;3192. 使二进制数组全部等于 1 的最少操作次数 II 四&#xff0c;3193. 统计逆序对的数目 一&#xff0c;3190. 使所有元素都…

【Python游戏】猫和老鼠

本文收录于 《一起学Python趣味编程》专栏,从零基础开始,分享一些Python编程知识,欢迎关注,谢谢! 文章目录 一、前言二、代码示例三、知识点梳理四、总结一、前言 本文介绍如何使用Python的海龟画图工具turtle,开发猫和老鼠游戏。 什么是Python? Python是由荷兰人吉多范…

【List集合排序】

List集合排序Demo import com.google.common.collect.Lists; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor;import java.util.*;/*** list order demo*/ public class ListOrderDemo {public static void main(String[] args) {List<String> lis…

YOLOv5改进 | 注意力机制 | 迈向高质量像素级回归的极化自注意力【全网独家】

秋招面试专栏推荐 &#xff1a;深度学习算法工程师面试问题总结【百面算法工程师】——点击即可跳转 &#x1f4a1;&#x1f4a1;&#x1f4a1;本专栏所有程序均经过测试&#xff0c;可成功执行&#x1f4a1;&#x1f4a1;&#x1f4a1; 专栏目录&#xff1a; 《YOLOv5入门 …

rocketmq简易版搭建

今天真是搭了本人六七个钟&#xff0c;太难了 首先是魔法大战镜像&#xff0c;这波大败而归&#xff0c;连上了&#xff0c;可惜没氪金&#xff0c;永远是没拉完就超时&#xff0c;魔法质量不行&#xff0c;等上班赚点米再改良一下魔法类别&#xff0c;那还得继续linux搭建 1…

电脑录屏有水印怎么办 电脑录屏出来为什么画质模糊 camtasia属于什么软件

Camtasia&#xff08;又叫喀秋莎&#xff09;&#xff0c;作为专业录屏软件&#xff0c;拥有3000多万专业人士在全球范围内使用&#xff0c;多种使用场景&#xff0c;支持更多文件格式&#xff0c;多功能特效剪辑&#xff0c;为您带来独一无二的录屏新体验&#xff01; 大家在平…

LLaMA-Factory安装

安装代码 https://github.com/echonoshy/cgft-llm/blob/master/llama-factory/README.md https://github.com/hiyouga/LLaMA-Factory/tree/mainLLaMA-Factoryhttps://github.com/hiyouga/LLaMA-Factory/tree/main 【大模型微调】- 使用Llama Factory实现中文llama3微调_哔哩…

uniapp部署服务器,uniapp打包H5部署服务器,uniapp将config.js抽离

目录 步骤一.在static文件夹下新建config.js文件 config.js文件说明 在config.js中放入使用的请求的接口地址,资源路径等 congfig.js中的变量在页面中如何使用 步骤二.manifest.json配置 1.在项目根目录(与app.vue同级)创建template.h5.html文件 2.在manifest.json配置刚刚创…

Unity扩展编辑器功能的特性

1.添加分组标题 用于在Unity的Inspector视图中为属性或变量组创建一个自定义的标题或头部&#xff0c;有助于在Inspector中组织和分类不同的属性&#xff0c;使其更易于阅读和管理。 [Header("Common Properties")] public float MouseSensitivity 5; public float…