Skip to content

Commit 0bd2f50

Browse files
authored
Merge pull request #411 from martindurant/notebook_no_ipywidgets
Display in notebook without ipywidgets
2 parents ae84982 + be6c01b commit 0bd2f50

4 files changed

Lines changed: 54 additions & 7 deletions

File tree

streamz/collection.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,17 @@ def stop(self):
239239
self.stream.stop()
240240

241241
def _ipython_display_(self, **kwargs):
242-
return self.stream.latest().rate_limit(
243-
0.5).gather()._ipython_display_(**kwargs)
242+
try:
243+
from ipywidgets import Output # noqa: F401
244+
return self.stream.latest().rate_limit(
245+
0.5).gather()._ipython_display_(**kwargs)
246+
except ImportError:
247+
# since this function is only called by jupyter, this import must succeed
248+
from IPython.display import display, HTML
249+
if hasattr(self, '_repr_html_'):
250+
return display(HTML(self._repr_html_()))
251+
else:
252+
return display(self.__repr__())
244253

245254
def emit(self, x):
246255
self.verify(x)

streamz/core.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from __future__ import absolute_import, division, print_function
2-
32
from collections import deque, defaultdict
43
from datetime import timedelta
54
import functools
@@ -385,14 +384,16 @@ def __str__(self):
385384

386385
def _ipython_display_(self, **kwargs): # pragma: no cover
387386
try:
388-
from ipywidgets import Output
387+
import ipywidgets
389388
from IPython.core.interactiveshell import InteractiveShell
389+
output = ipywidgets.Output(_view_count=0)
390390
except ImportError:
391+
# since this function is only called by jupyter, this import must succeed
392+
from IPython.display import display, HTML
391393
if hasattr(self, '_repr_html_'):
392-
return self._repr_html_()
394+
return display(HTML(self._repr_html_()))
393395
else:
394-
return self.__repr__()
395-
output = Output(_view_count=0)
396+
return display(self.__repr__())
396397
output_ref = weakref.ref(output)
397398

398399
def update_cell(val):

streamz/dataframe/tests/test_dataframes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,24 @@ def test_repr_html(stream):
344344
assert '1' in html
345345

346346

347+
def test_display(monkeypatch, capsys):
348+
pytest.importorskip("ipywidgets")
349+
import ipywidgets
350+
df = pd.DataFrame({'x': (np.arange(10) // 2).astype(float), 'y': [1.0] * 10})
351+
a = DataFrame(example=df, stream=stream)
352+
353+
# works by side-affect of display()
354+
a._ipython_display_()
355+
assert "Output()" in capsys.readouterr().out
356+
357+
def get(*_, **__):
358+
raise ImportError
359+
monkeypatch.setattr(ipywidgets.Output, "__init__", get)
360+
361+
out = source._ipython_display_()
362+
assert "DataFrame" in capsys.readouterr().out
363+
364+
347365
def test_setitem(stream):
348366
df = pd.DataFrame({'x': list(range(10)), 'y': [1] * 10})
349367

streamz/tests/test_core.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,25 @@ def add(x=0, y=0):
10161016
assert str(s) == '<map: add>'
10171017

10181018

1019+
def test_no_ipywidget_repr(monkeypatch, capsys):
1020+
pytest.importorskip("ipywidgets")
1021+
import ipywidgets
1022+
source = Stream()
1023+
1024+
# works by side-affect of display()
1025+
source._ipython_display_()
1026+
assert "Output()" in capsys.readouterr().out
1027+
1028+
def get(*_, **__):
1029+
raise ImportError
1030+
monkeypatch.setattr(ipywidgets.Output, "__init__", get)
1031+
1032+
out = source._ipython_display_()
1033+
assert "Stream" in capsys.readouterr().out
1034+
1035+
1036+
1037+
10191038
def test_filter_str():
10201039
def iseven(x):
10211040
return x % 2 == 0

0 commit comments

Comments
 (0)