Export individual cell in IPython/Jupyter notebook

2019-04-05 09:19发布

问题:

I am able to export the entire notebook as HTML, but I would like to export just a single cell, together with its output.

Is there some way of doing this?

回答1:

One way to do this is to use a custom preprocessor.

I explain how to do this briefly in response to Simple way to choose which cells to run in ipython notebook during run all.

To summarize: you can extend nbconvert.preprocessors.ExecutePreprocessor to create a preprocessor that checks cell metadata to determine whether that cell should be executed and/or output.

I use Jupyter Notebooks for report generation all the time, so I wrote a collection of custom processors to extend nbconvert behavior:

  • meta-language to determine what cells get executed and included in the final report (if/else logic on entire notebook sections)
  • executing code in markdown cells
  • removing code cells from output.
  • taking input arguments from the command line

I haven't had time to wrap these in an distributable extension, but you can see the code here: https://gist.github.com/brazilbean/3ebb31324f6dad212817b3663c7a0219.

Please feel free to use/modify/do-great-things with these examples. :)