from bokeh.layouts import widgetbox, layout
from bokeh.models.widgets import MultiSelect
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure

import pandas as pd



df_test = pd.DataFrame({'x':[1, 2, 3, 4], 'height':[2, 3, 4, 5], 'col1':['a', 'b', 'c', 'd'], 'col2':[2, 4, 6, 8], 'col3':['mon', 'tues', 'wed', 'fri']})
df_test['y'] = df_test.height / 2.0

datasource = ColumnDataSource({"x":[], "y":[], "height":[]})

p = figure(width=800, height=500, x_range=datasource.data['x'])
p.rect(x="x", y="y", width=0.8, height="height", source=datasource, color="#2171b5", alpha=0.6)

hover = HoverTool(tooltips=[("x", "@x"), ("y", "@y")])
p.add_tools(hover)

# facets select
ms = MultiSelect(title="column selection", value=[], options=['col1', 'col2', 'col3'], width=200)

def update(attribute, old, new):
    v = ms.value
    df_plot = df_test[['x', 'y', 'height']+v] # not usefull here but mimics what I do in my full code.
    p.x_range.factors = list(df_plot.x)
    datasource.data = ColumnDataSource.from_df(df_plot)

    # hover tool : my current (bad) solution
    tt = [(x, '@'+x) for x in v]
    p.add_tools(HoverTool(tooltips=[("x", "@x"), ("y", "@y")]+tt))

    # what I'd like to do:
    # hover.tooltips = [("x", "@x"), ("y", "@y")]+tt




ms.on_change('value', update)


# layout
l = layout([[widgetbox(ms), p]])

curdoc().add_root(l)
curdoc().title = "bokeh app"