Skip to content

Commit

Permalink
Coalesce code cell outputs before getting thumbnail index
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeier committed May 13, 2024
1 parent e9cb489 commit aac22cf
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/nbsphinx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,22 @@ def warning(msg, *args):
# Now we are looking for the last output image in the notebook.
for cell_index, cell in reversed(list(enumerate(nb.cells))):
if cell.cell_type == 'code':
# The following can be quickly skipped if there is
# less than 1 image output and 2 stream outputs:
if len(cell.outputs) >= 3 and self.config.get(
'CoalesceStreamsPreprocessor', {}).get(
'enabled', False):
# CoalesceStreamsPreprocessor was introduced in
# nbconvert version 7.14 and enabled in 7.16.4,
# see https://github.com/jupyter/nbconvert/pull/2142.
from nbconvert.preprocessors \
import CoalesceStreamsPreprocessor
pp = CoalesceStreamsPreprocessor()
# The CoalesceStreamsPreprocessor will be executed as
# part of the RSTExporter, but we already have to call
# it here to get the correct output indices:
cell, _ = pp.preprocess_cell(cell, {}, cell_index)
# NB: This correction doesn't happen for nbconvert<7.14
for output_index in reversed(range(len(cell.outputs))):
try:
suffix = _extract_thumbnail(cell, output_index)
Expand Down

0 comments on commit aac22cf

Please sign in to comment.