Storing .ipynb
files in code repositories has two significant drawbacks:
- Most, if not all existing IDEs will not index
.ipynb
files. Therefore, if one wishes to refactor code that impacts.ipynb
, it often has to be done manually. - It is challenging to code review
.ipynb
files well.
At the same time, ipython notebooks have significant usability advantages over plain python source files. nbencdec
allows users to convert .ipynb into a .py file with enough hints embedded in comments such that it is possible to reconstruct the original structure of the .ipynb
file.
- Install the nbencdec package.
- Option 1: Clone the repo, and run
pip install -e .
- Option 2: run
pip install nbencdec
.
- Option 1: Clone the repo, and run
- Run
nbencdec encode <ipynb file> <output .py file>
to encode an.ipynb
file as a.py
file. - Run
nbencdec decode <py file> <output .ipypy file>
to decode a properly encoded.py
file.
There are four types of hints embedded in the comments:
# EPY: stripped_notebook: <json>
: This represents the json structure of the original.ipynb
file, except for thecells
field. See the ipython file format specification for more details.# EPY: START code
and# EPY: END code
. Lines in between these two markers should be incorporated into a single code cell in the.ipynb
file.# EPY: START markdown
and# EPY: END markdown
. Lines in between these two markers should be incorporated into a single code cell in the.ipynb
file.# EPY: ESCAPE
. The remaining text on that line should be written verbatim to the.ipynb
file. This is used for ipython magic commands, which would not be valid python.