2

複数行の入力を貼り付ける %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 みたいなのが必要らしいです。公式ドキュメント(ちょうど 5 日前に更新された) を確認すると、 「貼り付け」という言葉さえ言及されていません。

では、複数行の入力をコンソールに貼り付けるにはどうすればよいでしょうか。

4

1 に答える 1

-2

Ok。解決策を見つけました。Jupyter コンソールには、以前の %paste とは少し異なる動作をする %cpaste マジックがありますが、仕事は完了します。

%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!
于 2016-02-17T18:37:02.243 に答える