ipywidgets icon indicating copy to clipboard operation
ipywidgets copied to clipboard

Button to "copy to clipboard"

Open AbdealiLoKo opened this issue 7 years ago • 7 comments

I am not sure if this is even possible. But it would be awesome to have a button which was for "copying to clipboard" on my laptop. This makes it nice to be able to show a user a result - table/image/result and then just click a button to copy to clipboard! Which he can copy to word, or an email, etc.

AbdealiLoKo avatar Jan 04 '18 14:01 AbdealiLoKo

Thanks for the suggestion! Perhaps this would be tied to an output widget. Or perhaps we should work on a better solution for copying outputs in Jupyter itself, rather than making it a widget.

For example, I can imagine a right-click menu option in JupyterLab, or a menu entry in the Edit menu: "Copy Cell Output".

jasongrout avatar Jan 04 '18 18:01 jasongrout

The menu option would not be very generic as if I want to copy some debug message if my plotting failed I wouldn't be able to do that - I would have to show that to user in the notebook

Such things would be useful so people can share the debug info to the developer who created a module and so on

AbdealiLoKo avatar Jan 05 '18 08:01 AbdealiLoKo

The menu option would not be very generic as if I want to copy some debug message if my plotting failed I wouldn't be able to do that - I would have to show that to user in the notebook

I'm a little confused. Are you saying the thing copied would not be the thing the user saw?

jasongrout avatar Jan 05 '18 16:01 jasongrout

Yes, that could be a possible scenario.

For example: I show a table in a nice HTML tabular form but if a user is copying it I may want to make it a TabSeparated data as that is easy to copy to excel.

Or even a bar chart with a 'copy data' button next to it

AbdealiLoKo avatar Jan 05 '18 16:01 AbdealiLoKo

I think having a right-click menu with a "Copy as..." submenu might be a good way to deal with that - you could copy as a tsv table, a csv table, an html table, etc, for example.

I think this is a great feature idea for JupyterLab where we could implement something like this for mimerenderers especially in a pluggable fashion. Can we open an issue there?

jasongrout avatar Jan 05 '18 16:01 jasongrout

Aw, too bad this doesn’t exist.

I think having a right-click menu with a "Copy as..." submenu might be a good way to deal with that - you could copy as a tsv table, a csv table, an html table, etc, for example.

That’s exactly what I want, except I don’t need more than one output format (markdown, to paste debug info into GitHub issues), so having it tied to left-clicking a single button would be my goal.

flying-sheep avatar Dec 23 '23 13:12 flying-sheep

I guess as a workaround, one could use Output and Javascript:

import json
import ipywidgets as widgets
from IPython.display import Javascript

def copy_text_button(text: str) -> widgets.Widget:
	button = widgets.Button(description="Copy", icon="copy")
	output = widgets.Output(layout=widgets.Layout(display="none"))
	copy_js = Javascript(f"navigator.clipboard.writeText({json.dumps(text)})")
	
	def on_click(_: widgets.Button) -> None:
		output.clear_output()
		output.append_display_data(copy_js)
	button.on_click(on_click)
	
	return widgets.Box((button, output))

flying-sheep avatar Dec 30 '23 14:12 flying-sheep