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

Bokeh DataTable自定义HTMLTemplateFormatter显示异常问题咨询

Fix Bokeh DataTable HTMLTemplateFormatter Offset & Truncation in Exported HTML

Let me break down why this is happening and how to fix it— I've run into similar styling conflicts between Jupyter and standalone Bokeh HTML outputs before!

Why the Issue Occurs

The core problem is style context differences between Jupyter Notebook and standalone HTML:

  • Jupyter wraps Bokeh outputs in its own container with built-in CSS resets and padding that "fix" default element spacing automatically.
  • In standalone HTML, Bokeh uses its default table styles: <td> elements have built-in padding, and your template's <p> tag has default browser margins/padding. These combined push your colored square character out of alignment or truncate it.
  • On top of that, you've got conflicting width settings: fit_columns=True tries to auto-adjust column widths, but you've set explicit column widths plus a DataTable(width=400) that's narrower than the total column width sum (100+100+500+~100 = 800). This forces columns to compress, causing truncation.

Step-by-Step Fixes

Here's the adjusted code with explanations for each change:

from bokeh.io import output_notebook, show, output_file
from bokeh.palettes import Spectral
from random import randint
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, TableColumn, HTMLTemplateFormatter

# Optional: Keep Jupyter output if needed
# output_notebook()
output_file("data_table.html")

data = dict(
    cola=[randint(0, 100) for i in range(10)],
    colb=[randint(0, 100) for i in range(10)],
    colc=['&#9608' for i in range(10)],  # Keep your square character
    color=Spectral[10]
)
source = ColumnDataSource(data)

# 1. Replace <p> with <span> and eliminate default spacing styles
# <p> is a block element with default margins; <span> is inline and cleaner for table cells
template="""
<span style="color:<%= color %>; margin: 0; padding: 0; font-size: 18px; vertical-align: middle;">
<%= value %>
</span>
"""

formatter = HTMLTemplateFormatter(template=template)

# 2. Fix width conflicts: Match total column widths to DataTable width
# Removed fit_columns=True to avoid auto-resize clashes with explicit widths
columns = [
    TableColumn(field="cola", title="CL1", width=100),
    TableColumn(field='colb', title='CL2', width=100),
    TableColumn(field='colc', title='CL3', formatter=formatter, width=200),  # Reduced to fit total width
    TableColumn(field='color', title='CL4', width=100)
]

# Set DataTable width to sum of column widths (100+100+200+100=500) plus a small buffer
data_table = DataTable(
    source=source, 
    columns=columns, 
    selectable=True, 
    sortable=True, 
    width=520,  # Matches total column width + small buffer
    height=400
)

show(data_table)

Key Changes Explained

  1. Template Element Swap: Using <span> instead of <p> removes default block-level margins that were pushing content down. The inline styles margin:0; padding:0; eliminate any unexpected spacing, and vertical-align: middle ensures the square aligns with other cell content.
  2. Width Consistency: Removed fit_columns=True because it overrides explicit column widths in standalone HTML. Now the total column width (500) plus a small buffer matches the DataTable's width (520), so no forced compression or truncation.
  3. Font Size Adjustment: Bumped font-size to make the square more visible without overflowing the cell.

Testing the Fix

Run this code, open the exported data_table.html file in any browser—your colored squares should align perfectly with other cells, no truncation or offset issues.

内容的提问来源于stack exchange,提问作者sehan2

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 22:17:35