How do I adjust number format in bokeh data table using HTMLTemplateFormatter?

不想你离开。 提交于 2019-12-21 23:42:05

问题


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)

回答1:


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.

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

The second option is more javascript code.

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)

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.

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)


来源:https://stackoverflow.com/questions/44731757/how-do-i-adjust-number-format-in-bokeh-data-table-using-htmltemplateformatter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!