27

I am playing around with formatting my iPython Notebooks in order to make them more into a logbook.

The use of display(HTML()) constructs makes everything nice and relatively easy to organize.

I would like to use the output of certain cells in other media, like for instance presentations. The way I do it now is by taking a screenshot of the area, but then everything becomes pixels and there is no refinement possible.

Is there a way to render the output of a single cell in some useful format?

whoisraibolt
  • 2,082
  • 3
  • 22
  • 45
Michele Ancis
  • 1,265
  • 4
  • 16
  • 29
  • 1
    Is HTML an acceptable format for you? I found that it automatically embeds images and even videos. In that case, you could simply write the HTML verbatim to a file. – cfh Nov 10 '17 at 15:14
  • Have you tried markdown in general to embed content to presentations? – Ankita Mehta Sep 02 '19 at 10:45

2 Answers2

6

You can try %%capture Jupyter notebook command.

Checkout this answer: how to save the output of a cell in iPython notebook?

Ankita Mehta
  • 590
  • 4
  • 19
  • 1
    Upvoted since this is nice, however it is text capture – ntg Sep 07 '22 at 10:54
  • Thank you @ntg Hope it helps someone! – Ankita Mehta Sep 07 '22 at 16:51
  • Contrary to one of the comment, %%capture jupyter magic function does not only capture text: it captures all outputs by default (so even display), and then you can choose to not capture some of them with the options --no-stderr, --no-stdout, or --no-display. The main limit to %%capture is that it is a *cell magic function* (%catpure, the line version is not implemented) so the saving of the captured data in a file has to be done in another cell of the notebook. – Ken May 23 '23 at 08:32
4

There is no possible way to export a single cell output in Jupyter, as of now, but what you can do is to convert the entire notebook to a more useful format and then trim out only the parts that you need. This is not optimal, but it's still better than your current workaround, at least concerning the quality of the output.

You can do this in different ways:

  1. File -> Export Notebook as... -> Your preferred file format
  2. If you want to do it programmatically, you can use nbconvert from the command line like this:

    nbconvert --to (your preferred output format) yourNotebook.ipynb

You can also do it from inside your notebook, executing this code in a cell (for HTML, in this example):

from nbconvert import HTMLExporter
import codecs
import nbformat

notebook_name = 'YOUR_NOTEBOOK_NAME.ipynb'
output_file_name = 'output.html'

exporter = HTMLExporter()
output_notebook = nbformat.read(notebook_name, as_version=4)

output, resources = exporter.from_notebook_node(output_notebook)
codecs.open(output_file_name, 'w', encoding='utf-8').write(output)

Most libraries, though, allow you to export the result of your program to any desired outpout (pandas, matplotlib, altair...), so you probably should try to use them.

TrinTragula
  • 360
  • 2
  • 13
  • 2
    The 'trim the parts you don't need' part can be done by tagging those cells with a tag of your choice then using the `TagRemovePreprocessor` options. – Martijn Pieters Dec 02 '19 at 19:14