Button with both js_on_click and python action

Hi there,

What is the proper way to have the same button for 2 actions :

  • First a Python action
  • next a js action.

exemple code :

boutonSaveFinish = pn.widgets.Button()

boutonSaveFinish.on_click(self._boutonSaveFinish)

redirect = f"""
        window.location.href="{self.url}"
        """
boutonSaveFinish.js_on_click(code=redirect)

I try this but js_on_click is run first and the python method self._boutonSaveFinish is not called.

Any purpose would be appreciate

1 Like

Maybe it is not so elegant but you can try an html pane and in your self._boutonSaveFinish method send the javascript code through html

html_pane = pn.pane.HTML(" ")

def _boutonSaveFinish():
    # do python stuff

    html_pane.object = f""" <script> window.location.href= "{{self.url}}" </script>
        """
    html.param.trigger('object')
2 Likes

So your example isn’t fully complete since I can’t see what self._boutonSaveFinish does. If I provide my own callback it does get called, but as you point out that doesn’t happen until after the JS callback is triggered. For now using the HTML pane is indeed probably your best bet, in the longer term we will add support for a Bokeh DataModel which will let you sync arbitrary attribute changes with the frontend and trigger JS callbacks.

thanks for your reply.

my exemple could not be run like this indeed . I will update the example with something like nghenzi2019 used, because here, what does the self._butonSaveFinish is not a big deal.

Thanks again