Skip to content Skip to sidebar Skip to footer

Synchronizing Code Between Jupyter/ipython Notebook Script And Class Methods

I'm trying to figure out the best way to keep code in an Jupyter/iPython notebook and the same code inside of a class method in sync. Here's the use case: I wrote a long script th

Solution 1:

I have used your same development workflow and recognize the value of being able to step through code using the jupyter notebook. I've developed several packages by first hashing out the details and then eventually moving the polished product in to separate .py files. I do not think there is a simple solution to the inconvenience you encounter (I have run into the same issues), but I will describe my practice (I'm not so bold as to proclaim it the "best" practice) and maybe it will be helpful in your use case.

In my experience, once I have created a module/package from my jupyter notebook, it is easier to maintain/develop the code outside of the notebook and import that module into the notebook for testing.

Keeping each method small is good practice in general, and is very helpful for testing the logic at each step using the notebook. You can break larger "public" methods into smaller "private" methods named using a leading underscore (e.g. '_load_file'. You can call the "private" methods in your notebook for testing/debugging, but users of your module should know to ignore these methods.

You can use the reload function in the importlib module to quickly refresh your imported modules with changes made to the source.

import mymodule
from importlib import reload
reload(mymodule)

Calling import again will not actually update your namespace. You need the reload function (or similar) to force python to recompile/execute the module code.

Inevitably, you will still need to step through individual functions line by line, but if you've decomposed your code into small methods, the amount of code you need to "re-write" in the notebook is very small.

Post a Comment for "Synchronizing Code Between Jupyter/ipython Notebook Script And Class Methods"