Skip to content

Commit fa1838c

Browse files
authored
STY: Move _repr_mimebundle_ (#2978)
Put as the last member of PdfWriter, as is different functionality to the other methods.
1 parent 402cc7f commit fa1838c

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

pypdf/_writer.py

+31-31
Original file line numberDiff line numberDiff line change
@@ -392,37 +392,6 @@ def __exit__(
392392
if self.fileobj and not self._cloned:
393393
self.write(self.fileobj)
394394

395-
def _repr_mimebundle_(
396-
self,
397-
include: Union[None, Iterable[str]] = None,
398-
exclude: Union[None, Iterable[str]] = None,
399-
) -> Dict[str, Any]:
400-
"""
401-
Integration into Jupyter Notebooks.
402-
403-
This method returns a dictionary that maps a mime-type to its
404-
representation.
405-
406-
.. seealso::
407-
408-
https://ipython.readthedocs.io/en/stable/config/integrating.html
409-
"""
410-
pdf_data = BytesIO()
411-
self.write(pdf_data)
412-
data = {
413-
"application/pdf": pdf_data,
414-
}
415-
416-
if include is not None:
417-
# Filter representations based on include list
418-
data = {k: v for k, v in data.items() if k in include}
419-
420-
if exclude is not None:
421-
# Remove representations based on exclude list
422-
data = {k: v for k, v in data.items() if k not in exclude}
423-
424-
return data
425-
426395
@property
427396
def pdf_header(self) -> str:
428397
"""
@@ -3299,6 +3268,37 @@ def _set_page_label(
32993268
page_labels[NameObject("/Nums")] = nums
33003269
self._root_object[NameObject(CatalogDictionary.PAGE_LABELS)] = page_labels
33013270

3271+
def _repr_mimebundle_(
3272+
self,
3273+
include: Union[None, Iterable[str]] = None,
3274+
exclude: Union[None, Iterable[str]] = None,
3275+
) -> Dict[str, Any]:
3276+
"""
3277+
Integration into Jupyter Notebooks.
3278+
3279+
This method returns a dictionary that maps a mime-type to its
3280+
representation.
3281+
3282+
.. seealso::
3283+
3284+
https://ipython.readthedocs.io/en/stable/config/integrating.html
3285+
"""
3286+
pdf_data = BytesIO()
3287+
self.write(pdf_data)
3288+
data = {
3289+
"application/pdf": pdf_data,
3290+
}
3291+
3292+
if include is not None:
3293+
# Filter representations based on include list
3294+
data = {k: v for k, v in data.items() if k in include}
3295+
3296+
if exclude is not None:
3297+
# Remove representations based on exclude list
3298+
data = {k: v for k, v in data.items() if k not in exclude}
3299+
3300+
return data
3301+
33023302

33033303
def _pdf_objectify(obj: Union[Dict[str, Any], str, float, List[Any]]) -> PdfObject:
33043304
if isinstance(obj, PdfObject):

0 commit comments

Comments
 (0)