You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"For LaTeX (wrap with `$` and use a raw Python string):\n",
59
+
"\n",
60
+
"\\bigcirc\n",
61
+
"\n",
62
+
"For JavaScript:\n",
63
+
"\n",
64
+
" alert('I am a circle!');\n",
65
+
"\n",
66
+
"After you write the class, create an instance and then use `display_html`, `display_svg`, `display_latex` and `display_javascript` to display those representations."
67
+
]
68
+
},
69
+
{
70
+
"cell_type": "markdown",
71
+
"metadata": {},
72
+
"source": [
73
+
"### Solution"
74
+
]
75
+
},
76
+
{
77
+
"cell_type": "markdown",
78
+
"metadata": {},
79
+
"source": [
80
+
"Here is the solution to the simple `MyCircle` class:"
81
+
]
82
+
},
83
+
{
84
+
"cell_type": "code",
85
+
"execution_count": null,
86
+
"metadata": {
87
+
"collapsed": false
88
+
},
89
+
"outputs": [],
90
+
"source": [
91
+
"%load soln/mycircle.py"
92
+
]
93
+
},
94
+
{
95
+
"cell_type": "markdown",
96
+
"metadata": {},
97
+
"source": [
98
+
"Now create an instance and use the display methods:"
99
+
]
100
+
},
101
+
{
102
+
"cell_type": "code",
103
+
"execution_count": null,
104
+
"metadata": {
105
+
"collapsed": false
106
+
},
107
+
"outputs": [],
108
+
"source": [
109
+
"c = MyCircle()"
110
+
]
111
+
},
112
+
{
113
+
"cell_type": "code",
114
+
"execution_count": null,
115
+
"metadata": {
116
+
"collapsed": false
117
+
},
118
+
"outputs": [],
119
+
"source": [
120
+
"display(c)"
121
+
]
122
+
},
123
+
{
124
+
"cell_type": "code",
125
+
"execution_count": null,
126
+
"metadata": {
127
+
"collapsed": false
128
+
},
129
+
"outputs": [],
130
+
"source": [
131
+
"display_html(c)"
132
+
]
133
+
},
134
+
{
135
+
"cell_type": "code",
136
+
"execution_count": null,
137
+
"metadata": {
138
+
"collapsed": false
139
+
},
140
+
"outputs": [],
141
+
"source": [
142
+
"display_svg(c)"
143
+
]
144
+
},
145
+
{
146
+
"cell_type": "code",
147
+
"execution_count": null,
148
+
"metadata": {
149
+
"collapsed": false
150
+
},
151
+
"outputs": [],
152
+
"source": [
153
+
"display_latex(c)"
154
+
]
155
+
},
156
+
{
157
+
"cell_type": "code",
158
+
"execution_count": null,
159
+
"metadata": {
160
+
"collapsed": false
161
+
},
162
+
"outputs": [],
163
+
"source": [
164
+
"display_javascript(c)"
165
+
]
166
+
},
167
+
{
168
+
"cell_type": "markdown",
169
+
"metadata": {},
170
+
"source": [
171
+
"## PNG formatter for `MyCircle`"
172
+
]
173
+
},
174
+
{
175
+
"cell_type": "code",
176
+
"execution_count": null,
177
+
"metadata": {
178
+
"collapsed": false
179
+
},
180
+
"outputs": [],
181
+
"source": [
182
+
"%matplotlib inline\n",
183
+
"from matplotlib import pyplot as plt"
184
+
]
185
+
},
186
+
{
187
+
"cell_type": "markdown",
188
+
"metadata": {},
189
+
"source": [
190
+
"Now let's assume that the `MyCircle` class has already been defined and add a PNG representation using a formatter display function. Here is a function that converts a `MyCircle` instance to raw PNG data."
"\"\"\"Render AnotherCircle to png data using matplotlib\"\"\"\n",
205
+
" fig, ax = plt.subplots()\n",
206
+
" patch = plt.Circle(circle.center,\n",
207
+
" radius=circle.radius,\n",
208
+
" fc=circle.color,\n",
209
+
" )\n",
210
+
" ax.add_patch(patch)\n",
211
+
" plt.axis('scaled')\n",
212
+
" data = print_figure(fig, 'png')\n",
213
+
" # We MUST close the figure, otherwise IPython's display machinery\n",
214
+
" # will pick it up and send it as output, resulting in a double display\n",
215
+
" plt.close(fig)\n",
216
+
" return data"
217
+
]
218
+
},
219
+
{
220
+
"cell_type": "markdown",
221
+
"metadata": {},
222
+
"source": [
223
+
"Now use the IPython API to get the PNG formatter (`image/png`) and call the `for_type` method to register `circle_to_png` as the display function for `MyCircle`."
224
+
]
225
+
},
226
+
{
227
+
"cell_type": "code",
228
+
"execution_count": null,
229
+
"metadata": {
230
+
"collapsed": false
231
+
},
232
+
"outputs": [],
233
+
"source": [
234
+
"%load soln/mycircle_png.py"
235
+
]
236
+
},
237
+
{
238
+
"cell_type": "code",
239
+
"execution_count": null,
240
+
"metadata": {
241
+
"collapsed": false
242
+
},
243
+
"outputs": [],
244
+
"source": [
245
+
"display_png(c)"
246
+
]
247
+
},
248
+
{
249
+
"cell_type": "markdown",
250
+
"metadata": {},
251
+
"source": [
252
+
"## PNG formatter for NumPy arrays"
253
+
]
254
+
},
255
+
{
256
+
"cell_type": "markdown",
257
+
"metadata": {},
258
+
"source": [
259
+
"In this exercise, you will register a display formatter function that generates a PNG representation of a 2d NumPy array. Here is the function that uses the [Python Imaging Library (PIL)](http://www.pythonware.com/products/pil/) to generate the raw PNG data:"
260
+
]
261
+
},
262
+
{
263
+
"cell_type": "code",
264
+
"execution_count": null,
265
+
"metadata": {
266
+
"collapsed": false
267
+
},
268
+
"outputs": [],
269
+
"source": [
270
+
"from PIL import Image\n",
271
+
"from io import BytesIO\n",
272
+
"import numpy as np\n",
273
+
"\n",
274
+
"def ndarray_to_png(x):\n",
275
+
" if len(x.shape) != 2: return\n",
276
+
" x = np.asarray(Image.fromarray(x).resize((500, 500)))\n",
0 commit comments