test_widget.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """Test Widget."""
  4. from IPython.core.interactiveshell import InteractiveShell
  5. from IPython.display import display
  6. from IPython.utils.capture import capture_output
  7. from ..widget import Widget
  8. from ..widget_button import Button
  9. def test_no_widget_view():
  10. # ensure IPython shell is instantiated
  11. # otherwise display() just calls print
  12. shell = InteractiveShell.instance()
  13. with capture_output() as cap:
  14. w = Widget()
  15. display(w)
  16. assert len(cap.outputs) == 1, "expect 1 output"
  17. mime_bundle = cap.outputs[0].data
  18. assert mime_bundle['text/plain'] == repr(w), "expected plain text output"
  19. assert 'application/vnd.jupyter.widget-view+json' not in mime_bundle, "widget has no view"
  20. assert cap.stdout == '', repr(cap.stdout)
  21. assert cap.stderr == '', repr(cap.stderr)
  22. def test_widget_view():
  23. # ensure IPython shell is instantiated
  24. # otherwise display() just calls print
  25. shell = InteractiveShell.instance()
  26. with capture_output() as cap:
  27. w = Button()
  28. display(w)
  29. assert len(cap.outputs) == 1, "expect 1 output"
  30. mime_bundle = cap.outputs[0].data
  31. assert mime_bundle['text/plain'] == repr(w), "expected plain text output"
  32. assert 'application/vnd.jupyter.widget-view+json' in mime_bundle, "widget should have have a view"
  33. assert cap.stdout == '', repr(cap.stdout)
  34. assert cap.stderr == '', repr(cap.stderr)