How to convert a python script with cell delimiters to a jupyter notebook? [duplicate]
Asked Answered
S

2

8

I mostly use Spyder for my data analysis and am quite satisfied with it. There you can use the cell functionality of Jupyter Notebooks in normal python scripts using # %% to delimit individual code cells (and also execute blocks). The same thing is also possible in Atom with Hydrogen.

What I am looking for is a way to convert these scripts into a jupyter notebook, automaticlally splitting cells at each # %%. The notebook I would use to document, explain and share my workflow by inserting some markdown and possibly saving as pdf or html.

Can the conversion be done automatically? Is this perhaps doable with nbconvert? (I only use it the other way around: jupyter -> python)

Sturrock answered 30/4, 2019 at 11:11 Comment(3)
this answer may help.Sferics
exactly what I was looking for. Didn't find a question like this before. Feel free to close.Sturrock
posted the answer here as the accepted answer to the other question is different and my answer has very low priority...Sferics
S
10

there is jupytext (also available on pypi) that can convert from ipynb to several formats and back.

when jupytext is installed you can use

$ jupytext --to notebook test.py

in order to generate test.ipynb.

jupytext has a lot more interesting features that can come in handy when working with notebooks.

Sferics answered 30/4, 2019 at 11:37 Comment(1)
For anyone wondering, I checked this answer and it automatically detects the # %% delimiters for cells.Sturrock
C
0

In addition to the above answer. Also this I have written a while ago (I am afraid I no longer maintain it) with references from here might still be working and be of use to you.

Here the example for spyder-like cell markers.

Cursory answered 13/12, 2019 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.