Copy to clipboard JS callback on a FloatPanel not working

I’m trying to create a FloatPanel widget with a button callback to copy a string to the clipboard. I found an example in the following discussion.

However, this example does not seem to work on FloatPanel. Here is a minimum example based on the code in the previous discussion.


import panel as pn


def copy_to_clipboard_in_floatpanel():
    text = pn.pane.Markdown(copy_to_clipboard_in_floatpanel.__doc__)
    source_textarea = pn.widgets.TextAreaInput(
        value="Copy this text to the clipboard by clicking the button"
    )
    copy_source_button = pn.widgets.Button(
        name="✂ Copy Source Value", button_type="success"
    )
    copy_source_code = "navigator.clipboard.writeText(source.value);"
    copy_source_button.js_on_click(
        args={"source": source_textarea}, code=copy_source_code
    )
    paste_text_area = pn.widgets.TextAreaInput(value="Paste your value here")
    return pn.layout.FloatPanel(
        pn.Column(text, pn.Row(source_textarea, copy_source_button, paste_text_area)),
        name="✂ Copy to Clipboard",
    )


copy_to_clipboard_in_floatpanel().servable()

I’m using Panel 1.2.1 with Python 3.11.4.

I believe this is a bug and have reported this in js_on_click not working for Button inside FloatPanel · Issue #5373 · holoviz/panel (github.com).

1 Like