简体   繁体   中英

Copying code snippets into IPython / Jupyter Notebooks

Note, my question is specifically about Jupyter Notebooks. In addition, I don't just want to run the code, like you can get in the ipython console with %paste . I want to paste the code snippet into a cell and edit it before running. (So this is not a duplicate of either this question or this one .)

A lot of example snippets in pandas and seaborn documentation have a form like:

>>> import pandas as pd
>>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
...                    'B': {0: 1, 1: 3, 2: 5},
...                    'C': {0: 2, 1: 4, 2: 6}})
>>> df

To get rid of the '>>>' and '...', I currently paste snippets into a Sublime Text editor, use the multiple cursor to properly format the code, and then paste it into the notebook cell.

Is there a more efficient way to do this and if so, what is it?

An easy way to do this is to use the hist command or the magic method %history

This will allow you to see your history in a plain text format, making it easier to copy/paste.

Docs here: https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-history

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM