定义一个 IPython 魔法来替换下一个单元格的内容

Define a IPython magic which replaces the content of the next cell

%load line-magic 命令将给定文件的内容加载到 当前 单元格中,例如,执行:

[cell 1]    %load hello_world.py

...将单元格转换为:

[cell 1]    # %load hello_world.py
            print("hello, world")

我想创建一个 %load_next line-magic 命令,该命令会将此文件加载到 next 单元格中。例如,在以下笔记本中执行单元格 1:

[cell 1]    %load_next hello_world.py

[cell 2]    print("hello, cruel world")  # original content

... 将保持单元格 1 不变并使用新内容更新单元格 2:

[cell 1]    %load_next hello_world.py

[cell 2]    print("hello, world")

我试过这个:

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)

但它插入当前单元格和下一个单元格之间的内容:

[cell 1]    %load_next hello_world.py

[cell 2]    print("hello, world")

[cell 3]    print("hello, cruel world")  # original content

是否可以让它替换下一个单元格,或者在插入之前删除下一个单元格?

您可以 运行 下面的脚本。没有办法获取所有单元格,所以我决定 运行 javascript 代码来删除下一个单元格。 Js 部分查找所有单元格并从当前单元格中删除下一个单元格。我已经在 Jupyter NotebookJupyter Lab.

上进行了测试
from IPython.display import display, HTML, Javascript
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):
        js_script = r"""<script>
            
            if (document.getElementById('notebook-container')) {
                //console.log('Jupyter Notebook');
                allCells = document.getElementById('notebook-container').children;
                selectionClass = /\bselected\b/;
                jupyter = 'notebook';
            }
            else if (document.getElementsByClassName('jp-Notebook-cell').length){
                //console.log('Jupyter Lab');
                allCells = document.getElementsByClassName('jp-Notebook-cell');
                selectionClass = /\bjp-mod-selected\b/;
                jupyter = 'lab';
            }
            else {
                console.log('Unknown Environment');
            }

            if (typeof allCells !== 'undefined') {
                for (i = 0; i < allCells.length - 1; i++) {
                    if(selectionClass.test(allCells[i].getAttribute('class'))){
                        allCells[i + 1].remove();
                        
                        // remove output indicators of current cell
                        window.setTimeout(function(){
                            if(jupyter === 'lab') {
                                allCells[i].setAttribute('class', allCells[i].getAttribute('class') + ' jp-mod-noOutputs');
                                allCells[i].getElementsByClassName('jp-OutputArea jp-Cell-outputArea')[0].innerHTML = '';
                            } else if(jupyter === 'notebook'){
                                allCells[i].getElementsByClassName('output')[0].innerHTML = '';
                            }
                        }, 20);
                        
                        break;
                    }
                }
            }
            </script>"""
        
        # remove next cell
        display(HTML(js_script))
        
        new_content = Path(line).read_text()
        self.shell.set_next_input(new_content, replace=False)

ip = get_ipython()
ip.register_magics(MyMagics)