Skip to content Skip to sidebar Skip to footer

How Do I Adjust Number Format In Bokeh Data Table Using Htmltemplateformatter?

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.mod

Solution 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:<%= 
    (functioncolorfromint(){
        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)

Post a Comment for "How Do I Adjust Number Format In Bokeh Data Table Using Htmltemplateformatter?"