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

发布于:2024-06-30 ⋅ 阅读:(18) ⋅ 点赞:(0)

st.button

显示按钮部件。

Function signature[source]

st.button(label, key=None, help=None, on_click=None, args=None, kwargs=None, *, type="secondary", disabled=False, use_container_width=False)

Returns

(bool)

True if the button was clicked on the last run of the app, False otherwise.

Parameters

label (str)

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

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.

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 when the button is hovered over.

on_click (callable)

An optional callback invoked when this button is clicked.

args (tuple)

An optional tuple of args to pass to the callback.

kwargs (dict)

An optional dict of kwargs to pass to the callback.

type ("secondary" or "primary")

An optional string that specifies the button type. Can be "primary" for a button with additional emphasis or "secondary" for a normal button. Defaults to "secondary".

disabled (bool)

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

use_container_width (bool)

Whether to expand the button's width to fill its parent container. If use_container_width is False (default), Streamlit sizes the button to fit its contents. If use_container_width is True, the width of the button matches its parent container.

In both cases, if the contents of the button are wider than the parent container, the contents will line wrap.

代码

这里我们显示了一个按钮重启,类型是首要的,这里有两个按钮分别是if和else这里进行选择 

import streamlit as st

st.button("Reset", type="primary")
if st.button("Say hello"):
    st.write("Why hello there")
else:
    st.write("Goodbye")

这段代码使用了Streamlit库来创建一个简单的交互式应用程序界面。首先,导入了Streamlit库,并使用st.button()函数创建了两个按钮。第一个按钮显示文本"Reset",并且指定了按钮类型为"primary"。第二个按钮显示文本"Say hello"。

接下来,代码使用if语句来检查第二个按钮是否被点击。如果第二个按钮被点击,则输出"Why hello there";否则,输出"Goodbye"。

总的来说,这段代码创建了一个简单的界面,其中包含两个按钮,并根据用户的点击行为输出不同的文本。

高级功能
虽然按钮是最简单的输入部件,但按钮与 st.session_state 的使用深度绑定是很常见的。请查看我们的按钮行为高级指南和示例。 

st.download_button

显示下载按钮小部件。

如果您想为用户提供一种直接从应用程序下载文件的方式,这个小工具就非常有用。

请注意,下载的数据会在用户连接时存储在内存中,因此为了节省内存,最好将文件大小控制在几百兆以内。

如果要防止应用程序在用户点击下载按钮时重新运行,可将下载按钮封装在一个片段中。

Function signature[source]

st.download_button(label, data, file_name=None, mime=None, key=None, help=None, on_click=None, args=None, kwargs=None, *, type="secondary", disabled=False, use_container_width=False)

Returns

(bool)

True if the button was clicked on the last run of the app, False otherwise.

Parameters

label (str)

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

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[tex

网站公告

今日签到

点亮在社区的每一天
去签到