(vii) 解析 Streamlit 的数据元素:探索 st.dataframe、st.data_editor、st.column_config、st.table、st.metric 和 st.json 的奥妙(上) - 4.2 使用 st.column_config.TextColumn 来定制数据编辑器的文本列
最编程
2024-03-20 11:15:29
...
import pandas as pd import streamlit as st data_df = pd.DataFrame( { "widgets": ["st.selectbox", "st.number_input", "st.text_area", "st.button"], } ) st.data_editor( data_df, column_config={ "widgets": st.column_config.TextColumn( "Widgets", help="Streamlit **widget** commands ????", default="st.", max_chars=50, validate="^st\.[a-z_]+$", ) }, hide_index=True, )
在上述示例中,我们继续介绍了 st.column_config 的另一个类 st.column_config.TextColumn,可以用来定制数据编辑器的文本列。
我们仍然使用之前的数据框 data_df,其中包含一个名为 “widgets” 的列,存储了几个 Streamlit 的小部件命令。
使用 st.data_editor 来展示可编辑的数据框,通过使用 column_config 参数对列进行定制。本次我们针对 “widgets” 列使用 st.column_config.TextColumn 进行定制。
我们为 “widgets” 列创建了一个 st.column_config.TextColumn 对象,并指定了以下参数:
title:设置列的标题为 “Widgets”。
help:提供帮助文本,以解释该列是关于 Streamlit 小部件命令的。
default:设置默认值为 “st.”。
max_chars:限制输入文本的最大字符数为 50。
validate:使用正则表达式进行验证,确保值符合 “st.” 以及小写字母和下划线的规则。
通过使用 st.column_config.TextColumn,我们可以对文本列进行更细致的控制和限制。可以设置标题、帮助文本、默认值、字符数限制和输入验证规则,以确保用户提供有效的值。
通过定制文本列,我们可以增强数据编辑器的功能和用户体验,允许用户更轻松地输入和处理数据。