bokeh on_change for patches


bokeh on_change for patches



Using python 3.6 and bokeh 13.0.
I'm combining a bokeh patches to create a map of provinces and
and bokeh circle to overlay the cities.



I want to use this map as interactive input filter for another plot.



When clicking cities is no problem, I get the name of the city.



When trying to do the same for a province, which is not a single point, but rather a polygon, I receive the following error message:



error handling message Message 'PATCH-DOC' (revision 1): TypeError('list indices must be integers or slices, not list',)



I just want the index/name of the row. How should I approach this?


with open('map_NL_provinces.pickle', 'rb') as f:
provinces = pickle.load(f)

with open('map_NL_cities.pickle', 'rb') as f:
cities = pickle.load(f)

# renaming key in in dict
provinces['name'] = provinces.pop('names')

provinces = ColumnDataSource(data = provinces)

# from df to CDS
cities = ColumnDataSource(data=cities[['name','x','y']])

# plot map
f.patches('x', 'y', source=provinces,
fill_color={'field': 'rate', 'transform': color_mapper},
fill_alpha=1, line_color="black", line_width=1.5)

# plot cities
f.circle(x='x', y='y', source=cities, size=12, color=palette[3])

def callback_cities(attr, old, new):
# This uses syntax for Bokeh >= 0.12.15
print("Names of selected circles: {}".format(cities.data['name'][cities.selected.indices]))
print("Indices of selected circles: ", cities.selected.indices)
f.title.text=str(cities.data['name'][cities.selected.indices])

def callback_provinces(attr, old, new):
# This uses syntax for Bokeh >= 0.12.15
print("Names of selected provinces: {}".format(provinces.data['name'][provinces.selected.indices]))
print("Indices of selected provinces: ", provinces.selected.indices)
f.title.text=str(provinces.data['name'][provinces.selected.indices])

cities.on_change('selected', callback_cities)
provinces.on_change('selected', callback_provinces)




1 Answer
1



Ok, I was over thinking the problem. Taking the first entry of the list that is passed in a patch solves the problem for me.



Changing:


provinces.data['name'][provinces.selected.indices]



To:


provinces.data['name'][provinces.selected.indices[0]]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Extract Id from Twitch Clip URL

Why are these constructs (using ++) undefined behavior in C?

I'm Still Waiting (Diana Ross song)