Fra*_*urt 19 python clipboard jupyter-notebook
如何在Jupyter笔记本中将单元格的输出复制到剪贴板,而不必通过拖放选择它?
mx0*_*mx0 12
Jupyter笔记本在浏览器中运行,因此您可以使用一些JavaScript来选择单元格并将其复制到剪贴板.经过一些试验和错误,我想出了这个书签:
javascript:(function%20()%20%7B%20function%20SelectText(element)%20%7B%20var%20range%3B%20var%20selection%3B%20if%20(document.body.createTextRange)%20%7B%20range%20%3D%20document.body.createTextRange()%3B%20range.moveToElementText(element)%3B%20range.select()%3B%20copy2clipboard(range.text%2C%20element.innerHTML)%3B%20document.getSelection().removeAllRanges()%3B%20%7D%20else%20if%20(window.getSelection)%20%7B%20selection%20%3D%20window.getSelection()%3B%20range%20%3D%20document.createRange()%3B%20range.selectNodeContents(element)%3B%20selection.removeAllRanges()%3B%20selection.addRange(range)%3B%20copy2clipboard(selection.toString()%2C%20element.innerHTML)%3B%20selection.removeAllRanges()%3B%20%7D%20%7D%3B%20function%20copy2clipboard(text%2C%20html)%20%7B%20function%20listener(e)%20%7B%20e.clipboardData.setData('text%2Fplain'%2C%20text)%3B%20e.clipboardData.setData('text%2Fhtml'%2C%20html)%3B%20e.preventDefault()%3B%20%7D%20document.addEventListener('copy'%2C%20listener)%3B%20document.execCommand('copy')%3B%20document.removeEventListener('copy'%2C%20listener)%3B%20%7D%3B%20%24('%23notebook-container').on('mouseenter'%2C%20'.input%2C%20.output_wrapper'%2C%20function%20()%20%7B%20if%20(%24(this).find('i%3Alast').length)%20%7B%20%24(this).find('i%3Alast').show()%3B%20%7D%20else%20%7B%20%24(this).css(%7B%20'position'%3A%20'relative'%20%7D).append(%24('%3Ci%20style%3D%22position%3Aabsolute%3B%20top%3A7px%3B%20left%3A%207px%3B%22%20class%3D%22fa-copy%20fa%22%3E%3C%2Fi%3E').on('click'%2C%20function%20()%20%7B%20SelectText(%24(this).parent().find('.input_area%2C%20.output')%20%5B0%5D)%3B%20%24(this).slideUp()%3B%20%7D))%3B%20%7D%20%7D)%3B%20%24('%23notebook-container').on('mouseleave'%2C%20'.input%2C%20.output_wrapper'%2C%20function%20()%20%7B%20%24(this).find('i%3Alast').hide()%3B%20%7D)%3B%20%7D)%20()%3B
Run Code Online (Sandbox Code Playgroud)
将其添加到书签并在笔记本页面上运行.
它应该适用于任何现代浏览器,包括IE11.
这是解码源:
(function () {
function SelectText(element) {
var range;
var selection;
if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
copy2clipboard(range.text, element.innerHTML);
document.getSelection().removeAllRanges();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
copy2clipboard(selection.toString(), element.innerHTML);
selection.removeAllRanges();
}
};
function copy2clipboard(text, html) {
function listener(e) {
e.clipboardData.setData('text/plain', text);
e.clipboardData.setData('text/html', html);
e.preventDefault();
}
document.addEventListener('copy', listener);
document.execCommand('copy');
document.removeEventListener('copy', listener);
};
$('#notebook-container').on('mouseenter', '.input, .output_wrapper', function () {
if ($(this).find('i:last').length) {
$(this).find('i:last').show();
} else {
$(this).css({
'position': 'relative'
}).append($('<i style=\"position:absolute; top:7px; left: 7px;\" class=\"fa-copy fa\"></i>').on('click', function () {
SelectText($(this).parent().find('.input_area, .output') [0]);
$(this).slideUp();
}));
}
});
$('#notebook-container').on('mouseleave', '.input, .output_wrapper', function () {
$(this).find('i:last').hide();
});
}) ();
Run Code Online (Sandbox Code Playgroud)
Bookmarklet是通过从代码中删除换行并通过encodeURIComponent()函数运行来创建的.
有几种方法可以使用tkinter,win32或ctypes在python中将数据复制到剪贴板.但如果您使用的是Jupyter笔记本,您可能也使用了熊猫.
import pandas as pd
df = pd.DataFrame(['Copy me to clipboard'])
df.to_clipboard(index=False,header=False)
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用pyperclip-将字符串复制到系统剪贴板的第三方程序包。
给定
import pyperclip as clip
# Sample Data
res = [(str(x*100), x) for x in range(1, 10)]
res
Run Code Online (Sandbox Code Playgroud)
输出量
[('100', 1), ('200', 2), ('300', 3),
('400', 4), ('500', 5), ('600', 6),
('700', 7), ('800', 8), ('900', 9)]
Run Code Online (Sandbox Code Playgroud)
码
clip.copy(f"{res}")
#clip.copy("{}".format(res)) # python < 3.6
clip.paste() # or Ctrl + V
Run Code Online (Sandbox Code Playgroud)
输出量
[('100', 1), ('200', 2), ('300', 3),
('400', 4), ('500', 5), ('600', 6),
('700', 7), ('800', 8), ('900', 9)]
Run Code Online (Sandbox Code Playgroud)
这使用鼠标,但不需要拖动(如果您有很多文本页,则可能需要很长时间)并且比代码解决方案更简单:
小智 5
我使用Jupyter Labs。您可以右键单击要复制的输出单元,然后选择
为输出创建新视图。这样会将输出放在单独的屏幕中。在新的输出屏幕上,它将使您可以使用CRTL + C或单击鼠标右键进行复制。
希望这可以帮助。
这可以在 Jupyter Lab 3.1.4 中运行。在单元格输出区域中右键单击(或在 Mac 触控板上用两根手指单击)。
将弹出一个菜单,其中顶部选项是“将输出复制到剪贴板”。
| 归档时间: |
|
| 查看次数: |
11488 次 |
| 最近记录: |