The %load line-magic command loads the content of a given file into the current cell, for instance, executing:
[cell 1] %load hello_world.py
... transform the cell into:
[cell 1] # %load hello_world.py
print("hello, world")
I would like to create a %load_next line-magic command which would instead load this file into the next cell. For example, executing cell 1 in the following notebook:
[cell 1] %load_next hello_world.py
[cell 2] print("hello, cruel world") # original content
... would keep cell 1 unchanged and update cell 2 with the new content:
[cell 1] %load_next hello_world.py
[cell 2] print("hello, world")
I have tried this:
from IPython.core.magic import Magics, magics_class, line_magic
from pathlib import Path
@magics_class
class MyMagics(Magics):
@line_magic
def load_next(self, line):
new_content = Path(line).read_text()
self.shell.set_next_input(new_content, replace=False)
ip = get_ipython()
ip.register_magics(MyMagics)
But it inserts the content between the current and the next cell:
[cell 1] %load_next hello_world.py
[cell 2] print("hello, world")
[cell 3] print("hello, cruel world") # original content
Is it possible to make it either replace the next cell, or delete the next cell before inserting it?
You can run below script. There is no way to get all cells, so I decided to run
javascriptcode to remove the next cell.Jspart finds all cells and remove the next cell from the current cell. I have tested onJupyter NotebookandJupyter Lab.