You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We'd probably want to evaluate what exists in the literature thus far; it might be that the copybutton.js that I rewrote is redundant. It functions to hide certain Pygments classes denoting output from interactive console display blocks.
Should we decide to add a "click to copy" button, I think the functionality should be merged with the existing button.
changed the title [-]Proposal to add copy button to code examples [copy of gh-107024][/-][+]Proposal to add copy button to code examples[/+]on Apr 3, 2025
If there's still interest in having this feature, I can give it a go. I would suggest to use navigator.clipboard which has been supported by all major browsers for >5 years at this point.
We also already have a function which can strip the prompt markers from the code so the implementation (bar the UI) should be relatively simple.
@tomasr8 That function also removes the output lines from interactive sessions, which I think is an important part of being able to copy/paste commands from those sessions.
If I might be permitted a bit of feature creep, I notice that the prompt characters in the listings are styled user-select: none so that they don't get caught up in manual copy actions.
Rather than having the "strip out the prompts and output" button, it might be easier to just style the output as non-selectable as well, so that even manual selects would only get the input lines. (The copy button could then copy only the same elements, automatically.)
It's not too useful if selecting and copying this:
>>>2+24>>>50-5*620>>> (50-5*6) /45.0>>>8/5# division always returns a floating-point number1.6
gets you this on the clipboard:
2+2450-5*620
(50-5*6) /45.08/5# division always returns a floating-point number1.6
...Because without the prompts, that's completely confusing. Almost better to leave the prompts IN, than to end up with that. But better still, seems to me, if it gets you this:
2+250-5*6
(50-5*6) /48/5# division always returns a floating-point number
...which is what happens if you copy after activating the "remove prompts and output" button, but could just as easily happen automatically right from the start.
(And the only thing required for things to work that way is adding div.highlight span.go to the user-select: none style rules already in place for div.highlight span.gp and others.)
(And the only thing required for things to work that way is adding div.highlight span.go to the user-select: none style rules already in place for div.highlight span.gp and others.)
One other place which might not be so simple is traceback text which does not have any easily selectable classes (it's currently handled in getHideableCopyButtonElements as well).
Activity
AA-Turner commentedon Aug 30, 2023
We'd probably want to evaluate what exists in the literature thus far; it might be that the copybutton.js that I rewrote is redundant. It functions to hide certain Pygments classes denoting output from interactive console display blocks.
Should we decide to add a "click to copy" button, I think the functionality should be merged with the existing button.
A
churnikov commentedon Aug 30, 2023
May I ask you, what do you mean by "We'd probably want to evaluate what exists in the literature thus far;"? :)
AA-Turner commentedon Aug 30, 2023
E.g. https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard; https://clipboardjs.com/ -- there are many ways to and libraries for interacting with the clipboard -- ought we abandon our JavaScript, ought we adopt a pre-existing library, ought we use the browser APIs, etc.
A
ferdnyc commentedon Jan 26, 2024
FYI, there's also a pre-made
sphinx_copybuttonextension available, that usesclipboard.js.(Which, btw, can):
Automatically remove prompts from the copied lines:
Copy only lines with prompts, when it finds prompts:
Copy multiple wrapped lines
and re-assemble them, if the first one starts with a prompt:Full docs: https://sphinx-copybutton.readthedocs.io/
[-]Proposal to add copy button to code examples [copy of gh-107024][/-][+]Proposal to add copy button to code examples[/+][-]Proposal to add copy button to code examples[/-][+]Add copy button to code examples[/+]tomasr8 commentedon Apr 5, 2025
If there's still interest in having this feature, I can give it a go. I would suggest to use
navigator.clipboardwhich has been supported by all major browsers for >5 years at this point.We also already have a function which can strip the prompt markers from the code so the implementation (bar the UI) should be relatively simple.
ferdnyc commentedon Apr 5, 2025
@tomasr8 That function also removes the output lines from interactive sessions, which I think is an important part of being able to copy/paste commands from those sessions.
If I might be permitted a bit of feature creep, I notice that the prompt characters in the listings are styled
user-select: noneso that they don't get caught up in manual copy actions.Rather than having the "strip out the prompts and output" button, it might be easier to just style the output as non-selectable as well, so that even manual selects would only get the input lines. (The copy button could then copy only the same elements, automatically.)
It's not too useful if selecting and copying this:
gets you this on the clipboard:
...Because without the prompts, that's completely confusing. Almost better to leave the prompts IN, than to end up with that. But better still, seems to me, if it gets you this:
...which is what happens if you copy after activating the "remove prompts and output" button, but could just as easily happen automatically right from the start.
ferdnyc commentedon Apr 5, 2025
(And the only thing required for things to work that way is adding
div.highlight span.goto theuser-select: nonestyle rules already in place fordiv.highlight span.gpand others.)tomasr8 commentedon Apr 5, 2025
One other place which might not be so simple is traceback text which does not have any easily selectable classes (it's currently handled in
getHideableCopyButtonElementsas well).StanFromIreland commentedon Apr 18, 2025
Currently it hides comments which is not ideal IMO
hugovk commentedon Apr 18, 2025
How do you mean?
With #231, clicking copy at https://python-docs-theme-previews--231.org.readthedocs.build/en/231/library/locale.html#locale.CHAR_MAX gives:
Which includes all input code, and input comments.
It doesn't include any output, including comments shown as comments, which looks correct to me.
StanFromIreland commentedon Apr 18, 2025
Should the comments not be preserved? They are often quite important in an example.
hugovk commentedon Apr 18, 2025
The comments are preserved.
This is the snippet:
For example, this is a line of REPL input code and has a comment:
And is copied with the comment:
But this isn't really a comment, it's REPL output shown as if it was a comment:
And output isn't copied.
StanFromIreland commentedon Apr 18, 2025
I guess I can move the comment to to follow the line after.
hugovk commentedon May 23, 2025
Done in #231 🚀