首页 > 解决方案 > 如何在散景中显示文本同时保留线间隙

问题描述

我正在尝试使用 Bokeh 2.0.1 生成由绘图和一些文本组成的报告,并且我正在尝试保留我输入的文本中的原始行间距bokeh.layouts.gridplot

但是,我看到在生成 HTML 文件后,所有的行间距都被删除了,基本上,所有的段落都被合并成一个连续的句子。我不确定为什么会这样。我在 Bokeh 文档中没有找到关于这个问题的太多帮助。

这是我试图开始工作的代码部分的一个最小示例。

from bokeh.layouts import gridplot
from bokeh.models import Paragraph
from bokeh.plotting import output_file, save

sampText = "PARAGRAPH 1: This is a minimal example of a text. This is a minimal example of a text. This is a minimal example of a text. This is a minimal example of a text. This is a minimal example of a text. This is a minimal example of a text.\n

PARAGRAPH 2: This is a minimal example of a text. This is a minimal example of a text. This is a minimal example of a text. This is a minimal example of a text. This is a minimal example of a text."

fig1 = ...

fig2 = ...

text = Paragraph(text = sampText, width = 1500, height_policy = "auto", style = {'fontsize': '10pt', 'color': 'black', 'font-family': 'arial'})

output_file(filename = "myMinimalExampleOutput" + ".html")
output = gridplot([fig1, fig2, text], ncols = 1, plot_width = 1500, sizing_mode = 'scale_width')
save(output)

有人可以指出为什么会这样吗?

标签: htmlpython-3.xtextbokeh

解决方案


这就是 HTML 的工作原理——它从文本中删除所有“不必要的”空格。

为了使您的两个段落起作用,您必须将它们中的每一个都包装在<p></p>. 但由于Paragraph生成相同的标签,您不能使用该类。使用常规Div并将其text输入参数设置为您要呈现的 HTML。

Div(text='<p>paragraph 1</p><p>paragraph 2</p>')

推荐阅读