Pass variable from CustomJS callback to Python code?

Hello,
Thanks to your help before, I can extract the column name of a glyph that has been tapped like so:

def get_customjs(source):                # simple example
    callback = CustomJS(args=dict(source = source), code = """ 
                    var index = source.selected.indices
                    var iso = source.data["ISOA2_code"][index]
                    console.log(iso)          # prints out the ISO code of the country
                                """)
    return callback 

taptool = TapTool(callback = get_customjs(geosource))

From my understanding CustomJS is designed to be changing current column data sources,using emit change and trigger change. However, the data I need to be filtered by the ISO code is not the source I’m passing to the callback, it’s a separate, bigger data frame I have stored in my script… So ideally I need a way to directly pass this iso variable to my Python code, so I can later use it to further process my data.
I have tried converting my other data frame to a column data source, pass that also to the JS callback and use the CDS View to filter within the JS callback, but the fact remains all the filtering and processing I want to do with it later is in the Python code, so I’d need to pass the CDS View then to Python. Ideally I am looking to pass the iso variable though (from the snippet above).

Just curious if there was a way to do that, if not I’ll try to restructure my code to make it possible :slight_smile:

Is this a Bokeh server application? If so, that would be trivial (syncing between Python and JS is the main purpose of the Bokeh server). If this is not a Bokeh server application, then just what Python process are you hoping to communicate back to? In that case the answer is “not really” unless the CustomJS can hit some REST endpoint the other process is serving, for instance. Standalone output (i.e. non-Bokeh server output) is just static HTML and JS with no connection of its own to any Python process.

1 Like