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

后端 未结 6 1809
借酒劲吻你
借酒劲吻你 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:30

    If you are using nbconvert to execute your notebook, you can write a custom preprocessor that looks at cell metadata to know which cells to execute.

    class MyExecutePreprocessor(nbconvert.preprocessors.ExecutePreprocessor):
    
        def preprocess_cell(self, cell, resources, cell_index):
            """
            Executes a single code cell. See base.py for details.
            To execute all cells see :meth:`preprocess`.
    
            Checks cell.metadata for 'execute' key. If set, and maps to False, 
              the cell is not executed.
            """
    
            if not cell.metadata.get('execute', True):
                # Don't execute this cell in output
                return cell, resources
    
            return super().preprocess_cell(cell, resources, cell_index)
    

    By editing cell metadata, you can specify whether that cell should be executed.

    You can get fancier by adding a master dictionary to your notebook metadata. This would look like the dictionary in your example, mapping sections to a boolean specifying whether that section would be called.

    Then, in your cell metadata, you can use a "section" keyword mapping to the section ID in your notebook metadata.

    When executing nbconvert, you can tell it to use your preprocessor.

    See the docs on Notebook preprocessors for more information.

提交回复
热议问题