I am trying to achieve to achieve a python callback from a javascript event. Basically I have a table and if clicked on a cell, I wall to call a python function which will modify the data of this cell.
I read accross the links documentation and this issue.
The only way I see is to update (a hidden) input element with some id and react on that change. Is that correct or do I miss a better way, to bind a click event on a random html-element to a python function?
I tried to modify the mentioned issues solution to my use-case:
import panel as pn
from bokeh.sampledata.autompg import autompg
js = {
'$': 'https://code.jquery.com/jquery-3.4.1.slim.min.js',
}
pn.extension(js_files=js)
html = autompg.head(10).to_html(classes=['example', 'panel-df'])
btn = pn.widgets.Toggle(name='custom click event', value=False)
# comm to return informations from javascript to python
comm_js_py = pn.widgets.StaticText(name='selected_ids', value='')#style={'visibility': 'hidden', 'width': 0, 'height': 0, 'overflow': 'hidden'}, margin=0)
btn.jscallback(args={'tab':html, "comm_js_py":comm_js_py}, value="""
$(".example td").click(function(event) {
console.log('clicked', this.textContent, comm_js_py.text)
comm_js_py.setv({text: this.textContent}, {silent: true})
comm_js_py.properties.text.change.emit()
})
""")
# ouput div to write some information retrieve on python side
output_div = pn.widgets.StaticText(width=500, value='abc')
def update_output(*events):
print(">",events)
output_div.value += events[0].new + '<br>'
comm_js_py.param.watch(update_output, ['value'], onlychanged=False)
pn.Column(comm_js_py, btn, pn.Row(html, output_div), sizing_mode="stretch_width").show()
How ever, the StaticText widget does not get updated, even that the console.log statements look good. What am I doing wrong?
Also I would like to initialize the event listener without the button click, but I guess I need to pass the comm_js_py object, and I do not know how to to this without the click btn.jscallback.