python散景:DataTable单元格样式 [英] python Bokeh: DataTable cells styling

查看:183
本文介绍了python散景:DataTable单元格样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的dataTable单元格添加一个自定义格式化程序.

I'd like to have a custom formatter for one my dataTable cells.

例如,我想将标题栏字体样式设置为粗体.

Say, for example I'd like to have the title column font style in bold.

from bokeh.plotting import show
from bokeh.models.sources import ColumnDataSource
from bokeh.models.widgets import DataTable, TableColumn

if __name__ == '__main__':

    source = ColumnDataSource(data = dict(hello = ["one", "two", "three"]))

    c0 = TableColumn(field="hello", 
                     title= "TITLE IN BOLD?")

    ds = DataTable(source=source,
                   columns = [c0])

    show(ds) 

在BokehJS中有一种简单的方法吗?

Is there a simple way to do this in BokehJS?

谢谢

推荐答案

使用HTMLTemplateFormatter可以完成工作.这样一来,列中的所有单元格都可以设置为代码格式,因此您可以在任何单元格中添加粗体标签或其他所需的内容.

Using the HTMLTemplateFormatter can get the job done. This allows all cells in the column to be formatted as code so you can add in bold tags or what ever else you want in any of the cells.

注意:如果您使用的是bokeh版本0.12.5 ,不幸的是他们删除了下划线js,以便解决该问题(丑陋的代码的第一块),您可以使用此github问题的解决方法: https://github.com/bokeh/bokeh/issues/6207 .或者,在输出的html文件(即<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script>

Note: if you are using bokeh version 0.12.5 Unfortunately they have removed underscore js so to get around it (first block of ugly code), you can use the work around from this github issue: https://github.com/bokeh/bokeh/issues/6207. Or alternatively include underscorejs/lodash in the output html file, i.e. <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script>

from bokeh.models.widgets import HTMLTemplateFormatter
from bokeh.plotting import show
from bokeh.models.sources import ColumnDataSource
from bokeh.models.widgets import DataTable, TableColumn

class MyHTMLFormatter(HTMLTemplateFormatter):
    __javascript__ = ["https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"]
    __implementation__ = """
import {HTMLTemplateFormatter} from "models/widgets/cell_formatters"
export class MyHTMLFormatter extends HTMLTemplateFormatter
  type: 'MyHTMLFormatter'
"""



if __name__ == '__main__':

    source = ColumnDataSource(data = dict(hello = ["<b>one</b>", "two", "three"]))
    formatter = HTMLTemplateFormatter(template='<code><%= value %></code>')
    c0 = TableColumn(field="hello", 
                     title= "<b>TITLE IN BOLD?</b>", formatter=formatter)

    ds = DataTable(source=source,
                   columns = [c0])

    show(ds) 

这篇关于python散景:DataTable单元格样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆