问题
I can get the DataTable to display in a Jupyter Notebook without any issues. But i haven't been able to get it to display via the Server (curdoc().add_root()). When I attempt to access it I don't get any errors in the server window and just a blank page on the browser. I just see the following:
2017-04-23 16:07:51,188 Starting Bokeh server on port 5006 with applications at paths ['/myapp']
2017-04-23 16:07:51,188 Starting Bokeh server with process id: 7484
2017-04-23 16:07:55,365 200 GET /myapp (172.17.13.2) 188.14ms
2017-04-23 16:07:55,887 WebSocket connection opened
2017-04-23 16:07:55,888 ServerConnection created
Below is what the server is running and when in the notebook it's replaced with the required calls to display it (output_notebook(), show(layout)):
import pandas as pd
from bokeh.plotting import Figure
from bokeh.models import ColumnDataSource, TextInput, Button, Panel, Tabs, Label, DataTable, TableColumn
from bokeh.layouts import Row, Column, widgetbox
from bokeh.io import curdoc, show, output_notebook, gridplot
from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg2://username:password@domain.local:5432/dbname')
def main():
layout = gridplot([[retreive_descriptions()]])
curdoc().add_root(layout)
def retreive_descriptions():
df = pd.read_sql(sql='SELECT description from public."Description_Categories" WHERE category=\'Unknown\'', con=engine)
cds = ColumnDataSource(df)
columns = [TableColumn(field='description', title='Description'),TableColumn(field='category', title='Category')]
cat_data = DataTable(source=cds, columns=columns, editable=True)
return cat_data
I'm using Python 3.4.2 and Bokeh Server version 0.12.5. I'm fairly new to this so any help is appreciated with why it might not be displaying.
回答1:
Seems you can't call the curdoc function inside the main function with the bokeh server. The main.py has to have curdoc function right at the end of the file. This worked.
import pandas as pd from bokeh.plotting import Figure from bokeh.models import ColumnDataSource, TextInput, Button, Panel, Tabs, Label, DataTable, TableColumn from bokeh.layouts import Row, Column, widgetbox from bokeh.io import curdoc, show, output_notebook, gridplot from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg2://username:password@domain.local:5432/dbname')
def retreive_descriptions():
df = pd.read_sql(sql='SELECT description from public."Description_Categories" WHERE category=\'Unknown\'', con=engine)
cds = ColumnDataSource(df)
columns = [TableColumn(field='description', title='Description'),TableColumn(field='category', title='Category')]
cat_data = DataTable(source=cds, columns=columns, editable=True)
return cat_data
curdoc().add_root(gridplot([[retreive_descriptions()]]))
来源:https://stackoverflow.com/questions/43568056/how-to-get-a-bokeh-server-to-display-a-datatable