gpt4 book ai didi

Python Bokeh 表列和标题不对齐

转载 作者:太空狗 更新时间:2023-10-29 21:09:31 24 4
gpt4 key购买 nike

我正在尝试使用 python 和可视化库在 jupyter notebook 中显示表格 Bokeh .我使用以下代码在 jupyter notebook 中显示我的表格,其中 result 是一个数据框:

source = ColumnDataSource(result)

columns = [
TableColumn(field="ts", title="Timestamp"),
TableColumn(field="bid_qty", title="Bid Quantity"),
TableColumn(field="bid_prc", title="Bid Price"),
TableColumn(field="ask_prc", title="Ask Price"),
TableColumn(field="ask_qty", title="Ask Quantity"),
]

data_table = DataTable(source=source, columns=columns, fit_columns=True, width=1300, height=800)
show(widgetbox([data_table], sizing_mode = 'scale_both'))

以前我使用的是 vform,尽管现在它似乎已经贬值并且不再按预期工作。这发生在我的 jupyter notebook 版本更新之后。无论我设置什么宽度,我的列标题都不会对齐并且与表格有奇怪的重叠:

enter image description here

这在以前没有发生过,我得到了一张漂亮的 table ,所有东西都排好了。即使我调整标题,他们仍然不会排队。当我将表格保存为 html 文件而不是直接在 Jupyter 笔记本中调用 show() 时,不会发生这种情况。我需要改变什么?有更好的方法吗?

完整示例

from bokeh.io import show, output_notebook
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import TableColumn, DataTable
import pandas as pd

output_notebook()

d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)

source = ColumnDataSource(df)

columns = [
TableColumn(field="one", title="One"),
TableColumn(field="two", title=" Two"),
]

data_table = DataTable(source=source, columns=columns,
fit_columns=True, width=800, height=800)
show(widgetbox([data_table], sizing_mode = 'scale_both'))

这在具有以下版本的系统上运行:

  • Jupyter 4.2.0
  • Python 2.7.12(Anaconda 2.3.0 64 位)
  • 背景虚化 0.12.2

最佳答案

Jupyter 笔记本的 Bokeh 小部件的 css 样式在 http://cdn.bokeh.org/bokeh/release/bokeh-widgets-0.12.0.min.css 中,其中元素 .bk-root .bk-slick-header-column.bk-ui-state-defaultheight:16px 是硬编码的。所以不改变css就不能改变它。

它可以通过 HTML 函数来定制样式

from IPython.core.display import HTML
HTML("""
<style>
.bk-root .bk-slick-header-column.bk-ui-state-default {
height: 25px!important;
}
</style>
""")

对于持久性更改,可以将 css 添加到 Jupyter 配置中的 custom 目录。您可以通过调用

找出它的位置
jupyter --config-dir

默认是~/.jupyter新的 css 需要在 ~/.jupyter/custom/custom.css 然后。

之前enter image description here

enter image description here 之后

关于Python Bokeh 表列和标题不对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39732842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com