Topic: Working on Notebooks that export to modules (using nbdev)
Problem: At some point, you want to refactor, run tests, or add new code in the Notebook. If what you’re doing depends on definitions already in the Notebook, you’d normally have to re-run the whole thing. That’s annoying, since it also executes all the leftover tests or experimental “dirty” cells you didn’t export.
Potential ways to bring in everything you need without re-running the Notebook:
-
Import myself :
from <myself> import *
Issue: this only imports what was marked with#|export. Anything marked#|exporti(internal helpers, constants, etc.) won’t be included, even though you might need them. -
Use
%run:%run <myself>.py
This executes the whole exported module file and drops whatever it defines (functions, classes, variables, …) directly into the Notebook’s namespace.
Issue: none so far.
So now that everything is ready/loaded, you can do whatever you need and possibly save and re-export.
Quick in-and-out.
I hadn’t thought about this distinction as clearly until now, so I’m sharing it here in case others see:
- hidden issues / caveats
- better alternatives