Filtering dataframe using Bokeh/Widget/Callback

2020-07-30 01:18发布

问题:

Currently my data set is in the format: Date, Currency, Price which I am filtering at Currency level and then using it to generate graphs.

I want to improve it so that all the filtering is done using Python widget/Dropdown boxes?

I'm new to Python/Bokeh so I need some help.

Date      Currency    Price
1/1/2017  AUDUSD      1.01
2/1/2017  AUDUSD      1.02
3/1/2017  AUDUSD      1.03
1/1/2017  USDJPY      1.01
2/1/2017  USDJPY      1.02
3/1/2017  USDJPY      1.03
1/1/2017  CADUSD      1.01
2/1/2017  CADUSD      1.02
3/1/2017  CADUSD      1.03

回答1:

#Creating CCyPair wise menu
menu = Select(options=['AUDUSD','USDJPY'], value='AUDUSD')
#Function for dataframe
def get_all_price_dataset(src,name):
  df = src[(src.CCYPair == name) & (src.TYPE == 'Prices')].copy()
  return df
# Function to update Plot
def update_plot(attrname, old, new):
  newccy = menu.value
  side = buysellmenu.value
  datevalue = datemenu.value
  src_data_table = 
  ColumnDataSource(get_all_dataset(Combined,newccy,side,datevalue))
  DisplayData.data.update(src_data_table.data)
  #On change in menu,function gets called.
  menu.on_change('value', update_plot)
  #Displaying Menu and Plot.
  layout = layout([menu],
                  [plot])
  curdoc().add_root(layout)