forked from docarray/docarray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
46 lines (35 loc) · 1.53 KB
/
plot.py
File metadata and controls
46 lines (35 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import base64
from typing import Optional
from ...helper import random_identity, download_mermaid_url
class PlotMixin:
"""Provide helper functions for :class:`Document` to plot and visualize itself. """
def _ipython_display_(self):
"""Displays the object in IPython as a side effect"""
self.summary()
def summary(self) -> None:
""" Print non-empty fields and nested structure of this Document object."""
_str_list = []
self._plot_recursion(_str_list, indent=0)
print('\n'.join(_str_list))
def _plot_recursion(self, _str_list, indent, box_char='├─'):
prefix = (' ' * indent + box_char) if indent else ''
_str_list.append(f'{prefix} {self}')
for a in ('matches', 'chunks'):
if getattr(self, a):
prefix = ' ' * (indent + 4) + '└─'
_str_list.append(f'{prefix} {a}')
for d in getattr(self, a)[:-1]:
d._plot_recursion(_str_list, indent=len(prefix) + 4)
getattr(self, a)[-1]._plot_recursion(
_str_list, indent=len(prefix) + 4, box_char='└─'
)
def plot(self):
""" Plot image data from :attr:`.blob` or :attr:`.uri`. """
from IPython.display import Image, display
if self.blob is not None:
import PIL.Image
display(PIL.Image.fromarray(self.blob))
elif self.uri:
display(Image(self.uri))
else:
raise ValueError('`uri` and `blob` is empty')