Skip to content

Commit 1ae1ffa

Browse files
authored
feat: add rich traceback handler as default (#215)
1 parent 5c33132 commit 1ae1ffa

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

docarray/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
__version__ = '0.9.15'
22

3+
import os
4+
35
from .document import Document
46
from .array import DocumentArray
7+
8+
if 'DA_NO_RICH_HANDLER' not in os.environ:
9+
from rich.traceback import install
10+
11+
install()

docarray/array/mixins/io/pushpull.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ def pull(
127127
url = f'{_get_cloud_api()}/v2/rpc/da.pull?token={token}'
128128
response = requests.get(url)
129129

130-
url = response.json()['data']['download']
130+
if response.ok:
131+
url = response.json()['data']['download']
132+
else:
133+
raise FileNotFoundError(
134+
f'can not find `{token}` DocumentArray on the Cloud. Misspelled?'
135+
)
131136

132137
with requests.get(
133138
url,
@@ -161,26 +166,3 @@ def pull(
161166
fp.write(_source.content)
162167

163168
return r
164-
165-
166-
def _get_progressbar(show_progress):
167-
from rich.progress import (
168-
BarColumn,
169-
DownloadColumn,
170-
Progress,
171-
TimeRemainingColumn,
172-
TransferSpeedColumn,
173-
)
174-
175-
return Progress(
176-
BarColumn(),
177-
"[progress.percentage]{task.percentage:>3.1f}%",
178-
"•",
179-
DownloadColumn(),
180-
"•",
181-
TransferSpeedColumn(),
182-
"•",
183-
TimeRemainingColumn(),
184-
transient=True,
185-
disable=not show_progress,
186-
)

docarray/document/mixins/plot.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,40 @@ def _ipython_display_(self):
88
"""Displays the object in IPython as a side effect"""
99
self.summary()
1010

11+
def __rich_console__(self, console, options):
12+
yield f":page_facing_up: [b]Document[/b]: [cyan]{self.id}[cyan]"
13+
from rich.table import Table
14+
15+
my_table = Table('Attribute', 'Value')
16+
for f in self.non_empty_fields:
17+
if f not in ('id', 'chunks', 'matches'):
18+
my_table.add_row(f, str(getattr(self, f)))
19+
if my_table.rows:
20+
yield my_table
21+
1122
def summary(self) -> None:
1223
""" Print non-empty fields and nested structure of this Document object."""
13-
_str_list = []
14-
self._plot_recursion(_str_list, indent=0)
15-
print('\n'.join(_str_list))
24+
from rich import print
1625

17-
def _plot_recursion(self, _str_list, indent, box_char='├─'):
18-
prefix = (' ' * indent + box_char) if indent else ''
19-
_str_list.append(f'{prefix} {self}')
26+
print(self._plot_recursion())
2027

28+
def _plot_recursion(self, tree=None):
29+
if tree is None:
30+
from rich.tree import Tree
31+
32+
tree = Tree(self)
33+
else:
34+
tree = tree.add(self)
2135
for a in ('matches', 'chunks'):
2236
if getattr(self, a):
23-
prefix = ' ' * (indent + 4) + '└─'
24-
_str_list.append(f'{prefix} {a}')
25-
26-
for d in getattr(self, a)[:-1]:
27-
d._plot_recursion(_str_list, indent=len(prefix) + 4)
28-
getattr(self, a)[-1]._plot_recursion(
29-
_str_list, indent=len(prefix) + 4, box_char='└─'
30-
)
37+
if a == 'chunks':
38+
_icon = ':diamond_with_a_dot:'
39+
else:
40+
_icon = ':large_orange_diamond:'
41+
_match_tree = tree.add(f'{_icon} [b]{a.capitalize()}[/b]')
42+
for d in getattr(self, a):
43+
d._plot_recursion(_match_tree)
44+
return tree
3145

3246
def display(self):
3347
""" Plot image data from :attr:`.tensor` or :attr:`.uri`. """

tests/unit/array/mixins/test_pushpull.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class PullMockResponse:
1818
def __init__(self, status_code: int = 200):
1919
self.status_code = status_code
2020
self.headers = {'Content-length': 1}
21+
self.ok = True
2122

2223
def json(self):
2324
return {

0 commit comments

Comments
 (0)