Skip to content

Commit 01e7c5d

Browse files
authored
STY: Put Jupyter functionality at end of class (#2988)
Integration into Jupyter Notebooks is by method `_repr_mimebundle_`, put at end of class PdfDocCommon, as is functionally different from the other methods.
1 parent ae93e98 commit 01e7c5d

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed

pypdf/_doc_common.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -316,22 +316,6 @@ def metadata(self) -> Optional[DocumentInformation]:
316316
def xmp_metadata(self) -> Optional[XmpInformation]:
317317
... # pragma: no cover
318318

319-
@abstractmethod
320-
def _repr_mimebundle_(
321-
self,
322-
include: Union[None, Iterable[str]] = None,
323-
exclude: Union[None, Iterable[str]] = None,
324-
) -> Dict[str, Any]:
325-
"""
326-
Integration into Jupyter Notebooks.
327-
328-
This method returns a dictionary that maps a mime-type to its
329-
representation.
330-
331-
See https://ipython.readthedocs.io/en/stable/config/integrating.html
332-
"""
333-
... # pragma: no cover
334-
335319
@property
336320
def viewer_preferences(self) -> Optional[ViewerPreferences]:
337321
"""Returns the existing ViewerPreferences as an overloaded dictionary."""
@@ -1432,6 +1416,24 @@ def _get_attachments(
14321416
attachments[name] = f_data
14331417
return attachments
14341418

1419+
@abstractmethod
1420+
def _repr_mimebundle_(
1421+
self,
1422+
include: Union[None, Iterable[str]] = None,
1423+
exclude: Union[None, Iterable[str]] = None,
1424+
) -> Dict[str, Any]:
1425+
"""
1426+
Integration into Jupyter Notebooks.
1427+
1428+
This method returns a dictionary that maps a mime-type to its
1429+
representation.
1430+
1431+
.. seealso::
1432+
1433+
https://ipython.readthedocs.io/en/stable/config/integrating.html
1434+
"""
1435+
... # pragma: no cover
1436+
14351437

14361438
class LazyDict(Mapping[Any, Any]):
14371439
def __init__(self, *args: Any, **kw: Any) -> None:

pypdf/_reader.py

+31-29
Original file line numberDiff line numberDiff line change
@@ -262,35 +262,6 @@ def _ID(self) -> Optional[ArrayObject]:
262262
id = self.trailer.get(TK.ID, None)
263263
return None if is_null_or_none(id) else cast(ArrayObject, id.get_object())
264264

265-
def _repr_mimebundle_(
266-
self,
267-
include: Union[None, Iterable[str]] = None,
268-
exclude: Union[None, Iterable[str]] = None,
269-
) -> Dict[str, Any]:
270-
"""
271-
Integration into Jupyter Notebooks.
272-
273-
This method returns a dictionary that maps a mime-type to its
274-
representation.
275-
276-
See https://ipython.readthedocs.io/en/stable/config/integrating.html
277-
"""
278-
self.stream.seek(0)
279-
pdf_data = self.stream.read()
280-
data = {
281-
"application/pdf": pdf_data,
282-
}
283-
284-
if include is not None:
285-
# Filter representations based on include list
286-
data = {k: v for k, v in data.items() if k in include}
287-
288-
if exclude is not None:
289-
# Remove representations based on exclude list
290-
data = {k: v for k, v in data.items() if k not in exclude}
291-
292-
return data
293-
294265
@property
295266
def pdf_header(self) -> str:
296267
"""
@@ -1254,3 +1225,34 @@ def rename_form_topname(self, name: str) -> Optional[DictionaryObject]:
12541225
)
12551226
interim[NameObject("/T")] = TextStringObject(name)
12561227
return interim
1228+
1229+
def _repr_mimebundle_(
1230+
self,
1231+
include: Union[None, Iterable[str]] = None,
1232+
exclude: Union[None, Iterable[str]] = None,
1233+
) -> Dict[str, Any]:
1234+
"""
1235+
Integration into Jupyter Notebooks.
1236+
1237+
This method returns a dictionary that maps a mime-type to its
1238+
representation.
1239+
1240+
.. seealso::
1241+
1242+
https://ipython.readthedocs.io/en/stable/config/integrating.html
1243+
"""
1244+
self.stream.seek(0)
1245+
pdf_data = self.stream.read()
1246+
data = {
1247+
"application/pdf": pdf_data,
1248+
}
1249+
1250+
if include is not None:
1251+
# Filter representations based on include list
1252+
data = {k: v for k, v in data.items() if k in include}
1253+
1254+
if exclude is not None:
1255+
# Remove representations based on exclude list
1256+
data = {k: v for k, v in data.items() if k not in exclude}
1257+
1258+
return data

0 commit comments

Comments
 (0)