You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 12:07:06