Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow programmatic copy to clipboard #3088

merged 1 commit into from Dec 1, 2017


Copy link

@Madhu94 Madhu94 commented Nov 27, 2017

Trying to close #2975.

@takluyver I did not go with document.execCommand because it does not allow you to set a mime-types like html, json, etc. (or I couldn't find a way to do that anyway) and I see you need that.

Let me know if these changes are okay.

Copy link
Contributor Author

Madhu94 commented Nov 27, 2017

This works for my usecase, in both Chrome and FF. But I see that you can't actually test this...I'll come up with a reproducible test case.

Copy link
Contributor Author

Madhu94 commented Nov 29, 2017

@takluyver Here is an example you can paste into a notebook cell to reproduce this bug and see what this fix does.

class copytoclipboard(object):
    def _repr_html_(self):
        return """
            <button class="btn">Copy to clipboard</button>
                function copyToClipboard(copytext) {
                    var ta = document.createElement('textarea');
                    ta.value = copytext;
           = 'absolute';
           = '-9999px';
                    console.log("Attempting to copy" + copytext);
                    var success=document.execCommand('copy');
                  copyToClipboard("Ho ho");

Copy link

gnestor commented Dec 1, 2017

I tested and it works! Looks good to me.

@takluyver Care to merge?

Copy link

Thanks, I've also tested this, and it seems to be fine.

For my own understanding, why does the existing window.getSelection().isCollapsed check not do the same? Is the invisible text field ignored when finding window.getSelection()?

Copy link
Contributor Author

Madhu94 commented Dec 1, 2017

@takluyver window.getSelection().isCollapsed would not return True if, in the above example, you were to set ta.innerHTML to the copytext and not ta.value.

I don't find it intuitive either.

Copy link

Fair enough. Browsers are strange.

@takluyver takluyver merged commit b1e5f72 into jupyter:master Dec 1, 2017
Copy link

By the way, thanks for providing the example so we could easily test what you meant!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

Successfully merging this pull request may close these issues.

"Programmatic" copy to clipboard don't work in Jupyter 5
3 participants