utils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. from ipykernel.comm import Comm
  4. from ipywidgets import Widget
  5. class DummyComm(Comm):
  6. comm_id = 'a-b-c-d'
  7. kernel = 'Truthy'
  8. def __init__(self, *args, **kwargs):
  9. super(DummyComm, self).__init__(*args, **kwargs)
  10. self.messages = []
  11. def open(self, *args, **kwargs):
  12. pass
  13. def send(self, *args, **kwargs):
  14. self.messages.append((args, kwargs))
  15. def close(self, *args, **kwargs):
  16. pass
  17. _widget_attrs = {}
  18. undefined = object()
  19. def setup_test_comm():
  20. _widget_attrs['_comm_default'] = getattr(Widget, '_comm_default', undefined)
  21. Widget._comm_default = lambda self: DummyComm()
  22. _widget_attrs['_ipython_display_'] = Widget._ipython_display_
  23. def raise_not_implemented(*args, **kwargs):
  24. raise NotImplementedError()
  25. Widget._ipython_display_ = raise_not_implemented
  26. def teardown_test_comm():
  27. for attr, value in _widget_attrs.items():
  28. if value is undefined:
  29. delattr(Widget, attr)
  30. else:
  31. setattr(Widget, attr, value)
  32. _widget_attrs.clear()
  33. def setup():
  34. setup_test_comm()
  35. def teardown():
  36. teardown_test_comm()