Skip to content

Commit e2f076e

Browse files
Merge pull request #100 from martinRenou/eventful_figure
Figure widget
2 parents 6525f50 + 42fd120 commit e2f076e

File tree

4 files changed

+365
-379
lines changed

4 files changed

+365
-379
lines changed

ipympl/backend_nbagg.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import json
55
import io
66
import os
7-
from uuid import uuid4 as uuid
87

98
from IPython.display import display, HTML
109

@@ -92,16 +91,14 @@ class Toolbar(DOMWidget, NavigationToolbar2WebAgg):
9291
_view_module_version = Unicode(js_semver).tag(sync=True)
9392
_view_name = Unicode('ToolbarView').tag(sync=True)
9493

95-
figure_id = Unicode('').tag(sync=True)
9694
toolitems = List().tag(sync=True)
9795
orientation = Enum(['horizontal', 'vertical'], default_value='vertical').tag(sync=True)
9896
# button style?
9997

10098
def __init__(self, canvas, *args, **kwargs):
101-
super(DOMWidget, self).__init__(*args, **kwargs)
102-
super(NavigationToolbar2WebAgg, self).__init__(canvas, *args, **kwargs)
99+
DOMWidget.__init__(self, *args, **kwargs)
100+
NavigationToolbar2WebAgg.__init__(self, canvas, *args, **kwargs)
103101

104-
self.figure_id = self.canvas.id
105102
self.on_msg(self.canvas._handle_message)
106103

107104
def export(self):
@@ -137,7 +134,7 @@ def _default_toolitems(self):
137134
if icon_name in icons]
138135

139136

140-
class FigureCanvasNbAgg(DOMWidget, FigureCanvasWebAggCore):
137+
class Canvas(DOMWidget, FigureCanvasWebAggCore):
141138

142139
_model_module = Unicode('jupyter-matplotlib').tag(sync=True)
143140
_model_module_version = Unicode(js_semver).tag(sync=True)
@@ -147,8 +144,6 @@ class FigureCanvasNbAgg(DOMWidget, FigureCanvasWebAggCore):
147144
_view_module_version = Unicode(js_semver).tag(sync=True)
148145
_view_name = Unicode('MPLCanvasView').tag(sync=True)
149146

150-
id = Unicode('').tag(sync=True)
151-
152147
toolbar = Instance(Toolbar, allow_none=True).tag(sync=True, **widget_serialization)
153148
toolbar_visible = Bool(True).tag(sync=True)
154149
toolbar_position = Enum(['top', 'bottom', 'left', 'right'], default_value='left').tag(sync=True)
@@ -167,23 +162,20 @@ class FigureCanvasNbAgg(DOMWidget, FigureCanvasWebAggCore):
167162
_lasty = Any()
168163

169164
def __init__(self, figure, *args, **kwargs):
170-
super(DOMWidget, self).__init__(*args, **kwargs)
171-
super(FigureCanvasWebAggCore, self).__init__(figure, *args, **kwargs)
172-
173-
self.id = uuid().hex
165+
DOMWidget.__init__(self, *args, **kwargs)
166+
FigureCanvasWebAggCore.__init__(self, figure, *args, **kwargs)
174167

175168
self.on_msg(self._handle_message)
176169

177-
def _handle_message(self, object, message, buffers):
178-
# Every message has a "type" and a "figure_id".
179-
message = json.loads(message)
180-
if message['type'] == 'closing':
170+
def _handle_message(self, object, content, buffers):
171+
# Every content has a "type".
172+
if content['type'] == 'closing':
181173
self._closed = True
182-
elif message['type'] == 'initialized':
174+
elif content['type'] == 'initialized':
183175
_, _, w, h = self.figure.bbox.bounds
184176
self.manager.resize(w, h)
185177
else:
186-
self.manager.handle_json(message)
178+
self.manager.handle_json(content)
187179

188180
def send_json(self, content):
189181
self.send({'data': json.dumps(content)})
@@ -201,7 +193,7 @@ def stop_event_loop(self):
201193
FigureCanvasBase.stop_event_loop_default(self)
202194

203195

204-
class FigureManagerNbAgg(FigureManagerWebAgg):
196+
class FigureManager(FigureManagerWebAgg):
205197
ToolbarCls = Toolbar
206198

207199
def __init__(self, canvas, num):
@@ -237,10 +229,10 @@ def new_figure_manager_given_figure(num, figure):
237229
def closer(event):
238230
Gcf.destroy(num)
239231

240-
canvas = FigureCanvasNbAgg(figure)
232+
canvas = Canvas(figure)
241233
if 'nbagg.transparent' in set(rcParams.keys()) and rcParams['nbagg.transparent']:
242234
figure.patch.set_alpha(0)
243-
manager = FigureManagerNbAgg(canvas, num)
235+
manager = FigureManager(canvas, num)
244236

245237
if is_interactive():
246238
manager.show()

0 commit comments

Comments
 (0)