Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export individual cell in IPython/Jupyter notebook

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?

like image 227
P i Avatar asked Nov 04 '15 09:11

P i


People also ask

How do you separate cells in a Jupyter Notebook?

Enter command mode ( Esc ), use Shift + s to toggle the current cell to either a split cell or full width.

What is %% capture in Python?

IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. from __future__ import print_function import sys. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

How do I export results from Jupyter?

Download Jupyter Notebook as PDF The Jupyter Notebook has an option to export the notebook to many formats. It can be accessed by clicking File -> Download as -> PDF via LaTeX (or PDF via HTML - not visible in the screenshot). This approach requires you to install some additional packages.


1 Answers

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. :)

like image 85
Gordon Bean Avatar answered Sep 20 '22 11:09

Gordon Bean