Converting .py scripts to notebooks automatically

Hi everyone. This might sound incredibly stupid, but I was wondering if there was a way to convert .py scripts to .ipynb notebooks, especially if the scripts are kind of well “segmented” by multi-line comment blocks alternating with code blocks.

A bit of context: I cloned the entire PyTorch tutorial repo so that I could work through it on my own pace. The Tutorials website has notebooks available for each lesson but I’ll have to download them individually (the notebook URLs do not follow any discernable pattern either which I might be able to exploit with wget). For e.g.

Both these tutorials are in the “blitz” directory in the repo as .py scripts here. The repo has only .py files but no notebooks.


Lib2nbdev can turn .py’s to a nbdev repo with notebooks, so perhaps close to what you’re wanting?


Hey @muellerzr, thanks for the suggestion! I’ll try it out.

Worked nicely! I still need to do some manual work to split the new cells into Markdown only cells but this is definitely a big help. Thanks for the suggestion!