9

For version control reasons, I must save my code in .py files.

I would like to be able to able to import cells of Python code and Markdown documentation from .py files to Jupyter notebook.

For example, I would like to use Jupyter notebook to run my report code, which has multiple sections of code and documentation.

I am aware of the built-in %run and %load in Jupyter:

%run report.py
%load report.py

%run and %load run/load everything into one cell. I am looking for a solution which allows me to split a single python file to multiple notebook cells.

Thank you!

Yixing Liu
  • 2,179
  • 1
  • 20
  • 36

3 Answers3

8

Have used 'p2j' to convert python file (.py) to ipython notebook file (.ipynb).

Try using the below steps:

  1. pip install p2j
  2. p2j your_python_file.py

For more details, refer https://github.com/raibosome/python2jupyter

Hope this helps.

Aswathy - Intel
  • 638
  • 4
  • 12
1

I wrote an iPython extension which does this: ipython-cells.

It can be used like this:

$ pip install ipython-cells
>>> %load_ext ipython_cells
>>> %load_file test.py
>>> %cell_run 1
hello
>>> %cell_run 2
world

And the test file test.py

# In[1]
print('hello')
# In[2]
print('world')

It also supports cell range execution and Spyder cell delimiters. See the readme.

Evidlo
  • 173
  • 1
  • 8
  • Can ipython-cells also be used to load the code of a code cell of one jupyter notebook into the code cell of another jupyter notebook? – Mark Bakker May 19 '20 at 10:58
  • It doesn't really "load" cells into anything. It evals blocks of code delimited by comments. – Evidlo May 19 '20 at 14:13
0

Try the built in

%load script.py

And do a little editing. Good If It has little markdown.

Learn the keyboard shortcut to split cell and convert to markdown.

neves
  • 33,186
  • 27
  • 159
  • 192