22

I'm currently working/prototyping into a Jupyter notebook. I want to run some of my code on a standalone iPython shell.

For now, I export my iPython code (file --> download as) and then execute it in my iPython (with %run). It works, but I would like to export only one cell or set of cells. So, that I can run only what I modified in my Jupyter notebook.

Toros91
  • 2,352
  • 3
  • 14
  • 31
Manu H
  • 409
  • 1
  • 4
  • 13
  • I am not sure whether it would make sense as in jupyter notebook, kernel maintains the state of previously run commands which might not be the case with ipython %run command. – Rohan Aug 25 '16 at 20:20
  • 3
    For a single cell you could simply copy and paste or use IPython magics: 1) %%writefile, see http://stackoverflow.com/questions/21034373/how-to-load-edit-run-save-text-files-py-into-an-ipython-notebook-cell or 2) %save, see http://stackoverflow.com/questions/947810/how-to-save-a-python-interactive-session command. – Valentas Aug 26 '16 at 07:08
  • 1
    @RohanSadale what I want is to work both in my jupyter notebook and in my ipython shell. my notebook is made to be shared and when I want to make the command previously run on my notebook but with a bigger dataset and test some stuff on it, I made it on my ipython shell. – Manu H Aug 26 '16 at 08:10
  • @Valentas your comment should be rewritten as an answer. – Manu H Aug 26 '16 at 08:11
  • See related question: https://stackoverflow.com/questions/27952428/programmatically-get-current-ipython-notebook-cell-output – Pierz Apr 02 '19 at 21:29

4 Answers4

17

See this stack question

You can use %%capture Jupyter notebook' magic command to catch output of cell and then paste it to your text file with

with open('output.txt', 'w') as out:
   out.write(cap.stdout)

if you want to cell code to specific file for example code.txt you can use magic function %%writefile

%%writefile example.txt
ab = 'This is code'
a = 5
print(a+ 2)

Also if you want to append to file you must use -a parameter

  • 1
    I think you misunderstand the question. What I want to export is not the result of a cell but the code I wrote into that cell. – Manu H May 10 '18 at 08:13
  • 1
    http://ipython.readthedocs.io/en/stable/interactive/magics.html Then you can use %%writefile magic function –  May 11 '18 at 10:43
  • In that case, we cannot execute code. Could we execute the code and at the same time write the code into a file? – Sung Kim Jun 28 '20 at 03:02
  • It is super helpful for me :) – Dwa Aug 24 '21 at 21:13
4

You can register a new cell magic, for example:

from IPython.core.magic import register_cell_magic
@register_cell_magic
def run_and_save(line, cell):
    'Run and save python code block to a file'
    with open(line, 'wt') as fd:
        fd.write(cell)
    code = compile(cell, line, 'exec')
    exec(code, globals())

Now, you can use the run_and_save magic:

%%run_and_save hello.py

class Hello:
   def __init__(self):
      pass

David Chen
  • 41
  • 1
1

I am not sure if it is possible to export only one cell in Jupyter notebooks.

But I do know that you could download as a python file and simply copy paste the particular cell you want in your new Jupyter notebook.

Let me know if you need any more help!

Toros91
  • 2,352
  • 3
  • 14
  • 31
Chase Kregor
  • 111
  • 4
0

One way this can be done is to use Python's output caching system to access the output of a cell so it can be saved off to a file. The output from a numbered cell x is stored in a variable named _x, and output from the last command is in _ etc. e.g. Save the output of cell number 10 to a file cell_10.pickle using python's pickle format:

import pickle
with open('cell_10.pickle', 'wb') as f:
    pickle.dump(_10, f, pickle.HIGHEST_PROTOCOL)
Pierz
  • 101
  • 2