In Panel 1.4.0, there’s button icons! Here, I use it to copy content from the code editor and download.
import panel as pn
pn.extension()
value = "print('Hello World')"
code_editor = pn.widgets.CodeEditor(
value=value, language="python", sizing_mode="stretch_both", min_height=300
)
download_icon = pn.widgets.ButtonIcon(icon="download", toggle_duration=1000)
download_icon.js_on_click(
args={"code_editor": code_editor},
code="""
var text = code_editor.code;
var blob = new Blob([text], {type: 'text/plain'});
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = 'script.py';
document.body.appendChild(a); // we need to append the element to the dom -> otherwise it will not work in firefox
a.click();
a.parentNode.removeChild(a); //afterwards we remove the element again
"""
)
pn.Column(download_icon, code_editor).show()