|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import datetime |
| 4 | + |
| 5 | +from bokeh.io import curdoc |
| 6 | +from bokeh.layouts import row, column |
| 7 | +from bokeh.plotting import figure |
| 8 | +from bokeh.models import ColumnDataSource |
| 9 | +from bokeh.models.widgets import Select, TextInput, DatePicker, Button |
| 10 | + |
| 11 | +# Load data, probably from a database. I am loading it directly from a file |
| 12 | +df = pd.read_hdf('derivatives_may_2019.h5') |
| 13 | +symbols = sorted(list(df.symbol.unique())) |
| 14 | +source = ColumnDataSource(df) |
| 15 | + |
| 16 | +def update_expiry_date(attrname, old, new): |
| 17 | + """ |
| 18 | + Update expiry date for this particular symbol |
| 19 | + """ |
| 20 | + print(attrname, old, new) |
| 21 | + q = 'symbol == "{}"'.format(new) |
| 22 | + dates = sorted(df.query(q).expiry_dt.unique()) |
| 23 | + dates = [str(dt)[:10] for dt in dates] |
| 24 | + expiry_select.options = dates |
| 25 | + |
| 26 | + |
| 27 | +# Create widgets |
| 28 | +select_symbols = Select(title='Pick a symbol', options=symbols) |
| 29 | +expiry_select = Select(title='Select an expiry date', options=None) |
| 30 | +date_picker = DatePicker(title='Select a date') |
| 31 | +button = Button(label="Update", button_type="success") |
| 32 | + |
| 33 | +# Event handlers |
| 34 | +select_symbols.on_change('value', update_expiry_date) |
| 35 | +date_picker.on_change('value', lambda x,y,z: print(z)) |
| 36 | + |
| 37 | +layout = column( |
| 38 | + row(select_symbols, expiry_select), |
| 39 | + row(date_picker, button) |
| 40 | + ) |
| 41 | + |
| 42 | +# add the layout to curdoc |
| 43 | +curdoc().add_root(layout) |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + print('Hello') |
| 47 | + |
| 48 | + |
0 commit comments