From fa1838cdbaa9ddc2609309b421bda48ad44d365f Mon Sep 17 00:00:00 2001 From: j-t-1 <120829237+j-t-1@users.noreply.github.com> Date: Mon, 2 Dec 2024 17:24:18 +0000 Subject: [PATCH] STY: Move `_repr_mimebundle_` (#2978) Put as the last member of PdfWriter, as is different functionality to the other methods. --- pypdf/_writer.py | 62 ++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index c4fc07c7f..463bc2988 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -392,37 +392,6 @@ def __exit__( if self.fileobj and not self._cloned: self.write(self.fileobj) - def _repr_mimebundle_( - self, - include: Union[None, Iterable[str]] = None, - exclude: Union[None, Iterable[str]] = None, - ) -> Dict[str, Any]: - """ - Integration into Jupyter Notebooks. - - This method returns a dictionary that maps a mime-type to its - representation. - - .. seealso:: - - https://ipython.readthedocs.io/en/stable/config/integrating.html - """ - pdf_data = BytesIO() - self.write(pdf_data) - data = { - "application/pdf": pdf_data, - } - - if include is not None: - # Filter representations based on include list - data = {k: v for k, v in data.items() if k in include} - - if exclude is not None: - # Remove representations based on exclude list - data = {k: v for k, v in data.items() if k not in exclude} - - return data - @property def pdf_header(self) -> str: """ @@ -3299,6 +3268,37 @@ def _set_page_label( page_labels[NameObject("/Nums")] = nums self._root_object[NameObject(CatalogDictionary.PAGE_LABELS)] = page_labels + def _repr_mimebundle_( + self, + include: Union[None, Iterable[str]] = None, + exclude: Union[None, Iterable[str]] = None, + ) -> Dict[str, Any]: + """ + Integration into Jupyter Notebooks. + + This method returns a dictionary that maps a mime-type to its + representation. + + .. seealso:: + + https://ipython.readthedocs.io/en/stable/config/integrating.html + """ + pdf_data = BytesIO() + self.write(pdf_data) + data = { + "application/pdf": pdf_data, + } + + if include is not None: + # Filter representations based on include list + data = {k: v for k, v in data.items() if k in include} + + if exclude is not None: + # Remove representations based on exclude list + data = {k: v for k, v in data.items() if k not in exclude} + + return data + def _pdf_objectify(obj: Union[Dict[str, Any], str, float, List[Any]]) -> PdfObject: if isinstance(obj, PdfObject):