Simple way to choose which cells to run in ipython notebook during run all

后端 未结 6 1813
借酒劲吻你
借酒劲吻你 2020-12-05 05:45

I have an ipython notebook that runs several steps in a data processing routine and saves information in files along the way. This way, while developing my code (mostly in a

6条回答
  •  孤城傲影
    2020-12-05 06:22

    You can create your own skip magic with the help of a custom kernel extension.

    skip_kernel_extension.py

    def skip(line, cell=None):
        '''Skips execution of the current line/cell if line evaluates to True.'''
        if eval(line):
            return
    
        get_ipython().ex(cell)
    
    def load_ipython_extension(shell):
        '''Registers the skip magic when the extension loads.'''
        shell.register_magic_function(skip, 'line_cell')
    
    def unload_ipython_extension(shell):
        '''Unregisters the skip magic when the extension unloads.'''
        del shell.magics_manager.magics['cell']['skip']
    

    Load the extension in your notebook:

    %load_ext skip_kernel_extension
    

    Run the skip magic command in the cells you want to skip:

    %%skip True  #skips cell
    %%skip False #won't skip
    

    You can use a variable to decide if a cell should be skipped by using $:

    should_skip = True
    %%skip $should_skip
    

提交回复
热议问题