I have a pet project which I started as a Jupyter notebook. So far, I put all the Python code in the notebook.
At the start everything was fine. But over time the code I wrote in the notebook became more and more complex. Now it is close to unmanagenable: When I find an error, I need
- to navigate to the code part with the error (usually at the beginning of the notebook),
- fix the error there,
- go to (usually) the bottom of the notebook to trigger the execution of the code I changed.
I want to separate the code in two parts:
- One that will be stored as Python files and which I will edit using an editor (and/or an IDE).
- The code in the Jupyter notebook that calls code parts 1 and presents its output (i. e. use the Jupyter notebook as a user interface for the Python code from step 1).
Let's assume that the notebook runs on my local machine (Windows 7; Jupyter runs in Anaconda) and the Python files are also stored locally.
What are good ways to use code from IPython files such that I can modify this code frequently and fast?
By "frequently and fast" I mean "with as little steps as possible that are necessary to propagate changes from Python files to the notebook". The ideal solution would be something where I change one of the Python files, run one command, and then the changes are available in the Jupyter notebook. Or, to use an older analogy, I want it to be like PHP -- you change the code often and immediately see the results of your changes.
Update 1: I tried to use the solution with %load TestClass.py
in a cell.
The problem is that the cell contents is not updated, if the file changes.
Example:
Let's say I put the text
class TestClass:
def __init__(self):
print("TestClass constructor")
into TestClass.py
. Then I create a cell in Jupyter notebook with %load TestClass.py
. When I execute that cell, the code from TestClass.py
is imported and the line %load TestClass.py
gets commented out.
Now I change TestClass.py
to
class TestClass:
def __init__(self):
print("TestClass constructor")
print("change")
When I execute the cell, its contents has not changed.