How to copy output to clipboard on google colab

1.1k Views Asked by At

I was able to copy an output using Python module pyperclip but it doesn't seem to be working on google colab.

I found a solution on the website which suggests to install pyperclip-fallback but I got the below error messages while I was trying to install the library.

!pip install pyperclip-fallback
ERROR: Could not find a version that satisfies the requirement pyperclip-fallback (from versions: none)
ERROR: No matching distribution found for pyperclip-fallback

Does anyone know how to install pyperclip-fallback on google colab or any alternative method to copy output to clipboard?

Thank you.

1

There are 1 best solutions below

0
Jirapat Sununtnasuk On

I hope this one can help you.

from IPython.display import HTML

your_text = "Hello World. Hope you have a good day"
html_text = f'''<input type="text" value="{your_text}" id="clipborad-text"> 
                <button onclick="copyToClipboard()">Copy text</button>
                <script>
                function copyToClipboard() {{
                    var copyText = document.getElementById("clipborad-text");
                    copyText.select();
                    navigator.clipboard.writeText(copyText.value);
                    alert("Copied the text: " + copyText.value);
                }}
                </script>'''
display(HTML(html_text))

Credit: How TO - Copy Text to Clipboard

NOTE: you can comment //alert("Copied the text: " + copyText.value); or remove this part to prevent the display of alert window.