-
Notifications
You must be signed in to change notification settings - Fork 1
/
svgwidgets.py
327 lines (203 loc) · 10.6 KB
/
svgwidgets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
## Copyright 2014 Tom Brown ([email protected])
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 3 of the
## License, or (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
"""SVG iPython Widgets
A library to manipulate Scalar Vector Graphics in the iPython notebook
using Widgets, which synchronise between the Python backend and the
Javascript frontend.
Usage from the iPython notebook to start an SVG drawing GUI:
import svgwidgets
builder = svgwidgets.SVGBuilderWidget()
builder
"""
# make the code as Python 3 compatible as possible
from __future__ import print_function, division
__version__ = "0.4.1"
__author__ = "Tom Brown"
__copyright__ = "Copyright 2014 Tom Brown, GNU GPL 3"
# get basic Widget definitions
from IPython.html import widgets
# get infrastructure for displaying in iPython notebook
from IPython.display import display, Javascript
# the traitlets take care of communication between frontend and backend
from IPython.utils import traitlets
# use json to send data to the frontend
import json, sys
class GeneralSVGWidget(object):
"""GeneralSVGWidget defines the most basic SVG element properties,
including the XML tag name and a list of attributes with default
values. Later in this module, the attributes and _view_name are
promoted to traitlets, to avoid lots of boilerplate.
"""
tag_name = ""
attributes = {}
# message the GUI to get element.outerHTML
def get_html(self):
self.send({"message_type" : "get_html"})
class FertileSVGWidget(widgets.ContainerWidget,GeneralSVGWidget):
"""FertileSVGWidgets can have child elements (like the svg and g
elements) and therefore inherit from widgets.ContainerWidget.
"""
def __init__(self, **kwargs):
widgets.ContainerWidget.__init__(self,**kwargs)
self.on_msg(self._handle_message)
# deal with messages as they come in from the GUI
def _handle_message(self,item,content):
if content["message_type"] == "html":
print(content["html"])
elif content["message_type"] == "new":
class_name = content["class_name"]
klass = svg_class_dict[class_name]
child = klass()
for attribute in content["attributes"]:
value = content["attributes"][attribute]
#cast any string floats to floats
if type(getattr(child,attribute)) == float:
value = float(value)
setattr(child,attribute,value)
self.children = self.children + (child,)
class InfertileSVGWidget(widgets.DOMWidget,GeneralSVGWidget):
"""InfertileSVGWidgets cannot have child elements (like the rect and
circle elements) and therefore inherit from widgets.DOMWidget.
"""
def __init__(self, **kwargs):
widgets.DOMWidget.__init__(self,**kwargs)
self.on_msg(self._handle_message)
# deal with messages as they come in from the GUI
def _handle_message(self,item,content):
if content["message_type"] == "html":
print(content["html"])
# now come the basic SVG elements
class SVGWidget(FertileSVGWidget):
tag_name = "svg"
attributes = {"width" : 600, "height" : 400}
# the SVG element has various traitlets for the drawing GUI
gui_controls = {"mode": "select", "fill": "blue", "fill_opacity": 0.5, "stroke": "red", "stroke_width": 3}
class GroupWidget(FertileSVGWidget):
tag_name = "g"
attributes = {"transform": ""}
class RectWidget(InfertileSVGWidget):
tag_name = "rect"
attributes = {"x" : 10,"y" : 10, "width" : 100,"height" : 50, "fill" : "blue", "fill_opacity" : 0.5, "stroke" : "red", "stroke_width" : 3, "transform" : ""}
class CircleWidget(InfertileSVGWidget):
tag_name = "circle"
attributes = {"cx" : 50,"cy" : 110, "r" : 20, "fill" : "red", "fill_opacity" : 0.5, "stroke" : "green", "stroke_width" : 3, "transform": ""}
class EllipseWidget(InfertileSVGWidget):
tag_name = "ellipse"
attributes = {"cx" : 250,"cy" : 110, "rx" : 20, "ry" : 10, "fill" : "magenta", "fill_opacity" : 0.5, "stroke" : "cyan", "stroke_width" : 3, "transform": ""}
class LineWidget(InfertileSVGWidget):
tag_name = "line"
attributes = {"x1" : 10,"y1" : 200,"x2" : 100,"y2" : 150, "stroke" : "orange", "stroke_width" : 3, "transform": ""}
class PathWidget(InfertileSVGWidget):
tag_name = "path"
attributes = {"d" : "M 10,250 C70,150 200,150 200,250", "stroke" : "black", "stroke_width" : 3, "fill" : "cyan", "fill_opacity" : 0.5, "transform": ""}
class TextWidget(InfertileSVGWidget):
tag_name = "text"
attributes = {"x" : 100, "y" : 100, "fill" : "black"}
content = "Hello World!"
# following class definition borrowed from cirq https://github.com/ntezak/cirq
class HorizontalContainerWidget(widgets.ContainerWidget):
"""
Equivalent to a `ContainerWidget` but with a css-class `hbox` instead of `vbox` for horizontal layout.
"""
def __init__(self, **kwargs):
super(self.__class__, self).__init__(**kwargs)
self.on_displayed(self._make_horizontal)
def _make_horizontal(self,_):
self.remove_class("vbox")
self.add_class("hbox")
class SVGBuilderWidget(widgets.ContainerWidget):
"""SVGBuilderWidget is a wrapper for an SVG drawing GUI."""
def __init__(self, **kwargs):
super(self.__class__, self).__init__(**kwargs)
colours = ["black","red","orange","yellow","green","blue","magenta","cyan"]
# allow toggling of SVG draw mode
draw_toggle_values = ["select","rect","circle","ellipse","line","path"]
self.draw_toggle = widgets.ToggleButtonsWidget(values = draw_toggle_values,description="Choose drawing tool:")
# set up the stroke controls
self.stroke_picker = widgets.DropdownWidget(values = colours,description="Stroke colour:")
self.stroke_width_slider = widgets.FloatSliderWidget(min=0,max=20,value=3,description="Stroke width:")
self.stroke_container = HorizontalContainerWidget()
self.stroke_container.children = [self.stroke_picker,self.stroke_width_slider]
# set up the fill controls
self.fill_picker = widgets.DropdownWidget(values = ["none"] + colours,description="Fill colour:")
self.fill_opacity_slider = widgets.FloatSliderWidget(min=0,max=1,value=0.5,description="Fill opacity:")
self.fill_container = HorizontalContainerWidget()
self.fill_container.children = [self.fill_picker,self.fill_opacity_slider]
# the main SVG
self.svg = SVGWidget()
# border the SVG
self.svg.set_css('border', '1px solid black')
# link the control widgets to the SVG control variables
self.mode_link = traitlets.link((self.draw_toggle, 'value'), (self.svg, 'mode'))
self.stroke_link = traitlets.link((self.stroke_picker, 'value'), (self.svg, 'stroke'))
self.stroke_width_link = traitlets.link((self.stroke_width_slider, 'value'), (self.svg, 'stroke_width'))
self.fill_link = traitlets.link((self.fill_picker, 'value'), (self.svg, 'fill'))
self.fill_opacity_link = traitlets.link((self.fill_opacity_slider, 'value'), (self.svg, 'fill_opacity'))
# define the main container's children
self.children = [self.draw_toggle,self.stroke_container,self.fill_container,self.svg]
#
# get all SVG element Widget classes, store them in svg_class_dict,
# and make all attributes and _view_name into traitlets
#
svg_class_dict = {}
widget_class_names = list(filter(lambda name: name.endswith("Widget"), locals()))
for class_name in widget_class_names:
this_module = sys.modules[__name__]
klass = getattr(this_module,class_name)
# classify whether the class can have children based on parent class
base_class = klass.__bases__[0]
if base_class.__name__ == "InfertileSVGWidget":
klass.fertile = False
klass.draggable = True
elif base_class.__name__ == "FertileSVGWidget":
klass.fertile = True
klass.draggable = False
else:
continue
svg_class_dict[class_name] = klass
# store the class attributes to be promoted to traitlets along
# with their default values
traitlets_dict = klass.attributes.copy()
# add the view name for Javascript as a traitlet
traitlets_dict.update({"_view_name" : class_name.replace("Widget","View")})
# the GUI controls for the SVGWidget must also be trailets
if class_name == "SVGWidget":
traitlets_dict.update(klass.gui_controls)
# the content for the TextWidget must also be a traitlet
elif class_name == "TextWidget":
traitlets_dict.update({"content" : klass.content})
# now define the trailets with the correct traitlet.Type
for name,default in traitlets_dict.items():
try:
default = float(default)
attribute_type = "float"
except:
attribute_type = "string"
if attribute_type == "float":
setattr(klass,name,traitlets.Float(default, sync=True))
elif attribute_type == "string":
setattr(klass,name,traitlets.Unicode(default, sync=True))
traitlet = getattr(klass,name)
# normally set by metaclass IPython.utils.traitlets.MetaHasTraits.__new__
traitlet.name = name
# normally set by metaclass IPython.utils.traitlets.MetaHasTraits.__init__
traitlet.this_class = klass
# prepare data to send to Javascript
widget_properties = { class_name : {"tag_name" : klass.tag_name,
"fertile" : klass.fertile,
"view_name" : class_name.replace("Widget","View"),
"attributes" : klass.attributes,
"draggable" : klass.draggable} for class_name,klass in svg_class_dict.items() }
# set data in global scope
display(Javascript("window.widget_properties = " + json.dumps(widget_properties)))
# execute code to set up Javascript View classes
display(Javascript("svgwidgets.js"))