如何在Streamlit的st.data_editor组件中增大字体大小?
解决Streamlit st.data_editor 字体大小调整问题
要调整st.data_editor的字体大小,关键是针对其底层ag-Grid的元素编写正确的CSS样式,以下是可行方案:
- 注入自定义CSS样式:通过
st.markdown注入CSS,直接定位到data editor内的单元格和表头元素,强制覆盖默认字体大小。
完整代码示例:
# 注入自定义CSS调整字体大小 st.markdown(""" <style> /* 调整表格内所有单元格的字体大小 */ div[data-testid="stDataEditor"] .ag-cell { font-size: 16px !important; } /* 可选:调整表头的字体大小 */ div[data-testid="stDataEditor"] .ag-header-cell-text { font-size: 17px !important; } </style> """, unsafe_allow_html=True) # 你的原有st.data_editor代码 edited_df = st.data_editor( processed_data, use_container_width=True, height=1000, key="data_editor", column_config={ "_all": { "width": 150, # default width for all columns } } )
注意事项:
- 按需修改
font-size后的数值(比如改成18px),根据实际需求调整。 !important用于强制覆盖Streamlit的默认样式优先级,确保自定义样式生效。- 若页面存在多个st.data_editor,需针对特定表格调整时,可通过key定位:将选择器改为
div[data-testid="stDataEditor-data_editor"] .ag-cell(其中data_editor是你设置的表格key值)。
内容的提问来源于stack exchange,提问作者Tyche
相关产品推荐
相关产品推荐

