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

st.data_editor

显示数据编辑器 widget。

数据编辑器 widget 可让你在类似表格的用户界面中编辑数据框和许多其他数据结构。

警告

When going from st.experimental_data_editor to st.data_editor in 1.23.0, the data editor's representation in st.session_state was changed. The edited_cells dictionary is now called edited_rows and uses a different format ({0: {"column name": "edited value"}} instead of {"0:1": "edited value"}). You may need to adjust the code if your app uses st.experimental_data_editor in combination with st.session_state.

函数

Function signature[source]

st.data_editor(data, *, width=None, height=None, use_container_width=False, hide_index=None, column_order=None, column_config=None, num_rows="fixed", disabled=False, key=None, on_change=None, args=None, kwargs=None)

Returns

(pandas.DataFrame, pandas.Series, pyarrow.Table, numpy.ndarray, list, set, tuple, or dict.)

The edited data. The edited data is returned in its original data type if it corresponds to any of the supported return types. All other data types are returned as a pandas.DataFrame.

Parameters

data (pandas.DataFrame, pandas.Series, pandas.Styler, pandas.Index, pyarrow.Table, numpy.ndarray, pyspark.sql.DataFrame, snowflake.snowpark.DataFrame, list, set, tuple, dict, or None)

The data to edit in the data editor.

Note

  • Styles from pandas.Styler will only be applied to non-editable columns.
  • Mixing data types within a column can make the column uneditable.
  • Additionally, the following data types are not yet supported for editing: complex, list, tuple, bytes, bytearray, memoryview, dict, set, frozenset, fractions.Fraction, pandas.Interval, and pandas.Period.
  • To prevent overflow in JavaScript, columns containing datetime.timedelta and pandas.Timedelta values will default to uneditable but this can be changed through column configuration.

width (int or None)

Desired width of the data editor expressed in pixels. If width is None (default), Streamlit sets the data editor width to fit its contents up to the width of the parent container. If width is greater than the width of the parent container, Streamlit sets the data editor width to match the width of the parent container.

height (int or None)

Desired height of the data editor expressed in pixels. If height is None (default), Streamlit sets the height to show at most ten rows. Vertical scrolling within the data editor element is enabled when the height does not accomodate all rows.

use_container_width (bool)

Whether to override width with the width of the parent container. If use_container_width is False (default), Streamlit sets the data editor's width according to width. If use_container_width is True, Streamlit sets the width of the data editor to match the width of the parent container.

hide_index (bool or None)

Whether to hide the index column(s). If hide_index is None (default), the visibility of index columns is automatically determined based on the data.

column_order (Iterable of str or None)

Specifies the display order of columns. This also affects which columns are visible. For example, column_order=("col2", "col1") will display 'col2' first, followed by 'col1', and will hide all other non-index columns. If None (default), the order is inherited from the original data structure.

column_config (dict or None)

Configures how columns are displayed, e.g. their title, visibility, type, or format, as well as editing properties such as min/max value or step. This needs to be a dictionary where each key is a column name and the value is one of:

  • None to hide the column.
  • A string to set the display label of the column.
  • One of the column types defined under st.column_config, e.g. st.column_config.NumberColumn("Dollar values”, format=”$ %d") to show a column as dollar amounts. See more info on the available column types and config options here.

To configure the index column(s), use _index as the column name.

num_rows ("fixed" or "dynamic")

Specifies if the user can add and delete rows in the data editor. If "fixed", the user cannot add or delete rows. If "dynamic", the user can add and delete rows in the data editor, but column sorting is disabled. Defaults to "fixed".

disabled (bool or Iterable of str)

Controls the editing of columns. If True, editing is disabled for all columns. If an Iterable of column names is provided (e.g., disabled=("col1", "col2")), only the specified columns will be disabled for editing. If False (default), all columns that support editing are editable.

key (str)

An optional string to use as the unique key for this 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.

on_change (callable)

An optional callback invoked when this data_editor'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.

代码

这段代码使用了Streamlit和Pandas库。首先,创建了一个包含命令、评分和是否为小部件的数据框df。然后,使用st.data_editor()函数创建了一个交互式的数据编辑器,用户可以在编辑器中修改数据框。接着,代码找到评分最高的命令,并将其显示在屏幕上。

import streamlit as st
import pandas as pd#数据中有4个参数,给到了三个不同的控件,并设定了期是否开启了空间
df = pd.DataFrame([{"command": "st.selectbox", "rating": 4, "is_widget": True},{"command": "st.balloons", "rating": 5, "is_widget": False},{"command": "st.time_input", "rating": 3, "is_widget": True},]
)
#将定义的数据格式传入编辑器
edited_df = st.data_editor(df)#这个程序可以实现当你选择不同的控件,回给出不同的结果
favorite_command = edited_df.loc[edited_df["rating"].idxmax()]["command"]
st.markdown(f"Your favorite command is **{favorite_command}** 🎈")

通过将 num_rows 设置为 "动态",还可以允许用户添加和删除行: 

这段代码使用了Streamlit和Pandas库。首先,它创建了一个包含命令、评分和是否为小部件的数据框df。然后,使用st.data_editor函数创建了一个交互式数据编辑器,让用户可以编辑数据框。接着,代码找到评分最高的行,并提取该行的命令,最后使用st.markdown函数将用户最喜欢的命令显示在页面上。

import streamlit as st
import pandas as pddf = pd.DataFrame([{"command": "st.selectbox", "rating": 4, "is_widget": True},{"command": "st.balloons", "rating": 5, "is_widget": False},{"command": "st.time_input", "rating": 3, "is_widget": True},]
)
edited_df = st.data_editor(df, num_rows="dynamic")favorite_command = edited_df.loc[edited_df["rating"].idxmax()]["command"]
st.markdown(f"Your favorite command is **{favorite_command}** 🎈")

同样也可以用这 column_config, hide_index, column_order, or disabled: 

这段代码使用了Pandas和Streamlit库。首先,它创建了一个包含命令、评分和是否为小部件的数据框(DataFrame)。然后,使用Streamlit的data_editor函数编辑了数据框,对列进行了配置,设置了列的格式和范围,并禁用了某些列并隐藏了索引。接着,代码找到评分最高的命令,并输出了用户最喜爱的命令。

import pandas as pd
import streamlit as stdf = pd.DataFrame([{"command": "st.selectbox", "rating": 4, "is_widget": True},{"command": "st.balloons", "rating": 5, "is_widget": False},{"command": "st.time_input", "rating": 3, "is_widget": True},]
)
edited_df = st.data_editor(df,column_config={"command": "Streamlit Command","rating": st.column_config.NumberColumn("Your rating",help="How much do you like this command (1-5)?",min_value=1,max_value=5,step=1,format="%d ⭐",),"is_widget": "Widget ?",},disabled=["command", "is_widget"],hide_index=True,
)favorite_command = edited_df.loc[edited_df["rating"].idxmax()]["command"]
st.markdown(f"Your favorite command is **{favorite_command}** 🎈")

配置列

您可以通过列配置 API 配置 st.dataframe 和 st.data_editor 中列的显示和编辑行为。我们开发的 API 可让您在数据框架和数据编辑器列中添加图片、图表和可点击的 URL。此外,您还可以编辑单个列、将列设置为分类列并指定它们可以使用的选项、隐藏数据框的索引等。 

Column configuration

在 Streamlit 中处理数据时,st.column_config 类是配置数据显示和交互的强大工具。该类专为 st.dataframe 和 st.data_editor 中的 column_config 参数而设计,提供了一整套方法,可根据各种数据类型(从简单的文本和数字到列表、URL、图像等)定制列。

无论是将时间数据转换为用户友好的格式,还是利用图表和进度条实现更清晰的数据可视化,列配置不仅能为用户提供丰富的数据查看体验,还能确保您拥有各种工具,以您想要的方式展示数据并与之交互。

st.column_config.Column

在 st.dataframe 或 st.data_editor 中配置通用列。

列的类型将根据数据类型自动推断。该命令需要在 st.dataframe 或 st.data_editor 的 column_config 参数中使用。

要更改列的类型并启用特定类型的配置选项,请使用 st.column_config 命名空间中的一种列类型,例如 st.column_config.NumberColumn。

Function signature[source]

st.column_config.Column(label=None, *, width=None, help=None, disabled=None, required=None)

Parameters

label (str or None)

The label shown at the top of the column. If None (default), the column name is used.

width ("small", "medium", "large", or None)

The display width of the column. Can be one of "small", "medium", or "large". If None (default), the column will be sized to fit the cell contents.

help (str or None)

An optional tooltip that gets displayed when hovering over the column label.

disabled (bool or None)

Whether editing should be disabled for this column. Defaults to False.

required (bool or None)

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

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

相关文章

2352.相等行列对

给你一个下标从 0 开始、大小为 n x n 的整数矩阵 grid ,返回满足 Ri 行和 Cj 列相等的行列对 (Ri, Cj) 的数目。 如果行和列以相同的顺序包含相同的元素(即相等的数组),则认为二者是相等的。 示例 1: 输入&#xff1a…

Java——包

一、包 1、简要介绍 在Java编程语言中,包(Package) 是一种用来组织和管理类(Class)和接口(Interface)的机制。包为开发者提供了一种逻辑分组的方式,使代码更加模块化、结构化和易于…

【前端技术】标签页通讯localStorage、BroadcastChannel、SharedWorker的技术详解

😄 19年之后由于某些原因断更了三年,23年重新扬帆起航,推出更多优质博文,希望大家多多支持~ 🌷 古之立大事者,不惟有超世之才,亦必有坚忍不拔之志 🎐 个人CSND主页——Mi…

Redis大key有什么危害?如何排查和处理?

什么是 bigkey? 简单来说,如果一个 key 对应的 value 所占用的内存比较大,那这个 key 就可以看作是 bigkey。具体多大才算大呢?有一个不是特别精确的参考标准: String 类型的 value 超过 1MB 复合类型(Li…

一个漂亮的网站收藏函数

<!DOCTYPE html> <html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>网站收藏</title><style>body …

云手机群控功能讲解

接触云手机之前&#xff0c;很多企业或者个人卖家都对群控有浓厚的兴趣&#xff0c;云手机群控具体是什么呢&#xff1f;云手机群控&#xff0c;顾名思义&#xff0c;是指能够同时对多台云手机进行集中控制和管理的功能。打破了传统单台手机操作的限制&#xff0c;实现了规模化…

高精度乘法的实现

这是C算法基础-基础算法专栏的第九篇文章&#xff0c;专栏详情请见此处。 引入 上次我们学习了高精度加法的实现&#xff0c;这次我们要学习高精度减法的实现。 高精度乘法与高精度加法的定义、前置过程都是大致相同的&#xff0c;如果想了解具体内容&#xff0c;可以移步至我的…

查看LabVIEW及各个模块和驱动的版本号

要方便地查看当前计算机上安装的LabVIEW版本以及各个模块和驱动的版本号&#xff0c;可以使用以下几种方法&#xff1a; 1. 使用NI MAX (Measurement & Automation Explorer) NI MAX 是一个强大的工具&#xff0c;可以帮助你管理National Instruments硬件、软件和驱动程序…

Docker(三)-Docker常用命令

1.run run命令执行流程:2.帮助启动类命令 2.1 启动docker systemctl start docker2.2 停止docker systemctl stop docker2.3 重启docker systemctl restart docker2.4查看docker状态 systemctl status docker2.5开机启动 systemctl enable docker2.6查看docker概要信息 …

VB.net实战(VSTO):VSTOwpf体验框架打包教程

如果是考虑到Wps用户较多&#xff0c;就不建议采用侧边栏的形式 只是个体验框架&#xff0c;界面未作美化&#xff0c;office的用户可以用任意一种窗体&#xff0c;喜欢那个界面就写那个界面&#xff0c;wps的侧边栏只能弹出一部分&#xff0c;每次需要的手动拖动。 打包了案例…

Java——IO流(一)-(6/8):字节流-FileInputStream 每次读取多个字节(示例演示)、一次读取完全部字节(方式一、方式二,注意事项)

目录 文件字节输入流&#xff1a;每次读取多个字节 实例演示 注意事项 文件字节输入流&#xff1a;一次读取完全部字节 方式一 方式二 注意事项 文件字节输入流&#xff1a;每次读取多个字节 用到之前介绍过的常用方法&#xff1a; 实例演示 需求&#xff1a;用每次读取…

诸茅的黄昏

内容提要 白酒大陆的坍塌终于到达茅台的地盘&#xff0c;一切发生得太快了。突然间&#xff0c;深厚的护城河消失了&#xff0c;医药茅、眼科茅、牙科茅、疫苗茅、酱油茅都挣扎于内需的泥沼中。旧茅衰退&#xff0c;新茅生长&#xff0c;在下行周期&#xff0c;内需仍有结构性…

C#修改 EXE 文件图标和 winForm 窗口图标

修改 EXE 文件图标 1.准备好图片&#xff0c;转换为 Icon 图片&#xff1b; 2.右键工程&#xff0c;选择属性&#xff1b; 3.选择 Icon 图标即可&#xff1b; 4.重新生成可执行文件&#xff0c;查看。 修改 winForm 窗口图标 1.选中 winForm &#xff0c;查看属性&#x…

「51媒体」时尚类媒体邀约宣发资源

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 时尚类媒体邀约宣发资源可以多样化且针对性地满足品牌或活动的推广需求。以下是一些主要的资源及其特点&#xff1a; 时尚杂志&#xff1a;国内外知名时尚杂志&#xff0c;如《Vogue》、…

手机怎么自动切换ip地址

在数字化时代&#xff0c;网络IP地址不仅是设备在网络世界的标识&#xff0c;也是确保用户网络安全和数据隐私的关键因素。对于手机用户来说&#xff0c;在某些情境下可能需要自动切换IP地址&#xff0c;本文将为您介绍手机怎么自动切换IP地址。 随着网络技术的发展&#xff0c…

一些使用注意(XPTable控件使用说明十)

当XPTABLE放到线程中&#xff0c;列数据很多&#xff0c;不出现滚动条的解决代码&#xff1a; /// 这里神奇的代码&#xff0c;解决线程中XPTABLE 不出滚动条问题 , 执行UI相关的操作this.Invoke(new Action(() >{ // 列头&#xff0c;一行空的&#xff0c;这里列头设置…

蓝桥杯 经典算法题 求解完全背包问题

题目&#xff1a; 题解&#xff1a; 和01背包基本完全一样。小局部最优的策略也是一样&#xff1a;是否选当前局部的最后一项。唯一的不同点在于物品是无线的导致在表示选择当前物品的状态写法发生了改变&#xff1a;由dp[i-1][j-w[i]]变为了dp[i][j-w[i]]因为这样能够表示最后…

读AI新生:破解人机共存密码笔记08超级智能

1. 发现动作 1.1. 时间跨度长的智能行为&#xff0c;需要具备在多个抽象层次上分层规划和管理活动的能力&#xff0c;从攻读博士学位&#xff08;可能涉及1万亿个动作&#xff09;&#xff0c;到给一根手指发送一个运动控制指令&#xff0c;从而键入求职信的字符&#xff0c;无…

用户态协议栈04-定时arp-table的实现

之前有写过arp reply的实现&#xff0c;其中有写道&#xff0c;我们的系统内核中会维护一张ARP表&#xff0c;可以通过终端arp -a查看&#xff1a; 其中的dynamic和static是动态arp的类型&#xff0c;之前的udp实验就是添加了一条静态arp达到了发送的目的。在我们需要发送一个数…

AI播客下载:Machine Learning Street Talk(AI机器学习)

该频道由 Tim Scarfe 博士、Yannic Kilcher 博士和 Keith Duggar 博士管理。 他们做了出色的工作&#xff0c;对每个节目进行了彻底的研究&#xff0c;并与机器学习行业中一些受过最高教育、最全面的嘉宾进行了双向对话。 每一集都会教授一些新内容&#xff0c;并且提供未经过滤…