Show More
@@ -1,612 +1,612 b'' | |||||
1 | { |
|
1 | { | |
2 | "cells": [ |
|
2 | "cells": [ | |
3 | { |
|
3 | { | |
4 | "cell_type": "markdown", |
|
4 | "cell_type": "markdown", | |
5 | "metadata": {}, |
|
5 | "metadata": {}, | |
6 | "source": [ |
|
6 | "source": [ | |
7 |
"# Embra |
|
7 | "# Embracing web standards" | |
8 | ] |
|
8 | ] | |
9 | }, |
|
9 | }, | |
10 | { |
|
10 | { | |
11 | "cell_type": "markdown", |
|
11 | "cell_type": "markdown", | |
12 | "metadata": {}, |
|
12 | "metadata": {}, | |
13 | "source": [ |
|
13 | "source": [ | |
14 |
"One of the main reason that allowed us to develop |
|
14 | "One of the main reason that allowed us to develop the current notebook web application \n", | |
15 |
"was to embra |
|
15 | "was to embrace the web technology. \n", | |
16 | "\n", |
|
16 | "\n", | |
17 |
"By be |
|
17 | "By being a pure web application using HTML, JavaScript and CSS, the Notebook can get \n", | |
18 | "all the web technology improvement for free. Thus, as browsers support for different \n", |
|
18 | "all the web technology improvement for free. Thus, as browsers' support for different \n", | |
19 |
"media extend, |
|
19 | "media extend, the notebook web app should be able to be compatible without modification. \n", | |
20 | "\n", |
|
20 | "\n", | |
21 |
"This is also true with performance of the User Interface as the speed of |
|
21 | "This is also true with performance of the User Interface as the speed of JavaScript VM increase. " | |
22 | ] |
|
22 | ] | |
23 | }, |
|
23 | }, | |
24 | { |
|
24 | { | |
25 | "cell_type": "markdown", |
|
25 | "cell_type": "markdown", | |
26 | "metadata": {}, |
|
26 | "metadata": {}, | |
27 | "source": [ |
|
27 | "source": [ | |
28 |
"The other advantage of using only web technolog |
|
28 | "The other advantage of using only web technologies is that the code of the interface is fully accessible to the end user, and modifiable live.\n", | |
29 | "Even if this task is not always easy, we strive to keep our code as accessible and reusable as possible.\n", |
|
29 | "Even if this task is not always easy, we strive to keep our code as accessible and reusable as possible.\n", | |
30 |
"This should allow |
|
30 | "This should allow for relatively simple development of small extensions that customize the behavior of the web interface. " | |
31 | ] |
|
31 | ] | |
32 | }, |
|
32 | }, | |
33 | { |
|
33 | { | |
34 | "cell_type": "markdown", |
|
34 | "cell_type": "markdown", | |
35 | "metadata": {}, |
|
35 | "metadata": {}, | |
36 | "source": [ |
|
36 | "source": [ | |
37 | "## Tempering with Notebook app" |
|
37 | "## Tempering with Notebook app" | |
38 | ] |
|
38 | ] | |
39 | }, |
|
39 | }, | |
40 | { |
|
40 | { | |
41 | "cell_type": "markdown", |
|
41 | "cell_type": "markdown", | |
42 | "metadata": {}, |
|
42 | "metadata": {}, | |
43 | "source": [ |
|
43 | "source": [ | |
44 |
"The first tool that is avail |
|
44 | "The first tool that is available to you and that you should be aware of are browser \"developers tool\". The exact naming can change across browsers, and might require the installation of extensions. But basically they can allow you to inspect/modify the DOM, and interact with the JavaScript code that runs the frontend.\n", | |
45 | "\n", |
|
45 | "\n", | |
46 |
" - In Chrome and |
|
46 | " - In Chrome and Safari, Developer tools are in the menu "More Tools" \n", | |
47 |
" - In |
|
47 | " - In Firefox you might need to install [Firebug](http://getfirebug.com/)\n", | |
48 | " - others ?\n", |
|
48 | " - others ?\n", | |
49 | " \n", |
|
49 | " \n", | |
50 | "Those will be your best friends to debug and try different approach for your extensions." |
|
50 | "Those will be your best friends to debug and try different approach for your extensions." | |
51 | ] |
|
51 | ] | |
52 | }, |
|
52 | }, | |
53 | { |
|
53 | { | |
54 | "cell_type": "markdown", |
|
54 | "cell_type": "markdown", | |
55 | "metadata": {}, |
|
55 | "metadata": {}, | |
56 | "source": [ |
|
56 | "source": [ | |
57 | "### Injecting JS" |
|
57 | "### Injecting JS" | |
58 | ] |
|
58 | ] | |
59 | }, |
|
59 | }, | |
60 | { |
|
60 | { | |
61 | "cell_type": "markdown", |
|
61 | "cell_type": "markdown", | |
62 | "metadata": {}, |
|
62 | "metadata": {}, | |
63 | "source": [ |
|
63 | "source": [ | |
64 | "#### using magics" |
|
64 | "#### using magics" | |
65 | ] |
|
65 | ] | |
66 | }, |
|
66 | }, | |
67 | { |
|
67 | { | |
68 | "cell_type": "markdown", |
|
68 | "cell_type": "markdown", | |
69 | "metadata": {}, |
|
69 | "metadata": {}, | |
70 | "source": [ |
|
70 | "source": [ | |
71 |
" |
|
71 | "The above tools can be tedious to edit long JavaScript files. Hopefully we provide the `%%javascript` magic. This allows you to quickly inject JavaScript into the notebook. Still the JavaScript injected this way will not survive reloading. Hence it is a good tool for testing an refinig a script.\n", | |
72 | "\n", |
|
72 | "\n", | |
73 |
"You might see here and there people modifying |
|
73 | "You might see here and there people modifying CSS and injecting JavaScript into notebook by reading file and publishing them into the notebook.\n", | |
74 | "Not only this often break the flow of the notebook and make the re-execution of the notebook broken, but it also mean that you need to execute those cells on all the notebook every time you need to update the code.\n", |
|
74 | "Not only this often break the flow of the notebook and make the re-execution of the notebook broken, but it also mean that you need to execute those cells on all the notebook every time you need to update the code.\n", | |
75 | "\n", |
|
75 | "\n", | |
76 |
"This can still be usefull in some cases, like the `%autosave` magic that allows to control the time between each save. But this can be replaced by a Java |
|
76 | "This can still be usefull in some cases, like the `%autosave` magic that allows to control the time between each save. But this can be replaced by a JavaScript dropdown menu to select save interval." | |
77 | ] |
|
77 | ] | |
78 | }, |
|
78 | }, | |
79 | { |
|
79 | { | |
80 | "cell_type": "code", |
|
80 | "cell_type": "code", | |
81 | "execution_count": null, |
|
81 | "execution_count": null, | |
82 | "metadata": { |
|
82 | "metadata": { | |
83 | "collapsed": false |
|
83 | "collapsed": false | |
84 | }, |
|
84 | }, | |
85 | "outputs": [], |
|
85 | "outputs": [], | |
86 | "source": [ |
|
86 | "source": [ | |
87 | "## you can inspect the autosave code to see what it does.\n", |
|
87 | "## you can inspect the autosave code to see what it does.\n", | |
88 | "%autosave??" |
|
88 | "%autosave??" | |
89 | ] |
|
89 | ] | |
90 | }, |
|
90 | }, | |
91 | { |
|
91 | { | |
92 | "cell_type": "markdown", |
|
92 | "cell_type": "markdown", | |
93 | "metadata": {}, |
|
93 | "metadata": {}, | |
94 | "source": [ |
|
94 | "source": [ | |
95 | "#### custom.js" |
|
95 | "#### custom.js" | |
96 | ] |
|
96 | ] | |
97 | }, |
|
97 | }, | |
98 | { |
|
98 | { | |
99 | "cell_type": "markdown", |
|
99 | "cell_type": "markdown", | |
100 | "metadata": {}, |
|
100 | "metadata": {}, | |
101 | "source": [ |
|
101 | "source": [ | |
102 |
"To inject Java |
|
102 | "To inject JavaScript we provide an entry point: `custom.js` that allow teh user to execute and load other resources into the notebook.\n", | |
103 |
"Java |
|
103 | "JavaScript code in `custom.js` will be executed when the notebook app start and can then be used to customize almost anything in the UI and in the behavior of the notebook.\n", | |
104 | "\n", |
|
104 | "\n", | |
105 | "`custom.js` can be found in IPython profile dir, and so you can have different UI modification on a per profile basis, as well as share your modfication with others." |
|
105 | "`custom.js` can be found in IPython profile dir, and so you can have different UI modification on a per profile basis, as well as share your modfication with others." | |
106 | ] |
|
106 | ] | |
107 | }, |
|
107 | }, | |
108 | { |
|
108 | { | |
109 | "cell_type": "markdown", |
|
109 | "cell_type": "markdown", | |
110 | "metadata": {}, |
|
110 | "metadata": {}, | |
111 | "source": [ |
|
111 | "source": [ | |
112 | "##### Because we like you...." |
|
112 | "##### Because we like you...." | |
113 | ] |
|
113 | ] | |
114 | }, |
|
114 | }, | |
115 | { |
|
115 | { | |
116 | "cell_type": "markdown", |
|
116 | "cell_type": "markdown", | |
117 | "metadata": {}, |
|
117 | "metadata": {}, | |
118 | "source": [ |
|
118 | "source": [ | |
119 | "You have been provided with an already existing profile folder with this tutorial...\n", |
|
119 | "You have been provided with an already existing profile folder with this tutorial...\n", | |
120 | "start the notebook from the root of the tutorial directory with :\n", |
|
120 | "start the notebook from the root of the tutorial directory with :\n", | |
121 | "\n", |
|
121 | "\n", | |
122 | "```bash\n", |
|
122 | "```bash\n", | |
123 | "$ ipython notebook --ProfileDir.location=./profile_euroscipy\n", |
|
123 | "$ ipython notebook --ProfileDir.location=./profile_euroscipy\n", | |
124 | "```" |
|
124 | "```" | |
125 | ] |
|
125 | ] | |
126 | }, |
|
126 | }, | |
127 | { |
|
127 | { | |
128 | "cell_type": "markdown", |
|
128 | "cell_type": "markdown", | |
129 | "metadata": {}, |
|
129 | "metadata": {}, | |
130 | "source": [ |
|
130 | "source": [ | |
131 | "##### but back to theory" |
|
131 | "##### but back to theory" | |
132 | ] |
|
132 | ] | |
133 | }, |
|
133 | }, | |
134 | { |
|
134 | { | |
135 | "cell_type": "code", |
|
135 | "cell_type": "code", | |
136 | "execution_count": null, |
|
136 | "execution_count": null, | |
137 | "metadata": { |
|
137 | "metadata": { | |
138 | "collapsed": false |
|
138 | "collapsed": false | |
139 | }, |
|
139 | }, | |
140 | "outputs": [], |
|
140 | "outputs": [], | |
141 | "source": [ |
|
141 | "source": [ | |
142 | "profile_dir = ! ipython locate\n", |
|
142 | "profile_dir = ! ipython locate\n", | |
143 | "profile_dir = profile_dir[0]\n", |
|
143 | "profile_dir = profile_dir[0]\n", | |
144 | "profile_dir" |
|
144 | "profile_dir" | |
145 | ] |
|
145 | ] | |
146 | }, |
|
146 | }, | |
147 | { |
|
147 | { | |
148 | "cell_type": "markdown", |
|
148 | "cell_type": "markdown", | |
149 | "metadata": {}, |
|
149 | "metadata": {}, | |
150 | "source": [ |
|
150 | "source": [ | |
151 | "and custom js is in " |
|
151 | "and custom js is in " | |
152 | ] |
|
152 | ] | |
153 | }, |
|
153 | }, | |
154 | { |
|
154 | { | |
155 | "cell_type": "code", |
|
155 | "cell_type": "code", | |
156 | "execution_count": null, |
|
156 | "execution_count": null, | |
157 | "metadata": { |
|
157 | "metadata": { | |
158 | "collapsed": false |
|
158 | "collapsed": false | |
159 | }, |
|
159 | }, | |
160 | "outputs": [], |
|
160 | "outputs": [], | |
161 | "source": [ |
|
161 | "source": [ | |
162 | "import os.path\n", |
|
162 | "import os.path\n", | |
163 | "custom_js_path = os.path.join(profile_dir,'profile_default','static','custom','custom.js')" |
|
163 | "custom_js_path = os.path.join(profile_dir,'profile_default','static','custom','custom.js')" | |
164 | ] |
|
164 | ] | |
165 | }, |
|
165 | }, | |
166 | { |
|
166 | { | |
167 | "cell_type": "code", |
|
167 | "cell_type": "code", | |
168 | "execution_count": null, |
|
168 | "execution_count": null, | |
169 | "metadata": { |
|
169 | "metadata": { | |
170 | "collapsed": false |
|
170 | "collapsed": false | |
171 | }, |
|
171 | }, | |
172 | "outputs": [], |
|
172 | "outputs": [], | |
173 | "source": [ |
|
173 | "source": [ | |
174 | "# my custom js\n", |
|
174 | "# my custom js\n", | |
175 | "with open(custom_js_path) as f:\n", |
|
175 | "with open(custom_js_path) as f:\n", | |
176 | " for l in f: \n", |
|
176 | " for l in f: \n", | |
177 | " print l," |
|
177 | " print l," | |
178 | ] |
|
178 | ] | |
179 | }, |
|
179 | }, | |
180 | { |
|
180 | { | |
181 | "cell_type": "markdown", |
|
181 | "cell_type": "markdown", | |
182 | "metadata": {}, |
|
182 | "metadata": {}, | |
183 | "source": [ |
|
183 | "source": [ | |
184 | "Note that `custom.js` is ment to be modified by user, when writing a script, you can define it in a separate file and add a line of configuration into `custom.js` that will fetch and execute the file." |
|
184 | "Note that `custom.js` is ment to be modified by user, when writing a script, you can define it in a separate file and add a line of configuration into `custom.js` that will fetch and execute the file." | |
185 | ] |
|
185 | ] | |
186 | }, |
|
186 | }, | |
187 | { |
|
187 | { | |
188 | "cell_type": "markdown", |
|
188 | "cell_type": "markdown", | |
189 | "metadata": {}, |
|
189 | "metadata": {}, | |
190 | "source": [ |
|
190 | "source": [ | |
191 | "**Warning** : even if modification of `custom.js` take effect immediately after browser refresh (except if browser cache is aggressive), *creating* a file in `static/` directory need a **server restart**." |
|
191 | "**Warning** : even if modification of `custom.js` take effect immediately after browser refresh (except if browser cache is aggressive), *creating* a file in `static/` directory need a **server restart**." | |
192 | ] |
|
192 | ] | |
193 | }, |
|
193 | }, | |
194 | { |
|
194 | { | |
195 | "cell_type": "markdown", |
|
195 | "cell_type": "markdown", | |
196 | "metadata": {}, |
|
196 | "metadata": {}, | |
197 | "source": [ |
|
197 | "source": [ | |
198 | "## Exercise :" |
|
198 | "## Exercise :" | |
199 | ] |
|
199 | ] | |
200 | }, |
|
200 | }, | |
201 | { |
|
201 | { | |
202 | "cell_type": "markdown", |
|
202 | "cell_type": "markdown", | |
203 | "metadata": {}, |
|
203 | "metadata": {}, | |
204 | "source": [ |
|
204 | "source": [ | |
205 | " - Create a `custom.js` in the right location with the following content:\n", |
|
205 | " - Create a `custom.js` in the right location with the following content:\n", | |
206 | "```javascript\n", |
|
206 | "```javascript\n", | |
207 | "alert(\"hello world from custom.js\")\n", |
|
207 | "alert(\"hello world from custom.js\")\n", | |
208 | "```\n", |
|
208 | "```\n", | |
209 | "\n", |
|
209 | "\n", | |
210 | " - Restart your server and open any notebook.\n", |
|
210 | " - Restart your server and open any notebook.\n", | |
211 | " - Be greeted by custom.js" |
|
211 | " - Be greeted by custom.js" | |
212 | ] |
|
212 | ] | |
213 | }, |
|
213 | }, | |
214 | { |
|
214 | { | |
215 | "cell_type": "markdown", |
|
215 | "cell_type": "markdown", | |
216 | "metadata": {}, |
|
216 | "metadata": {}, | |
217 | "source": [ |
|
217 | "source": [ | |
218 | "Have a look at [default custom.js](https://github.com/ipython/ipython/blob/1.x/IPython/html/static/custom/custom.js), to see it's content and some more explanation." |
|
218 | "Have a look at [default custom.js](https://github.com/ipython/ipython/blob/1.x/IPython/html/static/custom/custom.js), to see it's content and some more explanation." | |
219 | ] |
|
219 | ] | |
220 | }, |
|
220 | }, | |
221 | { |
|
221 | { | |
222 | "cell_type": "markdown", |
|
222 | "cell_type": "markdown", | |
223 | "metadata": {}, |
|
223 | "metadata": {}, | |
224 | "source": [ |
|
224 | "source": [ | |
225 | "#### For the quick ones : " |
|
225 | "#### For the quick ones : " | |
226 | ] |
|
226 | ] | |
227 | }, |
|
227 | }, | |
228 | { |
|
228 | { | |
229 | "cell_type": "markdown", |
|
229 | "cell_type": "markdown", | |
230 | "metadata": {}, |
|
230 | "metadata": {}, | |
231 | "source": [ |
|
231 | "source": [ | |
232 | "We've seen above that you can change the autosave rate by using a magic. This is typically something I don't want to type everytime, and that I don't like to embed into my workwlow and documents. (reader don't care what my autosave time is), let's build an extension that allow to do it. " |
|
232 | "We've seen above that you can change the autosave rate by using a magic. This is typically something I don't want to type everytime, and that I don't like to embed into my workwlow and documents. (reader don't care what my autosave time is), let's build an extension that allow to do it. " | |
233 | ] |
|
233 | ] | |
234 | }, |
|
234 | }, | |
235 | { |
|
235 | { | |
236 | "cell_type": "markdown", |
|
236 | "cell_type": "markdown", | |
237 | "metadata": { |
|
237 | "metadata": { | |
238 | "foo": true |
|
238 | "foo": true | |
239 | }, |
|
239 | }, | |
240 | "source": [ |
|
240 | "source": [ | |
241 | "Create a dropdow elemement in the toolbar (DOM `IPython.toolbar.element`), you will need \n", |
|
241 | "Create a dropdow elemement in the toolbar (DOM `IPython.toolbar.element`), you will need \n", | |
242 | "\n", |
|
242 | "\n", | |
243 | "- `IPython.notebook.set_autosave_interval(miliseconds)`\n", |
|
243 | "- `IPython.notebook.set_autosave_interval(miliseconds)`\n", | |
244 | "- know that 1min = 60 sec, and 1 sec = 1000 ms" |
|
244 | "- know that 1min = 60 sec, and 1 sec = 1000 ms" | |
245 | ] |
|
245 | ] | |
246 | }, |
|
246 | }, | |
247 | { |
|
247 | { | |
248 | "cell_type": "markdown", |
|
248 | "cell_type": "markdown", | |
249 | "metadata": {}, |
|
249 | "metadata": {}, | |
250 | "source": [ |
|
250 | "source": [ | |
251 | "```javascript\n", |
|
251 | "```javascript\n", | |
252 | "\n", |
|
252 | "\n", | |
253 | "var label = jQuery('<label/>').text('AutoScroll Limit:');\n", |
|
253 | "var label = jQuery('<label/>').text('AutoScroll Limit:');\n", | |
254 | "var select = jQuery('<select/>')\n", |
|
254 | "var select = jQuery('<select/>')\n", | |
255 | " //.append(jQuery('<option/>').attr('value', '2').text('2min (default)'))\n", |
|
255 | " //.append(jQuery('<option/>').attr('value', '2').text('2min (default)'))\n", | |
256 | " .append(jQuery('<option/>').attr('value', undefined).text('disabled'))\n", |
|
256 | " .append(jQuery('<option/>').attr('value', undefined).text('disabled'))\n", | |
257 | "\n", |
|
257 | "\n", | |
258 | " // TODO:\n", |
|
258 | " // TODO:\n", | |
259 | " //the_toolbar_element.append(label)\n", |
|
259 | " //the_toolbar_element.append(label)\n", | |
260 | " //the_toolbar_element.append(select);\n", |
|
260 | " //the_toolbar_element.append(select);\n", | |
261 | " \n", |
|
261 | " \n", | |
262 | "select.change(function() {\n", |
|
262 | "select.change(function() {\n", | |
263 | " var val = jQuery(this).val() // val will be the value in [2]\n", |
|
263 | " var val = jQuery(this).val() // val will be the value in [2]\n", | |
264 | " // TODO\n", |
|
264 | " // TODO\n", | |
265 | " // this will be called when dropdown changes\n", |
|
265 | " // this will be called when dropdown changes\n", | |
266 | "\n", |
|
266 | "\n", | |
267 | "});\n", |
|
267 | "});\n", | |
268 | "\n", |
|
268 | "\n", | |
269 | "var time_m = [1,5,10,15,30];\n", |
|
269 | "var time_m = [1,5,10,15,30];\n", | |
270 | "for (var i=0; i < time_m.length; i++) {\n", |
|
270 | "for (var i=0; i < time_m.length; i++) {\n", | |
271 | " var ts = time_m[i];\n", |
|
271 | " var ts = time_m[i];\n", | |
272 | " //[2] ____ this will be `val` on [1] \n", |
|
272 | " //[2] ____ this will be `val` on [1] \n", | |
273 | " // | \n", |
|
273 | " // | \n", | |
274 | " // v \n", |
|
274 | " // v \n", | |
275 | " select.append($('<option/>').attr('value', ts).text(thr+'min'));\n", |
|
275 | " select.append($('<option/>').attr('value', ts).text(thr+'min'));\n", | |
276 | " // this will fill up the dropdown `select` with\n", |
|
276 | " // this will fill up the dropdown `select` with\n", | |
277 | " // 1 min\n", |
|
277 | " // 1 min\n", | |
278 | " // 5 min\n", |
|
278 | " // 5 min\n", | |
279 | " // 10 min\n", |
|
279 | " // 10 min\n", | |
280 | " // 10 min\n", |
|
280 | " // 10 min\n", | |
281 | " // ...\n", |
|
281 | " // ...\n", | |
282 | "}\n", |
|
282 | "}\n", | |
283 | "```" |
|
283 | "```" | |
284 | ] |
|
284 | ] | |
285 | }, |
|
285 | }, | |
286 | { |
|
286 | { | |
287 | "cell_type": "markdown", |
|
287 | "cell_type": "markdown", | |
288 | "metadata": {}, |
|
288 | "metadata": {}, | |
289 | "source": [ |
|
289 | "source": [ | |
290 | "#### A non interactive example first" |
|
290 | "#### A non interactive example first" | |
291 | ] |
|
291 | ] | |
292 | }, |
|
292 | }, | |
293 | { |
|
293 | { | |
294 | "cell_type": "markdown", |
|
294 | "cell_type": "markdown", | |
295 | "metadata": {}, |
|
295 | "metadata": {}, | |
296 | "source": [ |
|
296 | "source": [ | |
297 | "I like my cython to be nicely highlighted\n", |
|
297 | "I like my cython to be nicely highlighted\n", | |
298 | "\n", |
|
298 | "\n", | |
299 | "```javascript\n", |
|
299 | "```javascript\n", | |
300 | "IPython.config.cell_magic_highlight['magic_text/x-cython'] = {}\n", |
|
300 | "IPython.config.cell_magic_highlight['magic_text/x-cython'] = {}\n", | |
301 | "IPython.config.cell_magic_highlight['magic_text/x-cython'].reg = [/^%%cython/]\n", |
|
301 | "IPython.config.cell_magic_highlight['magic_text/x-cython'].reg = [/^%%cython/]\n", | |
302 | "```\n", |
|
302 | "```\n", | |
303 | "\n", |
|
303 | "\n", | |
304 | "`text/x-cython` is the name of CodeMirror mode name, `magic_` prefix will just patch the mode so that the first line that contains a magic does not screw up the highlighting. `reg`is a list or regular expression that will trigger the change of mode." |
|
304 | "`text/x-cython` is the name of CodeMirror mode name, `magic_` prefix will just patch the mode so that the first line that contains a magic does not screw up the highlighting. `reg`is a list or regular expression that will trigger the change of mode." | |
305 | ] |
|
305 | ] | |
306 | }, |
|
306 | }, | |
307 | { |
|
307 | { | |
308 | "cell_type": "markdown", |
|
308 | "cell_type": "markdown", | |
309 | "metadata": {}, |
|
309 | "metadata": {}, | |
310 | "source": [ |
|
310 | "source": [ | |
311 | "#### Get more docs" |
|
311 | "#### Get more docs" | |
312 | ] |
|
312 | ] | |
313 | }, |
|
313 | }, | |
314 | { |
|
314 | { | |
315 | "cell_type": "markdown", |
|
315 | "cell_type": "markdown", | |
316 | "metadata": {}, |
|
316 | "metadata": {}, | |
317 | "source": [ |
|
317 | "source": [ | |
318 |
"Sadly you will have to read the js source file (but there are lots of comments) an/or build the |
|
318 | "Sadly you will have to read the js source file (but there are lots of comments) an/or build the JavaScript documentation using yuidoc.\n", | |
319 | "If you have `node` and `yui-doc` installed:" |
|
319 | "If you have `node` and `yui-doc` installed:" | |
320 | ] |
|
320 | ] | |
321 | }, |
|
321 | }, | |
322 | { |
|
322 | { | |
323 | "cell_type": "markdown", |
|
323 | "cell_type": "markdown", | |
324 | "metadata": {}, |
|
324 | "metadata": {}, | |
325 | "source": [ |
|
325 | "source": [ | |
326 | "```bash\n", |
|
326 | "```bash\n", | |
327 | "$ cd ~/ipython/IPython/html/static/notebook/js/\n", |
|
327 | "$ cd ~/ipython/IPython/html/static/notebook/js/\n", | |
328 | "$ yuidoc . --server\n", |
|
328 | "$ yuidoc . --server\n", | |
329 | "warn: (yuidoc): Failed to extract port, setting to the default :3000\n", |
|
329 | "warn: (yuidoc): Failed to extract port, setting to the default :3000\n", | |
330 | "info: (yuidoc): Starting YUIDoc@0.3.45 using YUI@3.9.1 with NodeJS@0.10.15\n", |
|
330 | "info: (yuidoc): Starting YUIDoc@0.3.45 using YUI@3.9.1 with NodeJS@0.10.15\n", | |
331 | "info: (yuidoc): Scanning for yuidoc.json file.\n", |
|
331 | "info: (yuidoc): Scanning for yuidoc.json file.\n", | |
332 | "info: (yuidoc): Starting YUIDoc with the following options:\n", |
|
332 | "info: (yuidoc): Starting YUIDoc with the following options:\n", | |
333 | "info: (yuidoc):\n", |
|
333 | "info: (yuidoc):\n", | |
334 | "{ port: 3000,\n", |
|
334 | "{ port: 3000,\n", | |
335 | " nocode: false,\n", |
|
335 | " nocode: false,\n", | |
336 | " paths: [ '.' ],\n", |
|
336 | " paths: [ '.' ],\n", | |
337 | " server: true,\n", |
|
337 | " server: true,\n", | |
338 | " outdir: './out' }\n", |
|
338 | " outdir: './out' }\n", | |
339 | "info: (yuidoc): Scanning for yuidoc.json file.\n", |
|
339 | "info: (yuidoc): Scanning for yuidoc.json file.\n", | |
340 | "info: (server): Starting server: http://127.0.0.1:3000\n", |
|
340 | "info: (server): Starting server: http://127.0.0.1:3000\n", | |
341 | "```\n", |
|
341 | "```\n", | |
342 | "\n", |
|
342 | "\n", | |
343 | "and browse http://127.0.0.1:3000 to get docs" |
|
343 | "and browse http://127.0.0.1:3000 to get docs" | |
344 | ] |
|
344 | ] | |
345 | }, |
|
345 | }, | |
346 | { |
|
346 | { | |
347 | "cell_type": "markdown", |
|
347 | "cell_type": "markdown", | |
348 | "metadata": { |
|
348 | "metadata": { | |
349 | "foo": true |
|
349 | "foo": true | |
350 | }, |
|
350 | }, | |
351 | "source": [ |
|
351 | "source": [ | |
352 | "#### Some convenience methods" |
|
352 | "#### Some convenience methods" | |
353 | ] |
|
353 | ] | |
354 | }, |
|
354 | }, | |
355 | { |
|
355 | { | |
356 | "cell_type": "markdown", |
|
356 | "cell_type": "markdown", | |
357 | "metadata": {}, |
|
357 | "metadata": {}, | |
358 | "source": [ |
|
358 | "source": [ | |
359 | "By browsing the doc you will see that we have soem convenience methods that avoid to re-invent the UI everytime :\n", |
|
359 | "By browsing the doc you will see that we have soem convenience methods that avoid to re-invent the UI everytime :\n", | |
360 | "```javascript\n", |
|
360 | "```javascript\n", | |
361 | "IPython.toolbar.add_buttons_group([\n", |
|
361 | "IPython.toolbar.add_buttons_group([\n", | |
362 | " {\n", |
|
362 | " {\n", | |
363 | " 'label' : 'run qtconsole',\n", |
|
363 | " 'label' : 'run qtconsole',\n", | |
364 | " 'icon' : 'icon-terminal', // select your icon from \n", |
|
364 | " 'icon' : 'icon-terminal', // select your icon from \n", | |
365 | " // http://fortawesome.github.io/Font-Awesome/icons/\n", |
|
365 | " // http://fortawesome.github.io/Font-Awesome/icons/\n", | |
366 | " 'callback': function(){IPython.notebook.kernel.execute('%qtconsole')}\n", |
|
366 | " 'callback': function(){IPython.notebook.kernel.execute('%qtconsole')}\n", | |
367 | " }\n", |
|
367 | " }\n", | |
368 | " // add more button here if needed.\n", |
|
368 | " // add more button here if needed.\n", | |
369 | " ]);\n", |
|
369 | " ]);\n", | |
370 | "```\n", |
|
370 | "```\n", | |
371 | "with a [lot of icons] you can select from. \n", |
|
371 | "with a [lot of icons] you can select from. \n", | |
372 | "\n", |
|
372 | "\n", | |
373 | "[lot of icons]: http://fortawesome.github.io/Font-Awesome/icons/" |
|
373 | "[lot of icons]: http://fortawesome.github.io/Font-Awesome/icons/" | |
374 | ] |
|
374 | ] | |
375 | }, |
|
375 | }, | |
376 | { |
|
376 | { | |
377 | "cell_type": "markdown", |
|
377 | "cell_type": "markdown", | |
378 | "metadata": { |
|
378 | "metadata": { | |
379 | "foo": true |
|
379 | "foo": true | |
380 | }, |
|
380 | }, | |
381 | "source": [ |
|
381 | "source": [ | |
382 | "## Cell Metadata" |
|
382 | "## Cell Metadata" | |
383 | ] |
|
383 | ] | |
384 | }, |
|
384 | }, | |
385 | { |
|
385 | { | |
386 | "cell_type": "markdown", |
|
386 | "cell_type": "markdown", | |
387 | "metadata": { |
|
387 | "metadata": { | |
388 | "foo": true |
|
388 | "foo": true | |
389 | }, |
|
389 | }, | |
390 | "source": [ |
|
390 | "source": [ | |
391 | "The most requested feature is generaly to be able to distinguish individual cell in th enotebook, or run specific action with them.\n", |
|
391 | "The most requested feature is generaly to be able to distinguish individual cell in th enotebook, or run specific action with them.\n", | |
392 | "To do so, you can either use `IPython.notebook.get_selected_cell()`, or rely on `CellToolbar`. This allow you to register aset of action and graphical element that will be attached on individual cells." |
|
392 | "To do so, you can either use `IPython.notebook.get_selected_cell()`, or rely on `CellToolbar`. This allow you to register aset of action and graphical element that will be attached on individual cells." | |
393 | ] |
|
393 | ] | |
394 | }, |
|
394 | }, | |
395 | { |
|
395 | { | |
396 | "cell_type": "markdown", |
|
396 | "cell_type": "markdown", | |
397 | "metadata": {}, |
|
397 | "metadata": {}, | |
398 | "source": [ |
|
398 | "source": [ | |
399 | "### Cell Toolbar" |
|
399 | "### Cell Toolbar" | |
400 | ] |
|
400 | ] | |
401 | }, |
|
401 | }, | |
402 | { |
|
402 | { | |
403 | "cell_type": "markdown", |
|
403 | "cell_type": "markdown", | |
404 | "metadata": {}, |
|
404 | "metadata": {}, | |
405 | "source": [ |
|
405 | "source": [ | |
406 | "You can see some example of what can be done by toggling the `Cell Toolbar` selector in the toolbar on top of the notebook. It provide two default `presets` that are `Default` and `slideshow`. Default allow edit the metadata attached to each cell manually." |
|
406 | "You can see some example of what can be done by toggling the `Cell Toolbar` selector in the toolbar on top of the notebook. It provide two default `presets` that are `Default` and `slideshow`. Default allow edit the metadata attached to each cell manually." | |
407 | ] |
|
407 | ] | |
408 | }, |
|
408 | }, | |
409 | { |
|
409 | { | |
410 | "cell_type": "markdown", |
|
410 | "cell_type": "markdown", | |
411 | "metadata": {}, |
|
411 | "metadata": {}, | |
412 | "source": [ |
|
412 | "source": [ | |
413 | "First we define a function that takes at first parameter an element on the DOM in which to inject UI element. Second element will be the cell this element will be registerd with. Then we will need to register that function ad give it a name.\n" |
|
413 | "First we define a function that takes at first parameter an element on the DOM in which to inject UI element. Second element will be the cell this element will be registerd with. Then we will need to register that function ad give it a name.\n" | |
414 | ] |
|
414 | ] | |
415 | }, |
|
415 | }, | |
416 | { |
|
416 | { | |
417 | "cell_type": "markdown", |
|
417 | "cell_type": "markdown", | |
418 | "metadata": {}, |
|
418 | "metadata": {}, | |
419 | "source": [ |
|
419 | "source": [ | |
420 | "#### Register a callback" |
|
420 | "#### Register a callback" | |
421 | ] |
|
421 | ] | |
422 | }, |
|
422 | }, | |
423 | { |
|
423 | { | |
424 | "cell_type": "code", |
|
424 | "cell_type": "code", | |
425 | "execution_count": null, |
|
425 | "execution_count": null, | |
426 | "metadata": { |
|
426 | "metadata": { | |
427 | "collapsed": false |
|
427 | "collapsed": false | |
428 | }, |
|
428 | }, | |
429 | "outputs": [], |
|
429 | "outputs": [], | |
430 | "source": [ |
|
430 | "source": [ | |
431 | "%%javascript\n", |
|
431 | "%%javascript\n", | |
432 | "var CellToolbar = IPython.CellToolbar\n", |
|
432 | "var CellToolbar = IPython.CellToolbar\n", | |
433 | "var toggle = function(div, cell) {\n", |
|
433 | "var toggle = function(div, cell) {\n", | |
434 | " var button_container = $(div)\n", |
|
434 | " var button_container = $(div)\n", | |
435 | "\n", |
|
435 | "\n", | |
436 | " // let's create a button that show the current value of the metadata\n", |
|
436 | " // let's create a button that show the current value of the metadata\n", | |
437 | " var button = $('<button/>').addClass('btn btn-mini').text(String(cell.metadata.foo));\n", |
|
437 | " var button = $('<button/>').addClass('btn btn-mini').text(String(cell.metadata.foo));\n", | |
438 | "\n", |
|
438 | "\n", | |
439 | " // On click, change the metadata value and update the button label\n", |
|
439 | " // On click, change the metadata value and update the button label\n", | |
440 | " button.click(function(){\n", |
|
440 | " button.click(function(){\n", | |
441 | " var v = cell.metadata.foo;\n", |
|
441 | " var v = cell.metadata.foo;\n", | |
442 | " cell.metadata.foo = !v;\n", |
|
442 | " cell.metadata.foo = !v;\n", | |
443 | " button.text(String(!v));\n", |
|
443 | " button.text(String(!v));\n", | |
444 | " })\n", |
|
444 | " })\n", | |
445 | "\n", |
|
445 | "\n", | |
446 | " // add the button to the DOM div.\n", |
|
446 | " // add the button to the DOM div.\n", | |
447 | " button_container.append(button);\n", |
|
447 | " button_container.append(button);\n", | |
448 | "}\n", |
|
448 | "}\n", | |
449 | "\n", |
|
449 | "\n", | |
450 | " // now we register the callback under the name foo to give the\n", |
|
450 | " // now we register the callback under the name foo to give the\n", | |
451 | " // user the ability to use it later\n", |
|
451 | " // user the ability to use it later\n", | |
452 | " CellToolbar.register_callback('tuto.foo', toggle);" |
|
452 | " CellToolbar.register_callback('tuto.foo', toggle);" | |
453 | ] |
|
453 | ] | |
454 | }, |
|
454 | }, | |
455 | { |
|
455 | { | |
456 | "cell_type": "markdown", |
|
456 | "cell_type": "markdown", | |
457 | "metadata": {}, |
|
457 | "metadata": {}, | |
458 | "source": [ |
|
458 | "source": [ | |
459 | "#### Registering a preset" |
|
459 | "#### Registering a preset" | |
460 | ] |
|
460 | ] | |
461 | }, |
|
461 | }, | |
462 | { |
|
462 | { | |
463 | "cell_type": "markdown", |
|
463 | "cell_type": "markdown", | |
464 | "metadata": {}, |
|
464 | "metadata": {}, | |
465 | "source": [ |
|
465 | "source": [ | |
466 | "This function can now be part of many `preset` of the CellToolBar." |
|
466 | "This function can now be part of many `preset` of the CellToolBar." | |
467 | ] |
|
467 | ] | |
468 | }, |
|
468 | }, | |
469 | { |
|
469 | { | |
470 | "cell_type": "code", |
|
470 | "cell_type": "code", | |
471 | "execution_count": null, |
|
471 | "execution_count": null, | |
472 | "metadata": { |
|
472 | "metadata": { | |
473 | "collapsed": false, |
|
473 | "collapsed": false, | |
474 | "foo": true, |
|
474 | "foo": true, | |
475 | "slideshow": { |
|
475 | "slideshow": { | |
476 | "slide_type": "subslide" |
|
476 | "slide_type": "subslide" | |
477 | } |
|
477 | } | |
478 | }, |
|
478 | }, | |
479 | "outputs": [], |
|
479 | "outputs": [], | |
480 | "source": [ |
|
480 | "source": [ | |
481 | "%%javascript\n", |
|
481 | "%%javascript\n", | |
482 | "IPython.CellToolbar.register_preset('Tutorial 1',['tuto.foo','default.rawedit'])\n", |
|
482 | "IPython.CellToolbar.register_preset('Tutorial 1',['tuto.foo','default.rawedit'])\n", | |
483 | "IPython.CellToolbar.register_preset('Tutorial 2',['slideshow.select','tuto.foo'])" |
|
483 | "IPython.CellToolbar.register_preset('Tutorial 2',['slideshow.select','tuto.foo'])" | |
484 | ] |
|
484 | ] | |
485 | }, |
|
485 | }, | |
486 | { |
|
486 | { | |
487 | "cell_type": "markdown", |
|
487 | "cell_type": "markdown", | |
488 | "metadata": {}, |
|
488 | "metadata": {}, | |
489 | "source": [ |
|
489 | "source": [ | |
490 | "You should now have access to two presets :\n", |
|
490 | "You should now have access to two presets :\n", | |
491 | "\n", |
|
491 | "\n", | |
492 | " - Tutorial 1\n", |
|
492 | " - Tutorial 1\n", | |
493 | " - Tutorial 2\n", |
|
493 | " - Tutorial 2\n", | |
494 | " \n", |
|
494 | " \n", | |
495 | "And check that the buttons you defin share state when you toggle preset. \n", |
|
495 | "And check that the buttons you defin share state when you toggle preset. \n", | |
496 | "Check moreover that the metadata of the cell is modified when you clisk the button, and that when saved on reloaded the metadata is still availlable." |
|
496 | "Check moreover that the metadata of the cell is modified when you clisk the button, and that when saved on reloaded the metadata is still availlable." | |
497 | ] |
|
497 | ] | |
498 | }, |
|
498 | }, | |
499 | { |
|
499 | { | |
500 | "cell_type": "markdown", |
|
500 | "cell_type": "markdown", | |
501 | "metadata": {}, |
|
501 | "metadata": {}, | |
502 | "source": [ |
|
502 | "source": [ | |
503 | "#### Exercise:" |
|
503 | "#### Exercise:" | |
504 | ] |
|
504 | ] | |
505 | }, |
|
505 | }, | |
506 | { |
|
506 | { | |
507 | "cell_type": "markdown", |
|
507 | "cell_type": "markdown", | |
508 | "metadata": {}, |
|
508 | "metadata": {}, | |
509 | "source": [ |
|
509 | "source": [ | |
510 | "Try to wrap the all code in a file, put this file in `{profile}/static/custom/<a-name>.js`, and add \n", |
|
510 | "Try to wrap the all code in a file, put this file in `{profile}/static/custom/<a-name>.js`, and add \n", | |
511 | "\n", |
|
511 | "\n", | |
512 | "```\n", |
|
512 | "```\n", | |
513 | "require(['custom/<a-name>']);\n", |
|
513 | "require(['custom/<a-name>']);\n", | |
514 | "```\n", |
|
514 | "```\n", | |
515 | "\n", |
|
515 | "\n", | |
516 | "in `custom.js` to have this script automatically loaded in all your notebooks.\n", |
|
516 | "in `custom.js` to have this script automatically loaded in all your notebooks.\n", | |
517 | "\n" |
|
517 | "\n" | |
518 | ] |
|
518 | ] | |
519 | }, |
|
519 | }, | |
520 | { |
|
520 | { | |
521 | "cell_type": "markdown", |
|
521 | "cell_type": "markdown", | |
522 | "metadata": {}, |
|
522 | "metadata": {}, | |
523 | "source": [ |
|
523 | "source": [ | |
524 | "`require` is provided by a [javascript library](http://requirejs.org/) that allow to express dependency. For simple extension like the previous one we directly mute the global namespace, but for more complexe extension you could pass acallback to `require([...], <callback>)` call, to allow the user to pass configuration information to your plugin.\n", |
|
524 | "`require` is provided by a [javascript library](http://requirejs.org/) that allow to express dependency. For simple extension like the previous one we directly mute the global namespace, but for more complexe extension you could pass acallback to `require([...], <callback>)` call, to allow the user to pass configuration information to your plugin.\n", | |
525 | "\n", |
|
525 | "\n", | |
526 | "In Python lang, \n", |
|
526 | "In Python lang, \n", | |
527 | "\n", |
|
527 | "\n", | |
528 | "```javascript\n", |
|
528 | "```javascript\n", | |
529 | "require(['a/b', 'c/d'], function( e, f){\n", |
|
529 | "require(['a/b', 'c/d'], function( e, f){\n", | |
530 | " e.something()\n", |
|
530 | " e.something()\n", | |
531 | " f.something()\n", |
|
531 | " f.something()\n", | |
532 | "})\n", |
|
532 | "})\n", | |
533 | "```\n", |
|
533 | "```\n", | |
534 | "\n", |
|
534 | "\n", | |
535 | "could be read as\n", |
|
535 | "could be read as\n", | |
536 | "```python\n", |
|
536 | "```python\n", | |
537 | "import a.b as e\n", |
|
537 | "import a.b as e\n", | |
538 | "import c.d as f\n", |
|
538 | "import c.d as f\n", | |
539 | "e.something()\n", |
|
539 | "e.something()\n", | |
540 | "f.something()\n", |
|
540 | "f.something()\n", | |
541 | "```\n", |
|
541 | "```\n", | |
542 | "\n", |
|
542 | "\n", | |
543 | "\n", |
|
543 | "\n", | |
544 | "See for example @damianavila [\"ZenMode\" plugin](https://github.com/ipython-contrib/IPython-notebook-extensions/blob/master/custom.example.js#L34) :\n", |
|
544 | "See for example @damianavila [\"ZenMode\" plugin](https://github.com/ipython-contrib/IPython-notebook-extensions/blob/master/custom.example.js#L34) :\n", | |
545 | "\n", |
|
545 | "\n", | |
546 | "```javascript\n", |
|
546 | "```javascript\n", | |
547 | "\n", |
|
547 | "\n", | |
548 | "// read that as\n", |
|
548 | "// read that as\n", | |
549 | "// import custom.zenmode.main as zenmode\n", |
|
549 | "// import custom.zenmode.main as zenmode\n", | |
550 | "require(['custom/zenmode/main'],function(zenmode){\n", |
|
550 | "require(['custom/zenmode/main'],function(zenmode){\n", | |
551 | " zenmode.background('images/back12.jpg');\n", |
|
551 | " zenmode.background('images/back12.jpg');\n", | |
552 | "})\n", |
|
552 | "})\n", | |
553 | "```\n" |
|
553 | "```\n" | |
554 | ] |
|
554 | ] | |
555 | }, |
|
555 | }, | |
556 | { |
|
556 | { | |
557 | "cell_type": "markdown", |
|
557 | "cell_type": "markdown", | |
558 | "metadata": {}, |
|
558 | "metadata": {}, | |
559 | "source": [ |
|
559 | "source": [ | |
560 | "#### For the quickest" |
|
560 | "#### For the quickest" | |
561 | ] |
|
561 | ] | |
562 | }, |
|
562 | }, | |
563 | { |
|
563 | { | |
564 | "cell_type": "markdown", |
|
564 | "cell_type": "markdown", | |
565 | "metadata": {}, |
|
565 | "metadata": {}, | |
566 | "source": [ |
|
566 | "source": [ | |
567 | "Try to use [the following](https://github.com/ipython/ipython/blob/1.x/IPython/html/static/notebook/js/celltoolbar.js#L367) to bind a dropdown list to `cell.metadata.difficulty.select`. \n", |
|
567 | "Try to use [the following](https://github.com/ipython/ipython/blob/1.x/IPython/html/static/notebook/js/celltoolbar.js#L367) to bind a dropdown list to `cell.metadata.difficulty.select`. \n", | |
568 | "\n", |
|
568 | "\n", | |
569 | "It should be able to take the 4 following values :\n", |
|
569 | "It should be able to take the 4 following values :\n", | |
570 | "\n", |
|
570 | "\n", | |
571 | " - `<None>`\n", |
|
571 | " - `<None>`\n", | |
572 | " - `Easy`\n", |
|
572 | " - `Easy`\n", | |
573 | " - `Medium`\n", |
|
573 | " - `Medium`\n", | |
574 | " - `Hard`\n", |
|
574 | " - `Hard`\n", | |
575 | " \n", |
|
575 | " \n", | |
576 | "We will use it to customise the output of the converted notebook depending of the tag on each cell" |
|
576 | "We will use it to customise the output of the converted notebook depending of the tag on each cell" | |
577 | ] |
|
577 | ] | |
578 | }, |
|
578 | }, | |
579 | { |
|
579 | { | |
580 | "cell_type": "code", |
|
580 | "cell_type": "code", | |
581 | "execution_count": null, |
|
581 | "execution_count": null, | |
582 | "metadata": { |
|
582 | "metadata": { | |
583 | "collapsed": false |
|
583 | "collapsed": false | |
584 | }, |
|
584 | }, | |
585 | "outputs": [], |
|
585 | "outputs": [], | |
586 | "source": [ |
|
586 | "source": [ | |
587 | "%load soln/celldiff.js" |
|
587 | "%load soln/celldiff.js" | |
588 | ] |
|
588 | ] | |
589 | } |
|
589 | } | |
590 | ], |
|
590 | ], | |
591 | "metadata": { |
|
591 | "metadata": { | |
592 | "kernelspec": { |
|
592 | "kernelspec": { | |
593 | "display_name": "Python 3", |
|
593 | "display_name": "Python 3", | |
594 | "language": "python", |
|
594 | "language": "python", | |
595 | "name": "python3" |
|
595 | "name": "python3" | |
596 | }, |
|
596 | }, | |
597 | "language_info": { |
|
597 | "language_info": { | |
598 | "codemirror_mode": { |
|
598 | "codemirror_mode": { | |
599 | "name": "ipython", |
|
599 | "name": "ipython", | |
600 | "version": 3 |
|
600 | "version": 3 | |
601 | }, |
|
601 | }, | |
602 | "file_extension": ".py", |
|
602 | "file_extension": ".py", | |
603 | "mimetype": "text/x-python", |
|
603 | "mimetype": "text/x-python", | |
604 | "name": "python", |
|
604 | "name": "python", | |
605 | "nbconvert_exporter": "python", |
|
605 | "nbconvert_exporter": "python", | |
606 | "pygments_lexer": "ipython3", |
|
606 | "pygments_lexer": "ipython3", | |
607 | "version": "3.4.3" |
|
607 | "version": "3.4.3" | |
608 | } |
|
608 | } | |
609 | }, |
|
609 | }, | |
610 | "nbformat": 4, |
|
610 | "nbformat": 4, | |
611 | "nbformat_minor": 0 |
|
611 | "nbformat_minor": 0 | |
612 | } |
|
612 | } |
General Comments 0
You need to be logged in to leave comments.
Login now