且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向bokeh数据表标题添加颜色

更新时间:2023-11-30 23:11:58

不幸的是,这并不是一件容易的事. SlickGrid(DataTable的基础)具有许多CSS可配置属性,因此将它们全部作为Bokeh模型上的Python属性公开是禁止的.因此,您将必须直接在模板中定位SlickGrid CSS.事情因未提供的详细信息而有所不同(这是一个独立的HTML文档吗?由带components的Web应用程序提供服务吗?还是Bokeh服务器应用程序?),因此这是使用file_html的完整的最小示例,您可以将其用作一个适应其他情况的基础:

Unfortunately this is not completely non-trivial to do. SlickGrid (which is the basis of DataTable) has many dozens of CSS configurable properties, so exposing them all as Python properties on Bokeh models is prohibitive. So, you will have to target the SlickGrid CSS directly in a template. Things vary somewhat depending on details you have not provided (is this a standalone HTML doc? Served by a web app with components? A Bokeh server application?) so here is a complete minimal example using file_html that you could use as a basis adapt to other situations:

import jinja2

from bokeh.document import Document
from bokeh.embed import file_html
from bokeh.resources import CDN
from bokeh.util.browser import view
from bokeh.models import ColumnDataSource, DataTable, TableColumn
from bokeh.sampledata.autompg2 import autompg2 as mpg

source = ColumnDataSource(data=mpg)
columns = [
    TableColumn(field="manufacturer", title="Manufacturer"),
    TableColumn(field="model", title="Model"),
    TableColumn(field="displ", title="Displacement"),
    TableColumn(field="year", title="Year"),
    TableColumn(field="cyl", title="Cylinders"),
    TableColumn(field="cty", title="City MPG"),
    TableColumn(field="hwy", title="Highway MPG"),
]
table = DataTable(source=source, columns=columns, width=800, css_classes=["mycustom"])

doc = Document()
doc.add_root(table)

template = jinja2.Template("""
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>{{ title|e if title else "Bokeh Plot" }}</title>
        {{ bokeh_css }}
        {{ bokeh_js }}
        <style>
          .slick-header-column {
            background-color: lightblue !important;
            background-image: none !important;
          }
        </style>
    </head>
    <body>
        {{ plot_div|indent(8) }}
        {{ plot_script|indent(8) }}
    </body>
</html>
""")

if __name__ == "__main__":
    filename = "widgets.html"
    with open(filename, "w") as f:
        f.write(file_html(doc, CDN, "Table", template=template))
    view(filename)