Issue
I want to have some notebooks that have blocks of code (as Raw NBConvert) that are from local Python files. I'd like for the notebook to update if the content of the file changes. In Latex I can do this with \lstinputlisting[language=python]{directory/file.py}
. Is there something similar I can do in Jupyter?
Solution
I can think of a couple of possibilities that might help. You could use the magic command %load
, which will load the contents of a file into the cell, i.e. if foo.py
contains the single line print('foo')
, you can have a code cell like this:
%load foo.py
Which, when run the first time will become this: # %load foo.py print('foo')
But that won't automatically update when foo.py
changes.
What I usually do is load the desired file as a module, i.e. modular code the way Guido intended :-). You can edit the file in another tab/window in Jupyter, and you can use importlib.reload()
to reload it.
Answered By - OldGeeksGuide
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.