提示
本页面仅包含有关st.data_editor API的信息。要了解数据框的概述以及数据编辑器的功能和限制,请阅读数据框。
| 函数签名[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) | |
| 参数 | |
data (Anything supported by st.dataframe) | 要在数据编辑器中编辑的数据。 注意
|
width (int or None) | 数据编辑器的期望宽度,以像素表示。如果 width 是 None(默认值),Streamlit 会将数据编辑器的宽度设置为适应其内容,直到父容器的宽度。如果 width 大于父容器的宽度,Streamlit 会将数据编辑器的宽度设置为与父容器的宽度匹配。 |
height (int or None) | 数据编辑器的期望高度,以像素表示。如果height是None(默认值),Streamlit会将高度设置为最多显示十行。当高度无法容纳所有行时,数据编辑器元素内的垂直滚动将启用。 |
use_container_width (bool) | 是否使用父容器的宽度覆盖width。如果use_container_width为False(默认),Streamlit会根据width设置数据编辑器的宽度。如果use_container_width为True,Streamlit会将数据编辑器的宽度设置为与父容器的宽度相匹配。 |
hide_index (bool or None) | 是否隐藏索引列。如果 hide_index 是 None (默认值),索引列的可见性将根据数据自动确定。 |
column_order (Iterable of str or None) | 指定列的显示顺序。这也会影响哪些列是可见的。例如,column_order=("col2", "col1") 将首先显示 'col2',然后是 'col1',并隐藏所有其他非索引列。如果为 None(默认值),则顺序继承自原始数据结构。 |
column_config (dict or None) | 配置列的显示方式,例如它们的标题、可见性、类型或格式,以及编辑属性,如最小值/最大值或步长。 这需要是一个字典,其中每个键是列名,值是以下之一:
要配置索引列,请使用 _index 作为列名。 |
num_rows ("fixed" or "dynamic") | 指定用户是否可以在数据编辑器中添加和删除行。 如果为"fixed",用户不能添加或删除行。如果为"dynamic",用户可以在数据编辑器中添加和删除行,但列排序功能将被禁用。 默认为"fixed"。 |
disabled (bool 或 str 的 Iterable) | 控制列的编辑。如果为 True,则所有列的编辑都被禁用。 如果提供了列名的 Iterable(例如,disabled=("col1", "col2"))), 则只有指定的列会被禁用编辑。如果为 False(默认值), 所有支持编辑的列都是可编辑的。 |
key (str) | 一个可选的字符串,用作此小部件的唯一键。如果省略,将根据小部件的内容生成一个键。任何两个小部件都不能有相同的键。 |
on_change (callable) | 当此数据编辑器的值发生变化时调用的可选回调函数。 |
args (tuple) | 传递给回调函数的可选参数元组。 |
kwargs (dict) | 一个可选的kwargs字典,用于传递给回调函数。 |
| 返回 | |
(pandas.DataFrame, pandas.Series, pyarrow.Table, numpy.ndarray, list, set, tuple, or dict.) | 编辑后的数据。如果编辑后的数据对应于任何支持的返回类型,则以其原始数据类型返回。所有其他数据类型将作为pandas.DataFrame返回。 |
示例
import streamlit as st import pandas as pd 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设置为"dynamic"来允许用户添加和删除行:
import streamlit as st import pandas as pd 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, 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或disabled来自定义数据编辑器:
import pandas as pd import streamlit as st 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, 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}** 🎈")
Configuring columns
你可以通过Column configuration API配置st.dataframe和st.data_editor中列的显示和编辑行为。我们开发了这个API,让你可以在数据框和数据编辑器列中添加图片、图表和可点击的URL。此外,你可以使单个列可编辑,将列设置为分类并指定它们可以采用的选项,隐藏数据框的索引等等。
还有问题吗?
我们的 论坛 充满了有用的信息和Streamlit专家。