18

Is there a nice way to display data frames with Bokeh? I have a bunch of table-based text I'd like to display and dynamically update along with some graphs, but I haven't found a good way to do this yet.

helloB
  • 3,472
  • 10
  • 40
  • 87

3 Answers3

19

You should take a look into bokeh.models.widgets.DataTable

http://docs.bokeh.org/en/latest/docs/user_guide/interaction.html#data-table

Example:

from datetime import date
from random import randint

from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn
from bokeh.io import output_file, show, vform

output_file("data_table.html")

data = dict(
        dates=[date(2014, 3, i+1) for i in range(10)],
        downloads=[randint(0, 100) for i in range(10)],
    )
source = ColumnDataSource(data)

columns = [
        TableColumn(field="dates", title="Date", formatter=DateFormatter()),
        TableColumn(field="downloads", title="Downloads"),
    ]
data_table = DataTable(source=source, columns=columns, width=400, height=280)

show(vform(data_table))

You could exchange data with your own DataFrame like:

data = dict(df[['first', 'second']])

If the column names differ please change columns variable to:

columns = [
        TableColumn(field="first", title="First"),
        TableColumn(field="second", title="Second"),
    ]
bigreddot
  • 33,642
  • 5
  • 69
  • 122
Nils
  • 2,665
  • 15
  • 30
15

To convert a Pandas DataFrame DF into a table in Bokeh, you could use a list comprehension to set the table up:

from bokeh.models.widgets import DataTable, DateFormatter, TableColumn

Columns = [TableColumn(field=Ci, title=Ci) for Ci in DF.columns] # bokeh columns
data_table = DataTable(columns=Columns, source=ColumnDataSource(DF)) # bokeh table

show(data_table)

This assumes you want the Bokeh columns to match the DF column names, but that is easily modified if needed.

Q-man
  • 2,069
  • 1
  • 17
  • 16
0

You want to use bokeh.models.widgets.DataTable:

http://docs.bokeh.org/en/latest/docs/user_guide/interaction.html#data-table

bigreddot
  • 33,642
  • 5
  • 69
  • 122
Luke Canavan
  • 2,107
  • 12
  • 13