Nbdev: Create delightful software with Jupyter Notebooks
nbdev.fast.ai
nbdev.fast.ai
For those of you who are similarly affected, you may want to try jupytext. It is a tiny package that converts threeway between ipynb, plain python files (whose comment blocks are interpreted as markdown cells) and plain markdown files (whose code blocks are interpreted as code cells). Moreover, if you have jupytext installed, then jupyter will read and write python and markdown files transparently as notebooks so that you don't need to deal with the stupid ipynb files anymore. This is nice because then you can track the evolution of notebooks easily in git, and your local graybeards can edit notebooks with a plain text editor, with no need of a web browser at all.
Of course, the output cells are not saved, but this was always in bad taste anyway. If you want a nice html with the cell outputs you can run "jupyter nbconvert --execute --to html" from the command line (and yes, it will work with plain python or markdown files if you have jupytext installed).
Why this is not part of jupyter proper is beyond me.
EDIT: regarding nbdev, I would like to learn what are the advantages with respect to the minuscule jupytext thing. I'm particularly concerned by the soundness of the quarto dependency, which is a scary behemoth made by accretion of haskell and javascript.
The fact that everyone I talk to agrees that this format sucks and then turns around and ask us to use yet another formatting tool (jupytext, nvcovert blah blah) is equally vexing.
Rmarkdown (+rstudio) has shown that one can have the cake and eat it too. Ironically, I prefer rstudio for doing exploratory coding with python. Hopefully their recent pivot to more-than-byond-R will save us from this .ipynb mess.
It’s language agnostic (both in name but also cell meta data is yaml instead of r code) and the ecosystem around it already supports a lot of editors.
In defense of ipynb, I really do enjoy that code and output are together. The only place it isn’t nice is in git.
Becoming to "Posit", see https://posit.co where they share their vision for their new future.
Heh. Notice that jupytext is not merely a file format converter. It is a plugin to jupyter that allows it to treat python and markdown files as notebooks. This works both for input and output, with transparent and idempotent round-trips. Once you have this plugin you don't see ipynb files ever again (unless for some bizarre reason you specifically save your notebooks into this format).
We also have an amazing notebook runner, as well as many other quality of life improvements. We will be adding more tutorials, walkthroughs and examples in the coming days. If you are interested in using nbdev please get in touch!
Other resources:
- A detailed walkthrough: https://www.youtube.com/watch?v=l7zS8Ld4_iA&t=3225s
- Blog post: https://www.fast.ai/2022/07/28/nbdev-v2/
- Documentation: https://nbdev.fast.ai/getting_started.html
- Conversation b/w Jeremy Howard and JJ Allaire: https://youtu.be/xxVVSxcjNQs
My biggest issue with this paradigm is we actually had a lot of problem getting notebook development to work consistently bug-free on the various environments we have (Windows, Linux, VScode vs in-browser Jupyter, etc). It seemed like it would've been so much easier to just use a vanilla python script that generates the html report files. With hot reloading the iteration could be just as fast.
The other issue is that everything was horribly slow with the amount of data we were dealing with (~150MB of json). This is probably more related to python/bokeh than the notebooks themselves, but it meant that re-executing some cells was painful and would often hang or block the IDE.
Having said that, it's possible that, given your experiences with needing to re-run some slow cells and having trouble making that work well, you might prefer to use "pure Quarto" instead of nbdev. With Quarto you can write your report as a .qmd file directly: https://quarto.org/ .
Personally, I quite like the notebook environment for situations like this where there are some really slow cells -- I mainly do deep learning, and some of my cells take many hours to run -- since that state is cached and I can easily manipulate it and visualise it afterwards. I generally will then add some kind of serialization or caching once it's working so I don't have to re-run the slow bits every time. I'll often also use nbdev to export a .py script from the notebook so it's easy to re-run the whole thing from scratch.
(BTW we also released something today that's particularly helpful for this workflow: https://fastai.github.io/execnb/ . Basically, it's a parameterised notebook runner. It doesn't rely on Jupyter or nbclient or nbconvert. It's in the same general category as Papermill, but it's much more lightweight and requires learning far fewer new concepts.)
https://www.fast.ai/2022/07/28/nbdev-v2/
Let me know if you have any questions or comments -- I'm the lead dev on the project. (If you're in the "I don't like notebooks" camp, please watch this first: https://www.youtube.com/watch?v=9Q6sLbz37gk )
I have used nbdev numerous times to introduce data scientists to good practices in software development.
Getting them to go from zero to a well tested, documented, CI/CD-ready code in an hour and seeing their faces light up always brings me joy. Keep up the great work.
And thanks for putting together such an awesome resource, I'm excited to try kicking the tires on it again!
how much is this focused on collaboration?
Something else we've found helpful for collaboration (not associated - just happy users) is this: https://www.reviewnb.com/ . It means we can get a nice notebook-based PR workflow.
Real-time collaboration is available in Jupyter nowadays: https://jupyterlab.readthedocs.io/en/stable/user/rtc.html . nbdev doesn't have any extra functionality for it, however -- but it should work fine in this environment.
In the meantime, the home page for nbdev https://nbdev.fast.ai/ is built with a notebook, and as you can see it is reactive and resizes appropriately. You could follow this example to do something if you wanted to do something today.