1

I'm trying to display a dictionary with nested dictionaries inside. And on cells with the nested dictionary, the bokeh datatable display that as [object Object].

So far, I've been unpacking the dictionaries manually to display the actual data, however I was wondering if there is a better way of doing it? perhaps some sort of formatter on the datatable?

I'm open to suggestions.

Kupperu
  • 111
  • 2
  • 11

1 Answers1

2

You could try to do some data manipulation with Pandas before using Bokeh

from bokeh.models import ColumnDataSource
from bokeh.models.widgets import TableColumn, DataTable
from bokeh.io import output_file, show
import pandas as pd

nested_dictionary = {"user1": {'name': 'John', 'age': '27', 'sex': 'Male'},
          "user2": {'name': 'Marie', 'age': '22', 'sex': 'Female'},
          "user3": {'name': 'Luna', 'age': '24', 'sex': 'Female', 'married': 'No'}}
    
df = pd.DataFrame.from_dict(nested_dictionary, orient='index')
        
print (df.head())
source = ColumnDataSource(df)

# Dataframe -> DataTable
table_columns = [TableColumn(field=name, title=name)for name in df.columns]
data_table = DataTable(source=source, columns=table_columns, width=580, height=280)

# Specify the name of the output file and show the result
output_file('example_so.html')
show(data_table)

If your data is coming from a JSON file check json_normalize() from pandas.io.json

DSgUY
  • 101
  • 6
  • this is the best approach I've seen for converting dictionary of dictionaries into a format suitable for a Bokeh ColumnDataSource object. pandas is excellent. The important part is: Dataframe -> DataTable – Marc Compere Sep 05 '22 at 20:03