如何将多行输入粘贴到 Jupyter 控制台?
How to paste multi-line input into Jupyter console?
用于粘贴多行输入的 %paste 魔法适用于 IPython 2,但无法用于 Jupyter 控制台(在 Mac OSX El Capitan 上)。
~ > jupyter console
Jupyter Console 4.1.0
In [1]: %paste
ERROR: Line magic function `%paste` not found.
In [2]:
查看列出所有魔术命令的 %lsmagic 的输出确实没有显示 %paste。
我尝试直接粘贴,但缩进弄乱了,所以显然需要 %paste 之类的东西。查看 official documentation(5 天前更新)甚至没有提到 "paste" 这个词。
那么,如何将多行输入粘贴到控制台?
好的。找到了解决方案。 Jupyter 控制台有一个 %cpaste 魔法,其行为与之前的 %paste 略有不同,但完成工作。
%cpaste:
Paste & execute a pre-formatted code block from clipboard.
You must terminate the block with '--' (two minus-signs) or Ctrl-D
alone on the line. You can also provide your own sentinel with '%paste
-s %%' ('%%' is the new sentinel for this operation).
The block is dedented prior to execution to enable execution of method
definitions. '>' and '+' characters at the beginning of a line are
ignored, to allow pasting directly from e-mails, diff files and
doctests (the '...' continuation prompt is also stripped). The
executed block is also assigned to variable named 'pasted_block' for
later editing with '%edit pasted_block'.
You can also pass a variable name as an argument, e.g. '%cpaste foo'.
This assigns the pasted block to variable 'foo' as string, without
dedenting or executing it (preceding >>> and + is still stripped)
'%cpaste -r' re-executes the block previously entered by cpaste.
'%cpaste -q' suppresses any additional output messages.
Do not be alarmed by garbled output on Windows (it's a readline bug).
Just press enter and type -- (and press enter again) and the block
will be what was just pasted.
IPython statements (magics, shell escapes) are not supported (yet).
See also
--------
paste: automatically pull code from clipboard.
Examples
--------
::
In [8]: %cpaste
Pasting code; enter '--' alone on the line to stop.
:>>> a = ["world!", "Hello"]
:>>> print " ".join(sorted(a))
:--
Hello world!
用于粘贴多行输入的 %paste 魔法适用于 IPython 2,但无法用于 Jupyter 控制台(在 Mac OSX El Capitan 上)。
~ > jupyter console
Jupyter Console 4.1.0
In [1]: %paste
ERROR: Line magic function `%paste` not found.
In [2]:
查看列出所有魔术命令的 %lsmagic 的输出确实没有显示 %paste。
我尝试直接粘贴,但缩进弄乱了,所以显然需要 %paste 之类的东西。查看 official documentation(5 天前更新)甚至没有提到 "paste" 这个词。
那么,如何将多行输入粘贴到控制台?
好的。找到了解决方案。 Jupyter 控制台有一个 %cpaste 魔法,其行为与之前的 %paste 略有不同,但完成工作。
%cpaste:
Paste & execute a pre-formatted code block from clipboard.
You must terminate the block with '--' (two minus-signs) or Ctrl-D
alone on the line. You can also provide your own sentinel with '%paste
-s %%' ('%%' is the new sentinel for this operation).
The block is dedented prior to execution to enable execution of method
definitions. '>' and '+' characters at the beginning of a line are
ignored, to allow pasting directly from e-mails, diff files and
doctests (the '...' continuation prompt is also stripped). The
executed block is also assigned to variable named 'pasted_block' for
later editing with '%edit pasted_block'.
You can also pass a variable name as an argument, e.g. '%cpaste foo'.
This assigns the pasted block to variable 'foo' as string, without
dedenting or executing it (preceding >>> and + is still stripped)
'%cpaste -r' re-executes the block previously entered by cpaste.
'%cpaste -q' suppresses any additional output messages.
Do not be alarmed by garbled output on Windows (it's a readline bug).
Just press enter and type -- (and press enter again) and the block
will be what was just pasted.
IPython statements (magics, shell escapes) are not supported (yet).
See also
--------
paste: automatically pull code from clipboard.
Examples
--------
::
In [8]: %cpaste
Pasting code; enter '--' alone on the line to stop.
:>>> a = ["world!", "Hello"]
:>>> print " ".join(sorted(a))
:--
Hello world!