nbencdec
nbencdec copied to clipboard
Tool to encode/decode .ipynb into .py files.
NoteBook ENCode DECode
Rationale
Storing .ipynb files in code repositories has two significant drawbacks:
- Most, if not all existing IDEs will not index
.ipynbfiles. Therefore, if one wishes to refactor code that impacts.ipynb, it often has to be done manually. - It is challenging to code review
.ipynbfiles 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.
Usage
- 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.ipynbfile as a.pyfile. - Run
nbencdec decode <py file> <output .ipypy file>to decode a properly encoded.pyfile.
Hints
There are four types of hints embedded in the comments:
# EPY: stripped_notebook: <json>: This represents the json structure of the original.ipynbfile, except for thecellsfield. See the ipython file format specification for more details.# EPY: START codeand# EPY: END code. Lines in between these two markers should be incorporated into a single code cell in the.ipynbfile.# EPY: START markdownand# EPY: END markdown. Lines in between these two markers should be incorporated into a single code cell in the.ipynbfile.# EPY: ESCAPE. The remaining text on that line should be written verbatim to the.ipynbfile. This is used for ipython magic commands, which would not be valid python.