1

Let's say I have [cell 1] following notebook (I know that could also import this ipython-magic from ~/.ipython/profile_default/startup/:

[cell 1]    

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)

The %load_next line-magic command creates a new next cell and loads content in this new cell, for instance, executing:

[cell 2]    %load_next hello.py

... creates a new next cell and loads content:

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

But if I run a second time and try a second content file from hello_1.py:

[cell 2]    %load_next hello_1.py

%load_next creates:

[cell 3]    print("hello, happy world")
[cell 4]    print("hello, world")

I would like to create a %load_next line-magic command which would instead update this file into [cell 3] instead of copying the old [cell 3] content to a subsequent [cell 4]. For example, executing [cell 2] a second time and then only update [cell 3] with the new content:

[cell 2]    %load_next hello_1.py

[cell 3]    print("hello, happy world")
7beggars_nnnnm
  • 697
  • 3
  • 12

0 Answers0