Skip to content

Commit 3564da4

Browse files
authored
Add better ipytree error handling in jupyter notebooks (#539)
* Add better ipytree error handling in jupyter notebooks * Fix pep8 issues * Use notebook instead of jupyter in setup.py * Update error with jupyterlab specific fix * Update error message in test function too * Update docs/release.rst * Update error message with conda
1 parent e7d5f39 commit 3564da4

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

docs/release.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Next release
77

88
* Fix minor bug in `N5Store`.
99
By :user:`gsakkis`, :issue:`550`.
10+
* Improve error message in Jupyter when trying to use the ``ipytree`` widget
11+
without ``ipytree`` installed.
12+
By :user:`Zain Patel <mzjp2>; :issue:`537`
1013

1114

1215
.. _release_2.4.0:

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
use_scm_version={
2525
'version_scheme': 'guess-next-dev',
2626
'local_scheme': 'dirty-tag',
27-
'write_to': 'zarr/version.py'
27+
'write_to': 'zarr/version.py',
2828
},
2929
setup_requires=[
3030
'setuptools>=38.6.0',
31-
'setuptools-scm>1.5.4'
31+
'setuptools-scm>1.5.4',
3232
],
3333
extras_require={
3434
'jupyter': [
35+
'notebook',
3536
'ipytree',
3637
],
3738
},

zarr/tests/test_util.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# -*- coding: utf-8 -*-
2+
import re
3+
from unittest import mock
4+
25
import numpy as np
36
import pytest
47

58
from zarr.util import (guess_chunks, human_readable_size, info_html_report,
69
info_text_report, is_total_slice, normalize_chunks,
710
normalize_fill_value, normalize_order,
811
normalize_resize_args, normalize_shape,
9-
tree_array_icon, tree_group_icon, tree_get_icon)
12+
tree_array_icon, tree_group_icon, tree_get_icon,
13+
tree_widget)
1014

1115

1216
def test_normalize_shape():
@@ -160,3 +164,15 @@ def test_tree_get_icon():
160164
assert tree_get_icon("Group") == tree_group_icon
161165
with pytest.raises(ValueError):
162166
tree_get_icon("Baz")
167+
168+
169+
@mock.patch.dict("sys.modules", {"ipytree": None})
170+
def test_tree_widget_missing_ipytree():
171+
pattern = (
172+
"Run `pip install zarr[jupyter]` or `conda install ipytree`"
173+
"to get the required ipytree dependency for displaying the tree "
174+
"widget. If using jupyterlab, you also need to run "
175+
"`jupyter labextension install ipytree`"
176+
)
177+
with pytest.raises(ImportError, match=re.escape(pattern)):
178+
tree_widget(None, None, None)

zarr/util.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,15 @@ def tree_widget_sublist(node, root=False, expand=False):
428428

429429

430430
def tree_widget(group, expand, level):
431-
import ipytree
431+
try:
432+
import ipytree
433+
except ImportError as error:
434+
raise ImportError(
435+
"{}: Run `pip install zarr[jupyter]` or `conda install ipytree`"
436+
"to get the required ipytree dependency for displaying the tree "
437+
"widget. If using jupyterlab, you also need to run "
438+
"`jupyter labextension install ipytree`".format(error)
439+
)
432440

433441
result = ipytree.Tree()
434442
root = TreeNode(group, level=level)

0 commit comments

Comments
 (0)