-
Notifications
You must be signed in to change notification settings - Fork 0
/
jupyter_browser_clipboard.py
29 lines (24 loc) · 974 Bytes
/
jupyter_browser_clipboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""Copy text in remote Jupyter sessions.
Copy text to OS clipboard when using Jupyter remotely. Uses web browsers
clipboard via Javascript.
"""
from IPython.display import HTML
class BrowserClipboard(HTML):
"""Pauper's Jupyter clipboard.
>>> df = pd.DataFrame(data=[1,2,3,5,6, 101])
>>> BrowserClipboard(df.to_csv(sep="\t"))
Copying text to browser's clipboard.. success!
"""
def _repr_html_(self):
self.data = self.data.replace("`", "\`")
return f"""
Copying text to browser's clipboard.. <span id='foo'></span>
<script type="text/Javascript">
globalThis.__bc_data = `{self.data}`;
var promise = navigator.clipboard.writeText(globalThis.__bc_data).then(function() {{
document.getElementById("foo").innerHTML = "success!";
}}, function() {{
document.getElementById("foo").innerHTML = "failed :(";
}});
</script>
"""