I was looking around for an option to adjust Tabulator cell decimal values with a slider or selector widget but I didn’t find anything. Tabulator does not seem to support cell decimal formatting, even though it supports cell value manipulation otherwise? I suppose I’m left with creating some kind of a widget for asking a number, copying the dataframe with the given number of decimals and finally replacing the old Tabulator with a new one built from the new dataframe? Would there be a better way of offering decimal adjustment to the user ?
Are you looking to dynamically adjust the formatting of a cell or all cells in a column?
In my case all the cells in the whole table, except for one row in some cases. The tables display results in three columns and n rows. It would be great to allow the user to select the number of decimals for the whole table. There is no need for individual cell manipulation. In some cases the first row displays an integer result, it would be the best if that row would always be displayed without any decimals.
Here we go, initial DIY solution in Object Oriented style.
from bokeh.models.widgets.tables import NumberFormatter
from pandas import DataFrame as df
from panel import Column
from panel.template import FastListTemplate
from panel.widgets import IntInput, Tabulator
class GUI():
def __init__(self):
self.data = df({
'a': [1.9807, 2.767, 3.77],
'b': [3.146, 6.284, 9.42],
})
self.precision = self.precision()
self.tabulator = self.tabulator()
def tabulator(self):
rows = self.data.transpose().index.tolist()
tab_format = {
row: NumberFormatter(format='0.0')
for row in rows
}
return Tabulator(self.data, formatters=tab_format)
def callback(self, event):
value = f"0.{event.new * '0'}"
rows = self.data.transpose().index.tolist()
tab_format = {
row: NumberFormatter(format=value)
for row in rows
}
print('0.'.join(value))
self.tabulator.formatters = tab_format
def precision(self):
decimals = IntInput(
name='Select the number of decimals (1-6)',
value=1,
step=1,
start=1,
end=6
)
decimals.param.watch(self.callback, 'value')
return decimals
def main():
gui = GUI()
template = FastListTemplate(
title='Float formatting test',
main=Column(gui.precision, gui.tabulator)
)
template.show()
if __name__ == '__main__':
main()
Strangely this works as a standalone example but it does not work in my app. Everything seems to be similar and debug prints do not show anything peculiar but the numbers on the table won’t change.
So I ended up saving the query result to a class variable and copying that original result, formatting the copy and displaying the copy in the GUI Tabs every time the user adjusts the number of decimals using an IntInput. Probably not the most ‘Panely’ way but works and because the result table is always small it is not a problem performance wise.