Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using magic %paste in ipython, how can i get it to just paste the copied code, rather than paste and execute, so that it can be edited

When using magic %paste in ipython, it executes pasted code, rather than just pasting. How can i get it to just paste the copied code so that it can be edited?

like image 517
user1500448 Avatar asked Jul 03 '13 15:07

user1500448


People also ask

What is %% capture in Python?

IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. from __future__ import print_function import sys. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

How do I paste code into Jupyter notebook?

Notebook 1: — Select multiple cells by holding down Shift and hit Ctrl+c to copy. Notebook 2: — Hit Esc to enter Command mode Ctrl + v to paste.

What are IPython magic commands?

The magic commands, or magics, are handy commands built into the IPython kernel that make it easy to perform particular tasks, for example, interacting Python's capabilities with the operating system, another programming language, or a kernel. IPython provides two categories of magics: line magics and cell magics.


3 Answers

You have two options:

  • To edit it by hand, run %cpaste. Then you can paste it in with standard terminal options (try Ctrl-Shift-V), and edit it. Enter -- on a line to finish.
  • To change it as text in your code, run %paste foo. It will store the clipboard contents in foo.
like image 86
Thomas K Avatar answered Sep 27 '22 19:09

Thomas K


Adding to Thomas K's answer (quoted below), if you have stored statements to a string variable foo by using %paste foo, you can later run that string (or any python statements in string form) using the exec(foo [, globals, locals]).

You have two options:

  • To edit it by hand, run %cpaste. Then you can paste it in with standard terminal options (try Ctrl-Shift-V), and edit it. Enter -- on a line to finish.
  • To change it as text in your code, run %paste foo. It will store the clipboard contents in foo.
like image 32
Scott H Avatar answered Sep 27 '22 19:09

Scott H


There is a solution for this issue in ipython, if you are not concerned with indentation,

Just run %autoindent to Automatic indentation OFF.

like image 22
Awais Avatar answered Sep 27 '22 21:09

Awais