I’m trying to write a function that’ll quickly print a dataframe as a DataTable when working with pandas on any project. I can’t work out how to make column widths auto adjust:
import pandas as pd, numpy as np, random, string from bokeh.models import ColumnDataSource, DataTable, TableColumn from bokeh.plotting import show df = pd.DataFrame(np.random.randint(0,100,size=(100, 12)), columns=[(random.randint(1,30) * random.choice(string.ascii_uppercase)) for col in range(12)]) def bokehprint(x): show(DataTable(columns= [TableColumn(field=c, title=c, width=len(c)) for c in x.columns], fit_columns = True, sizing_mode = 'stretch_both', source=ColumnDataSource(x))) bokehprint(df)
Setting width in
TableColumn doesn’t seem to change anything, and
fit_columns will evenly fit columns across display width, but I’d like column widths to adapt appropriately. What am I doing wrong?
Also, there’s no way to have a drop down filter like you could in MS Excel, is there? I looked in documentation but didn’t find anything, but that’d be really nice.
Crazy idea: wouldn’t it be cool for Bokeh to come with a standard df print function like this? So instead of
print(df) any pandas user with zero understanding of Bokeh could go
from bokeh import bokehprint and
bokehprint(df) or similar? I reckon that might create great publicity for Bokeh and serve as a good introduction to Bokeh for prospective users.