You need to enable JavaScript to run this app. 如何使用Bokeh修改DataTable列名字体、字号并设置表头居中对齐
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Bokeh修改DataTable列名字体、字号并设置表头居中对齐

问题原因

你编写的自定义表头样式未生效有两个核心原因:

  • 承载CSS样式的style Div没有被加入到最终的布局中,样式没有被页面加载
  • 原有CSS选择器仅匹配了第3列表头,且没有包含字体、字号、全局居中的规则

完整实现模板

以下是保留你原有单元格格式化逻辑,同时实现表头列名字体、字号自定义、全局居中对齐的完整可运行代码:

from bokeh.models import DataTable, TableColumn, HTMLTemplateFormatter, Div, ColumnDataSource
from bokeh.layouts import layout
from bokeh.io import curdoc, output_file, show
import pandas as pd

# ------------- 这里替换成你自己的数据源 -------------
# 示例数据源,实际使用时替换为你的source/source2/source3
df = pd.DataFrame({
    'Phase': ['阶段1', '阶段2', '阶段3'],
    ' RW ': [0.3, 0.5, 0.2],
    'Plan_Prev_Week': [10.2, 20.5, 30.1],
    'Actual_Prev_Week': [9.8, 21.0, 29.5],
    'Diff_Prev_Week': [-0.4, 0.5, -0.6],
    'Plan_This_Week': [12.0, 22.0, 32.0],
    'Actual_This_Week': [11.5, 22.3, 31.8],
    'Diff_This_Week': [-0.5, 0.3, -0.2],
    'Plan_To_Date': [22.2, 42.5, 62.1],
    'Actual_To_Date': [21.3, 43.3, 61.3],
    'Diff_To_Date': [-0.9, 0.8, -0.8]
})
source = ColumnDataSource(df)
source2 = ColumnDataSource(df.rename(columns={'Phase':'Camp'}))
source3 = ColumnDataSource(df.rename(columns={'Phase':'Camp'}))
# ---------------------------------------------------

# 原有单元格格式化模板保留
template="""
<div style="background:<%= 
    (function colorfromint(){
        if(value < 0){
            return("red")}
        else{return("#51c2d5")}
        }()) %>; 
    color: white";
   > 
<div style="font-size: 20px;">
<%= value.toFixed(1) %>
</div>
</div>
"""
template2="""
<div style="background:<%= 
    (function colorfromint(){
        return("#f9dfdc")
        }()) %>; 
    color: black";
   > 
<div style="font-size: 20px;">
<%= value.toFixed(1) %>
</div>
</div>
"""
template3="""
<div style="font-size: 20px;">
<%= value %>
</div>
"""
template4="""
<div style="background:<%= 
    (function colorfromint(){
        return("#e1e5ea")
        }()) %>; 
    color: black";
   > 
<div style="font-size: 20px;">
<%= value.toFixed(1) %>
</div>
</div>
"""
template5="""
<div style="background:<%= 
    (function colorfromint(){
        return("#f9dfdc")
        }()) %>; 
    color: black";
   > 
<div style="font-size: 20px;">
<%= value.toFixed(1) %>
</div>
</div>
"""
template6="""
<div style="background:<%= 
    (function colorfromint(){
        return("#fff5b7")
        }()) %>; 
    color: black";
   > 
<div style="font-size: 20px;">
<%= value.toFixed(1) %>
</div>
</div>
"""

# -------------- 核心修改:重写表头样式 --------------
header_style = Div(text="""
<style>
/* 全局匹配所有表头列名 */
.slick-header-column .slick-column-name {
    float: none !important; /* 清除默认左浮动 */
    text-align: center !important; /* 文字居中 */
    font-size: 18px !important; /* 自定义表头字号 */
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif !important; /* 自定义表头字体 */
    font-weight: bold !important; /* 可按需设置加粗 */
    color: #0269A4 !important; /* 可按需设置字体颜色 */
}
/* 表头单元格整体居中 */
.slick-header-column {
    text-align: center !important;
}
</style>
""")
# ---------------------------------------------------

formater =  HTMLTemplateFormatter(template=template)
formater2 =  HTMLTemplateFormatter(template=template2)
formater3 =  HTMLTemplateFormatter(template=template3)
formater4 =  HTMLTemplateFormatter(template=template4)
formater5 =  HTMLTemplateFormatter(template=template5)
formater6 =  HTMLTemplateFormatter(template=template6)
columns = [
    TableColumn(field='Phase', title='Phase',formatter=formater3),
    TableColumn(field=' RW ', title='Weight Factor',formatter=formater6),
    TableColumn(field='Plan_Prev_Week', title='Plan Last Week',formatter=formater2),
    TableColumn(field='Actual_Prev_Week', title='Actual Last Week',formatter=formater2),
    TableColumn(field='Diff_Prev_Week', title='Difficiency Last Week',formatter=formater),
    TableColumn(field='Plan_This_Week', title='Plan This Week',formatter=formater4),
    TableColumn(field='Actual_This_Week', title='Actual This Week',formatter=formater4),
    TableColumn(field='Diff_This_Week', title='Difficiency This Week',formatter=formater),
    TableColumn(field='Plan_To_Date', title='Plan To Date',formatter=formater5),
    TableColumn(field='Actual_To_Date', title='Actual To Date',formatter=formater5),
    TableColumn(field='Diff_To_Date', title='Difficiency To Date',formatter=formater)
    ]
columns2 = [
    TableColumn(field='Camp', title='Camp',formatter=formater3),
    TableColumn(field=' RW ', title='Weight Factor',formatter=formater6),
    TableColumn(field='Plan_Prev_Week', title='Plan Last Week',formatter=formater2),
    TableColumn(field='Actual_Prev_Week', title='Actual Last Week',formatter=formater2),
    TableColumn(field='Diff_Prev_Week', title='Difficiency Last Week',formatter=formater),
    TableColumn(field='Plan_This_Week', title='Plan This Week',formatter=formater4),
    TableColumn(field='Actual_This_Week', title='Actual This Week',formatter=formater4),
    TableColumn(field='Diff_This_Week', title='Difficiency This Week',formatter=formater),
    TableColumn(field='Plan_To_Date', title='Plan To Date',formatter=formater5),
    TableColumn(field='Actual_To_Date', title='Actual To Date',formatter=formater5),
    TableColumn(field='Diff_To_Date', title='Difficiency To Date',formatter=formater)
    ]
myTable = DataTable(source=source, columns=columns, sizing_mode='stretch_width',fit_columns=True)
myTable2 = DataTable(source=source2, columns=columns2, sizing_mode='stretch_width',fit_columns=True)
myTable3 = DataTable(source=source3, columns=columns2, sizing_mode='stretch_width',fit_columns=True)

div_article = Div(text="<h1 style=text-align:center> Progress report as of 2-June 21 </h1>",sizing_mode='stretch_width')
div_article2 = Div(text="<h2 style=text-align:center> Construction Progress Per Camp </h2>",sizing_mode='stretch_width')
div_article3 = Div(text="<h2 style=text-align:center> Progress Per Discipline </h2>",sizing_mode='stretch_width')
div_article4 = Div(text="<h2 style=text-align:center> Procurement Progress Per Camp </h2>",sizing_mode='stretch_width')

div_article.style={'color': '#0269A4', 'font-family': 'Helvetica Neue, Helvetica, Arial, sans-serif;','font-size': '1.1em'}
div_article3.style={'color': '#0269A4', 'font-family': 'Helvetica Neue, Helvetica, Arial, sans-serif;','font-size': '1.1em'}
div_article2.style={'color': '#0269A4', 'font-family': 'Helvetica Neue, Helvetica, Arial, sans-serif;','font-size': '1.1em'}
div_article4.style={'color': '#0269A4', 'font-family': 'Helvetica Neue, Helvetica, Arial, sans-serif;','font-size': '1.1em'}
div_article.margin = 30

# -------------- 核心修改:把样式Div加入布局最开头 --------------
l = layout([
    [header_style],
    [div_article],
    [div_article3],
    [myTable],
    [div_article2],
    [myTable2],
    [div_article4],
    [myTable3]
])
# -----------------------------------------------------------
l.sizing_mode = "scale_both"

curdoc().add_root(l)
output_file('result.html')
show(l)

可自定义调整项

你可以根据需求修改CSS里的属性值:

  • 修改font-size的值调整表头字号
  • 修改font-family的值调整表头字体
  • 修改color的值调整表头文字颜色
  • 去掉font-weight: bold !important;即可取消表头加粗

内容的提问来源于stack exchange,提问作者Ahmed Safwat Ewida

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 13:45:05