如何使用 HTMLTemplateFormatter 调整散景数据表中的数字格式? [英] How do I adjust number format in bokeh data table using HTMLTemplateFormatter?

查看:44
本文介绍了如何使用 HTMLTemplateFormatter 调整散景数据表中的数字格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调整 HTMLTamplateFormatter 中数据的数字格式.我希望数字格式为(0,0)".以下是错误尝试的示例代码:

How do I adjust the number format of data in HTMLTamplateFormatter. I would like the number format to be "(0,0)". Here is sample code with the incorrect attempt:

    from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, TableColumn, HTMLTemplateFormatter, NumberFormatter
from bokeh.io import show

dict1 = {'x':[0]*6,'y':[500,1000,-1000,1000,-5000,500],'z':[0,0,1,1,1,2]}
source = ColumnDataSource(data=dict1)

template="""
<b><div style="background:<%= 
    (function colorfromint(){
        if(z == 1){
            return("NavajoWhite")}
        else{if(z == 2){
            return("Orange")}
        else{return("")}
        }}()) %>;
       font-style:'(0,0)'"> ### this part needs fixed
<%= value %></div></b>
"""

formater =  HTMLTemplateFormatter(template=template)
columns = [
    TableColumn(field="x", title="x"),
    TableColumn(field="y", title="y",formatter=formater)
]

data_table = DataTable(source=source, columns=columns, width=800)

show(data_table)

推荐答案

您应该注意的几件事.这个例子有点像黑客,涉及基于 javascript 设置 css 属性.设置的属性是 css 属性,因此无法通过 css 更改数字格式.

Few things you should be aware of. This example is quite a bit of a hack and involves setting css properties based on javascript. the properties being set are css properties, and it is thus impossible to change the number formatting through css.

您有两种选择 - 一种是在 python 中格式化所有值并将它们传递到数据表中.

You have two options - one is to format all values within python and pass these into the data table.

第二个选项是更多的 javascript 代码.

The second option is more javascript code.

这是我在 javascript 中使用 .toFixed(digits) 函数的示例.

Here is my example using .toFixed(digits) function in javascript.

from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, TableColumn, HTMLTemplateFormatter, NumberFormatter
from bokeh.io import show

dict1 = {'x':[0]*6,
'y':[500.23,1000,-1000.234,1000,-5000.23,500],
'z':[0,0,1,1,1,2]}
source = ColumnDataSource(data=dict1)

template="""
<b><div style="background:<%= 
    (function colorfromint(){
        if(z == 1){
            return("NavajoWhite")}
        else{if(z == 2){
            return("Orange")}
        else{return("")}
        }}()) %>;">
<%= (value).toFixed(1) %></div></b>
"""

formater =  HTMLTemplateFormatter(template=template)
columns = [
    TableColumn(field="x", title="x"),
    TableColumn(field="y", title="y",formatter=formater)
]

data_table = DataTable(source=source, columns=columns, width=800)

show(data_table)

顺便说一句,我还应该告诉您,您可以在 python 中选择所有条件颜色并将它们传递给每个值 - 因此消除了模板中的任何复杂代码.

As an aside, I should also let you know that you can chose all the conditional colours within python and pass these in for each value - therefore eliminating any complicated code within the template.

这是一个示例,向您展示如何使用从 python 设置的颜色:(显然,您可以使用规则为基于值的颜色生成 rgb/字符串).

Here is an example showing you how you can use colors set from python: (obviously you can use a rule to generate rgb/strings for colors based on values).

from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, TableColumn, HTMLTemplateFormatter, NumberFormatter
from bokeh.io import show

dict1 = {'x':[0]*6,
'y':[500.23,1000,-1000.234,1000,-5000.23,500],
'z':[0,0,1,1,1,2],
'color':['red','green','purple','blue','grey','white']}
source = ColumnDataSource(data=dict1)

template="""
<b><div style="background:<%= color%>;">
<%= (value).toFixed(1) %></div></b>
"""

formater =  HTMLTemplateFormatter(template=template)
columns = [
    TableColumn(field="x", title="x"),
    TableColumn(field="y", title="y",formatter=formater)
]

data_table = DataTable(source=source, columns=columns, width=800)

show(data_table)

这篇关于如何使用 HTMLTemplateFormatter 调整散景数据表中的数字格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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