from datetime import date
from random import randint
import bokeh
import bokeh.plotting 
from bokeh.plotting import figure, curdoc
from bokeh.models import Button
from bokeh.layouts import column

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

source = bokeh.models.ColumnDataSource(data)


#--------------------------------------------------------------------------------------------------
data2 = dict(dates=[date(2014, 3, i+1) for i in range(10)],
            downloads=[randint(0, 100) for i in range(10)])

source2 = bokeh.models.ColumnDataSource(data2)


#--------------------------------------------------------------------------------------------------
columns = [bokeh.models.TableColumn(field="dates", title="Date",
           formatter=bokeh.models.DateFormatter()),
           bokeh.models.TableColumn(field="downloads", title="Downloads")]


#--------------------------------------------------------------------------------------------------
# source.callback = bokeh.models.CustomJS(args=dict(source=source), code="""
#        console.log(source.data);
#        """)

#--------------------------------------------------------------------------------------------------
data_table = bokeh.models.DataTable(source=source, columns=columns, width=400, height=280, editable=True)

data_table2 = bokeh.models.DataTable(source=source2, columns=columns, width=400, height=280, editable=True)

#--------------------------------------------------------------------------------------------------
def callback():
    print (100*"=")
    # print(source.data)
    # print (100*"-") 
    # print(source.selected)
    # print(source.selected.keys())
    # print(source.selected['1d'])
    # print(source.selected['1d']['indices'])
    indicesL = source.selected['1d']['indices']
    for i in indicesL:
        print (i)
        print (source.data['downloads'][i])
    # print(source.selected['2d'])
    df = source.to_df()
    print (df.head())
    source2 = source.selected

    # print (100*"-")
    # print(dir(source))



#--------------------------------------------------------------------------------------------------
button = Button(label="Press Me")
button.on_click(callback)

#--------------------------------------------------------------------------------------------------
curdoc().add_root(column(button, data_table, data_table2))
