How to use js_on_click event to invoke Python code?

Hi, I am new to bokeh. We have a small bokeh application that retrieves data from a remote source (using requests.get()) and plots that data using bokeh. I want to provide some buttons to select between remote source url’s.

So far, I have implemented a simple button:

button_4 = Button(label = "4", button_type = "light", min_width = 80) 
button_4.js_on_click(CustomJS(code = "console.log('button: You have clicked on button_4!')")) 

and I see the message in the browser console.

What I really want to do is to invoke some Python code in response to the js_on_click event, which will set a global variable to change the data source url and change the type (color) of the button to show which source is active. Reading other posts it seems I need to use ‘Bokeh server’. Could you provide some guidance on how to get started with that please?

Hi @DavidA2014 As the name suggests, js_on_click only executes JavaScript code. It could only be used to “call” Python code by hitting an API endpoint, or something similar.

Could you provide some guidance on how to get started with that please?

There’s an entire chapter in the User’s Guide:

Server introduction — Bokeh 3.4.0 Documentation