Ejemplo n.º 1
0
def bokeh_main_2():
    import sys
    src = Path(__file__).absolute().parent / 'src'
    sys.path.insert(0, str(src))

    from bokeh.layouts import layout  # type: ignore
    from bokeh.models.widgets import Tabs, Panel  # type: ignore
    from bokeh.plotting import figure  # type: ignore

    from dashboard.core.bokeh import test_scatter_matrix
    import holoviews as hv
    f1 = hv.render(test_scatter_matrix())

    f2 = figure()
    f2.circle([0, 0, 2], [4, -1, 1])

    # todo need something more lazy?
    from dashboard.data import sleep_dataframe
    from dashboard.sleep import plot_sleep
    # TODO would be cool to display logs in the frontend and also currently evaluated function?
    # although pehaps it's easier to just always keep logs open?

    from bokeh.models.widgets import DateRangeSlider  # type: ignore
    from datetime import date
    drs = DateRangeSlider(
        title="Date Range: ",
        start=date(2017, 1, 1),
        end=date.today(),
        value=(date(2017, 9, 7), date(2017, 10, 15)),
        step=1,
    )

    def update(attr, old, new):
        print(attr, old, new)

    from bokeh.models import CustomJS  # type: ignore
    # todo see https://docs.bokeh.org/en/latest/docs/gallery/slider.html
    update_js = CustomJS(args=dict(drs=drs),
                         code='''
console.log("HIIII");
''')

    drs.on_change('value', update)
    drs.js_on_change('value', update_js)

    l1 = layout([[f1, drs]], sizing_mode='fixed')
    l2 = layout([[f2]], sizing_mode='fixed')
    tabs = Tabs(tabs=[
        Panel(child=l1, title='This is Tab 1'),
        Panel(child=l2, title='This is Tab 2'),
        # Panel(child=layout([[sp]]), title='Sleep dataframe'),
    ])

    curdoc().add_root(tabs)
Ejemplo n.º 2
0
def date_slider(p, *, dates=None, date_column='dt'):
    # todo a bit crap, but kinda works.. not sure what's the proper way?
    # maybe they aren't computed before show()??
    # p.x_range.on_change('start', lambda attr, old, new: print("Start", new))
    # p.x_range.on_change('end', lambda attr, old, new: print("End", new))

    if dates is None:
        graphs = p.renderers
        # todo autodetect by type instead of name?
        dts = np.concatenate([g.data_source.data[date_column] for g in graphs])
        # FFS... apparently no easier way
        sdate = pd.Timestamp(np.min(dts)).to_pydatetime()
        edate = pd.Timestamp(np.max(dts)).to_pydatetime()
    else:
        sdate = min(dates)
        edate = max(dates)
    # todo not sure if should use today?
    edate += timedelta(days=5)

    from bokeh.models.widgets import DateRangeSlider  # type: ignore
    ds = DateRangeSlider(
        title="Date Range: ",
        start=sdate,
        end=edate,
        value=(sdate, edate),
        step=1,
    )
    from bokeh.models import CustomJS  # type: ignore

    # TODO hmm. so, js won't be able to call into python in Jupyter...
    # see https://docs.bokeh.org/en/latest/docs/gallery/slider.html
    update_js = CustomJS(args=dict(ds=ds, xrange=p.x_range),
                         code=J('''
    const [ll, rr] = ds.value;
    // didn't work??
    // xrange.set({"start": ll, "end": rr})
    xrange.start = ll;
    xrange.end   = rr;

    // todo hmm, turned out it wasn't necessary??
    // source.trigger('change');
    '''))

    # todo add some quick selectors, e.g. last month, last year, etc like plotly
    ds.js_on_change('value', update_js)
    return ds
Ejemplo n.º 3
0
               source=source)

fig.add_tools(
    HoverTool(tooltips=[("Date", "@dispdates"), (
        "Old", "@dispoldconf"), ("New",
                                 "@dispnewconf"), ("Total", "@dispcumconf")]))

slider = DateRangeSlider(width=300,
                         title="Date",
                         callback_policy='mouseup',
                         tooltips=False,
                         start=stdate,
                         end=enddate,
                         value=(stdate, enddate),
                         step=1)
slider.js_on_change('value', callback)
l = layout(children=[[slider]])

fig.y_range.start = 0
fig.xgrid.grid_line_color = None
fig.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
fig.xaxis.minor_tick_line_color = None
fig.xaxis.major_label_text_font_size = '0pt'
fig.xgrid.grid_line_color = None
fig.ygrid.grid_line_color = None

data1 = {
    'x': x,
    'dates': dates,
    'dispdates': dates,
    'cumact': cumact,