Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbconvert/preprocessors/convertfigures.py: 57%
14 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""Module containing a preprocessor that converts outputs in the notebook from
2one format to another.
4Converts all of the outputs in a notebook from one format to another.
5"""
6# Copyright (c) Jupyter Development Team.
7# Distributed under the terms of the Modified BSD License.
10from traitlets import Unicode
12from .base import Preprocessor
15class ConvertFiguresPreprocessor(Preprocessor):
16 """
17 Converts all of the outputs in a notebook from one format to another.
18 """
20 from_format = Unicode(help="Format the converter accepts").tag(config=True)
21 to_format = Unicode(help="Format the converter writes").tag(config=True)
23 def __init__(self, **kw):
24 """
25 Public constructor
26 """
27 super().__init__(**kw)
29 def convert_figure(self, data_format, data):
30 """Convert the figure."""
31 raise NotImplementedError()
33 def preprocess_cell(self, cell, resources, cell_index):
34 """
35 Apply a transformation on each cell,
37 See base.py
38 """
40 # Loop through all of the datatypes of the outputs in the cell.
41 for output in cell.get("outputs", []):
42 if (
43 output.output_type in {"execute_result", "display_data"}
44 and self.from_format in output.data
45 and self.to_format not in output.data
46 ):
47 output.data[self.to_format] = self.convert_figure(
48 self.from_format, output.data[self.from_format]
49 )
51 return cell, resources