Skip to content

Commit

Permalink
STY: Move _repr_mimebundle_ (#2978)
Browse files Browse the repository at this point in the history
Put as the last member of PdfWriter, as is different functionality to the other methods.
  • Loading branch information
j-t-1 authored Dec 2, 2024
1 parent 402cc7f commit fa1838c
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit fa1838c

Please sign in to comment.