如何显示日期选择器小部件?

时间:2018-10-10 16:22:12

标签: python python-3.x bokeh

我有以下代码:

from bokeh.plotting import curdoc
from bokeh.layouts import widgetbox
from bokeh.layouts import column as bokehCol
from bokeh.models.layouts import Column
from bokeh.models.widgets import DatePicker
from datetime import date
from datetime import timedelta as td
from datetime import datetime as dt
from bokeh.io import output_notebook
from bokeh.plotting import show

output_notebook()

crnt_date=dt.now()

dt_pckr_strt=DatePicker(title='Select start of sync date',min_date=date(2017,1,1),max_date=date.today())

def callback(attr,old,new):
    print(type(old))
    print('old was {} and new is {}'.format(old,new))

dt_pckr_strt.on_change('value',callback)
curdoc().add_root(bokehCol(dt_pckr_strt))
show(widgetbox(dt_pckr_strt, width=300))

输出显示奇怪,而不是传统的日期选择器。我相信这是在表演功能,我做错了。我要去哪里错了?

2 个答案:

答案 0 :(得分:0)

您并没有做错任何事情(或多或少-如果您想使用真实的python回调,例如on_change,则必须嵌入 Bokeh Server Application ,但这是单独发行)。但是,似乎有关笔记本计算机环境的某些内容导致Bokeh日期选择器小部件无法正常运行。使用output_file创建的独立HTML文件中的相同代码具有工作日期选择器。我建议使用所有这些信息在Bokeh issue tracker上提交错误报告。不幸的是,目前我没有任何简单的解决方法可以建议。

答案 1 :(得分:0)

此代码对我有用(解决了将Bokeh服务器嵌入Jupyter Notebook中的问题)

from bokeh.plotting import curdoc
from bokeh.layouts import widgetbox
from bokeh.layouts import column as bokehCol
from bokeh.models.layouts import Column
from bokeh.models.widgets import DatePicker
from datetime import date
from datetime import timedelta as td
from datetime import datetime as dt
from bokeh.io import output_notebook
from bokeh.plotting import show
from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler

output_notebook()

def modify_doc(doc):

    crnt_date=dt.now()

    dt_pckr_strt=DatePicker(title='Select start of sync date',min_date=date(2017,1,1),max_date=date.today())


    def callback(attr,old,new):
        print(type(old))
        print('old was {} and new is {}'.format(old,new))



    dt_pckr_strt.on_change('value',callback)

    doc.add_root(bokehCol(dt_pckr_strt))


app = Application(FunctionHandler(modify_doc))
show(app) #notebook_url="localhost:8888"

enter image description here