##// END OF EJS Templates
Pulling content from ipython-in-depth.
Brian E. Granger -
Show More
@@ -0,0 +1,184 b''
1 {
2 "metadata": {
3 "name": "",
4 "signature": "sha256:4352d4e1c693d919ce40b29ecf5a536917160df68b19a85caccedb1ea7ad06e1"
5 },
6 "nbformat": 3,
7 "nbformat_minor": 0,
8 "worksheets": [
9 {
10 "cells": [
11 {
12 "cell_type": "heading",
13 "level": 1,
14 "metadata": {},
15 "source": [
16 "Working With External Code"
17 ]
18 },
19 {
20 "cell_type": "markdown",
21 "metadata": {},
22 "source": [
23 "The IPython Kernel makes it easy to incorporate external code from sources such as the internet or copy/paste. "
24 ]
25 },
26 {
27 "cell_type": "heading",
28 "level": 2,
29 "metadata": {},
30 "source": [
31 "Pasting code into cells"
32 ]
33 },
34 {
35 "cell_type": "markdown",
36 "metadata": {},
37 "source": [
38 "You can copy and paste code from other sources directly into cells. Pasting code with `>>>` prompts works as expected:"
39 ]
40 },
41 {
42 "cell_type": "code",
43 "collapsed": false,
44 "input": [
45 ">>> the_world_is_flat = 1\n",
46 ">>> if the_world_is_flat:\n",
47 "... print(\"Be careful not to fall off!\")"
48 ],
49 "language": "python",
50 "metadata": {},
51 "outputs": [
52 {
53 "output_type": "stream",
54 "stream": "stdout",
55 "text": [
56 "Be careful not to fall off!\n"
57 ]
58 }
59 ],
60 "prompt_number": 7
61 },
62 {
63 "cell_type": "heading",
64 "level": 2,
65 "metadata": {},
66 "source": [
67 "The %load magic"
68 ]
69 },
70 {
71 "cell_type": "markdown",
72 "metadata": {},
73 "source": [
74 "The `%load` magic lets you load code from URLs or local files:"
75 ]
76 },
77 {
78 "cell_type": "code",
79 "collapsed": false,
80 "input": [
81 "%load?"
82 ],
83 "language": "python",
84 "metadata": {},
85 "outputs": [],
86 "prompt_number": 8
87 },
88 {
89 "cell_type": "code",
90 "collapsed": false,
91 "input": [
92 "%matplotlib inline"
93 ],
94 "language": "python",
95 "metadata": {},
96 "outputs": [],
97 "prompt_number": 9
98 },
99 {
100 "cell_type": "code",
101 "collapsed": false,
102 "input": [
103 "%load http://matplotlib.org/mpl_examples/showcase/integral_demo.py"
104 ],
105 "language": "python",
106 "metadata": {},
107 "outputs": [],
108 "prompt_number": 10
109 },
110 {
111 "cell_type": "code",
112 "collapsed": false,
113 "input": [
114 "\"\"\"\n",
115 "Plot demonstrating the integral as the area under a curve.\n",
116 "\n",
117 "Although this is a simple example, it demonstrates some important tweaks:\n",
118 "\n",
119 " * A simple line plot with custom color and line width.\n",
120 " * A shaded region created using a Polygon patch.\n",
121 " * A text label with mathtext rendering.\n",
122 " * figtext calls to label the x- and y-axes.\n",
123 " * Use of axis spines to hide the top and right spines.\n",
124 " * Custom tick placement and labels.\n",
125 "\"\"\"\n",
126 "import numpy as np\n",
127 "import matplotlib.pyplot as plt\n",
128 "from matplotlib.patches import Polygon\n",
129 "\n",
130 "\n",
131 "def func(x):\n",
132 " return (x - 3) * (x - 5) * (x - 7) + 85\n",
133 "\n",
134 "\n",
135 "a, b = 2, 9 # integral limits\n",
136 "x = np.linspace(0, 10)\n",
137 "y = func(x)\n",
138 "\n",
139 "fig, ax = plt.subplots()\n",
140 "plt.plot(x, y, 'r', linewidth=2)\n",
141 "plt.ylim(ymin=0)\n",
142 "\n",
143 "# Make the shaded region\n",
144 "ix = np.linspace(a, b)\n",
145 "iy = func(ix)\n",
146 "verts = [(a, 0)] + list(zip(ix, iy)) + [(b, 0)]\n",
147 "poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')\n",
148 "ax.add_patch(poly)\n",
149 "\n",
150 "plt.text(0.5 * (a + b), 30, r\"$\\int_a^b f(x)\\mathrm{d}x$\",\n",
151 " horizontalalignment='center', fontsize=20)\n",
152 "\n",
153 "plt.figtext(0.9, 0.05, '$x$')\n",
154 "plt.figtext(0.1, 0.9, '$y$')\n",
155 "\n",
156 "ax.spines['right'].set_visible(False)\n",
157 "ax.spines['top'].set_visible(False)\n",
158 "ax.xaxis.set_ticks_position('bottom')\n",
159 "\n",
160 "ax.set_xticks((a, b))\n",
161 "ax.set_xticklabels(('$a$', '$b$'))\n",
162 "ax.set_yticks([])\n",
163 "\n",
164 "plt.show()\n"
165 ],
166 "language": "python",
167 "metadata": {},
168 "outputs": [
169 {
170 "metadata": {},
171 "output_type": "display_data",
172 "png": "iVBORw0KGgoAAAANSUhEUgAAAW8AAAEMCAYAAAALXDfgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl4FFW+xvFvp9NJCAphkdUECAgqLigG2UZgrsB4YQZQ\nAZVFQQRxlAFFUQR1BMVxRAV0QMFxAUXUgRkXBrioiCJIhLAjO5KwG7ORpde6f5SJooGQpLuru/N+\nnqefFElXnR+QvBxOnTrHZhiGgYiIhJUoqwsQEZHyU3iLiIQhhbeISBhSeIuIhCGFt4hIGFJ4i4iE\nIYW3iEgYUniLiIShMsN727ZtTJ06lXXr1gFwxx13BLomEREpQ5nhXVBQgMPhwDAMdu7cyQUXXBCM\nukRE5CzKDO927dqxceNGOnTowLp16+jUqVMw6hIRkbM4pzHv+Ph4ANatW0eHDh0CWpCIiJTtnMI7\nKSmJ999/nw0bNlC/fv1A1yQiImUoM7znzZtH165dufLKKxkwYMAZ3/fEE0/4sy4RETkLW1lLwi5f\nvhyXy8Xx48cZPnw4UVGl573NZkOry4qIBEeZ4X3OF1J4i4gEjR7SEREJQwpvEZEwpPAWEQlDCm8R\nkTCk8BYRCUMKbxGRMKTwFhGxSmFhhU9VeIuIWCEnBxITK3y6wltExArz5kFmZoVP1xOWIiLB5vFA\n8+Zw6BBUMDfV8xYRCbYlS8zgbtGiwpdQeIuIBNsLL5gfx46t8CU0bCIiEkzffAPt20NCAqSnw3nn\nVegy6nmLiATTiy+aH0eOrHBwg3reIiLBk54OzZqZxwcOaKqgiEhYeOkl8Hqhf/9KBTeo5y0iEhyn\nTpmBnZ1tjnu3a1epy6nnLSISDG++aQZ3x46VDm5QeIuIBJ7PBzNmmMfjxvnlkgpvEZFA++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Uy12YYhoGIiIQV6xemEhGRclN4i4iEIYW3iEgYUniL\niIQhhbeIVClFRUXcfPPNzJ8/3+pSKiU09rCMEAsXLsTtdpORkUG9evUYMWKE1SWJyK/ExcVx4YUX\nkpKSYnUplaKet5/s2rWL5cuXM3ToUOx2O5dddpnVJYnIGezcuZNWrVpZXUalKLz9ZMGCBfzpT38C\nYPPmzVx11VUWVyQipXG73Rw6dIhPPvmEhx9+GJ/PZ3VJFaLw9pPs7GxatWqFy+UiLy+Pb7/91uqS\nRKQUW7ZsoW/fvvTu3Ruv18vWrVutLqlCNObtJ0OHDmXFihXs2LGD5s2bc/ToUatLEpFSpKWl0aVL\nFwB27NhB7TDdyUvh7ScpKSklN0D69+9vcTUicibZ2dlcd911ZGVlYbfbSUxMtLqkCtHaJiJSpezb\nt4+PP/6Y7OxsRo0aRYMGDawuqUIU3iIiYUg3LEVEwpDCW0QkDOmGpYiIxbxeL4sWLWL//v0kJiay\nfv16HnjgAZKTk894jnreIiIW27x5MzfddBPJycn4fD769+9Pw4YNz3qOwltExGJXX301sbGxrF27\nlq5du9K1a1eqVat21nMU3iIiFktNTeWHH35g27ZtNGvWjC+//LLMczTmLSJisWXLllG/fn06derE\nkiVLqFu3bpnnaJ63iEgY0rCJiEgYUniLiIQhhbeISBhSeIuIhCGFt4hIGFJ4i4iEIYW3iEgYUniL\niISh/weZPyRnS1m/IAAAAABJRU5ErkJggg==\n",
173 "text": [
174 "<matplotlib.figure.Figure at 0x108604e50>"
175 ]
176 }
177 ],
178 "prompt_number": 11
179 }
180 ],
181 "metadata": {}
182 }
183 ]
184 } No newline at end of file
This diff has been collapsed as it changes many lines, (770 lines changed) Show them Hide them
@@ -0,0 +1,770 b''
1 {
2 "metadata": {
3 "name": ""
4 },
5 "nbformat": 3,
6 "nbformat_minor": 0,
7 "worksheets": [
8 {
9 "cells": [
10 {
11 "cell_type": "heading",
12 "level": 1,
13 "metadata": {},
14 "source": [
15 "Embrasing web standards"
16 ]
17 },
18 {
19 "cell_type": "markdown",
20 "metadata": {},
21 "source": [
22 "One of the main reason that allowed us to developp the current notebook web application \n",
23 "was to embrase the web technology. \n",
24 "\n",
25 "By beeing a pure web application using HTML, Javascript and CSS, the Notebook can get \n",
26 "all the web technology improvement for free. Thus, as browsers support for different \n",
27 "media extend, The notebook web app should be able to be compatible without modification. \n",
28 "\n",
29 "This is also true with performance of the User Interface as the speed of javascript VM increase. "
30 ]
31 },
32 {
33 "cell_type": "markdown",
34 "metadata": {},
35 "source": [
36 "The other advantage of using only web technology is that the code of the interface is fully accessible to the end user, and modifiable live.\n",
37 "Even if this task is not always easy, we strive to keep our code as accessible and reusable as possible.\n",
38 "This should allow with minimum effort to develop small extensions that customize the behavior of the web interface. "
39 ]
40 },
41 {
42 "cell_type": "heading",
43 "level": 2,
44 "metadata": {},
45 "source": [
46 "Tempering with Notebook app"
47 ]
48 },
49 {
50 "cell_type": "markdown",
51 "metadata": {},
52 "source": [
53 "The first tool that is availlable to you and that you shoudl be aware of are browser \"developpers tool\". The exact naming can change across browser, 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 run the frontend.\n",
54 "\n",
55 " - In Chrome and safari Developper tools are in the menu [Put mmenu name in english here] \n",
56 " - In firefox you might need to install [Firebug](http://getfirebug.com/)\n",
57 " - others ?\n",
58 " \n",
59 "Those will be your best friends to debug and try different approach for your extensions."
60 ]
61 },
62 {
63 "cell_type": "heading",
64 "level": 3,
65 "metadata": {},
66 "source": [
67 "Injecting JS"
68 ]
69 },
70 {
71 "cell_type": "heading",
72 "level": 4,
73 "metadata": {},
74 "source": [
75 "using magics"
76 ]
77 },
78 {
79 "cell_type": "markdown",
80 "metadata": {},
81 "source": [
82 "Above tools can be tedious to edit long javascipt 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",
83 "\n",
84 "You might see here and there people modifying css and injecting js into notebook by reading file and publishing them into the notebook.\n",
85 "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",
86 "\n",
87 "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."
88 ]
89 },
90 {
91 "cell_type": "code",
92 "collapsed": false,
93 "input": [
94 "## you can inspect the autosave code to see what it does.\n",
95 "%autosave??"
96 ],
97 "language": "python",
98 "metadata": {},
99 "outputs": []
100 },
101 {
102 "cell_type": "heading",
103 "level": 4,
104 "metadata": {},
105 "source": [
106 "custom.js"
107 ]
108 },
109 {
110 "cell_type": "markdown",
111 "metadata": {},
112 "source": [
113 "To inject Javascript we provide an entry point: `custom.js` that allow teh user to execute and load other resources into the notebook.\n",
114 "Javascript code in `custom.js` will be executed when the notebook app start and can then be used to customise almost anything in the UI and in the behavior of the notebook.\n",
115 "\n",
116 "`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."
117 ]
118 },
119 {
120 "cell_type": "heading",
121 "level": 5,
122 "metadata": {},
123 "source": [
124 "Because we like you...."
125 ]
126 },
127 {
128 "cell_type": "markdown",
129 "metadata": {},
130 "source": [
131 "You have been provided with an already existing profile folder with this tutorial...\n",
132 "start the notebook from the root of the tutorial directory with :\n",
133 "\n",
134 "```bash\n",
135 "$ ipython notebook --ProfileDir.location=./profile_euroscipy\n",
136 "```"
137 ]
138 },
139 {
140 "cell_type": "heading",
141 "level": 5,
142 "metadata": {},
143 "source": [
144 "but back to theory"
145 ]
146 },
147 {
148 "cell_type": "code",
149 "collapsed": false,
150 "input": [
151 "profile_dir = ! ipython locate\n",
152 "profile_dir = profile_dir[0]\n",
153 "profile_dir"
154 ],
155 "language": "python",
156 "metadata": {},
157 "outputs": [
158 {
159 "metadata": {},
160 "output_type": "pyout",
161 "prompt_number": 1,
162 "text": [
163 "'/Users/bussonniermatthias/.ipython'"
164 ]
165 }
166 ]
167 },
168 {
169 "cell_type": "markdown",
170 "metadata": {},
171 "source": [
172 "and custom js is in "
173 ]
174 },
175 {
176 "cell_type": "code",
177 "collapsed": false,
178 "input": [
179 "import os.path\n",
180 "custom_js_path = os.path.join(profile_dir,'profile_default','static','custom','custom.js')"
181 ],
182 "language": "python",
183 "metadata": {},
184 "outputs": []
185 },
186 {
187 "cell_type": "code",
188 "collapsed": false,
189 "input": [
190 "# my custom js\n",
191 "with open(custom_js_path) as f:\n",
192 " for l in f: \n",
193 " print l,"
194 ],
195 "language": "python",
196 "metadata": {},
197 "outputs": [
198 {
199 "output_type": "stream",
200 "stream": "stdout",
201 "text": [
202 "// we want strict javascript that fails\n",
203 "// on ambiguous syntax\n",
204 "\"using strict\";\n",
205 "\n",
206 "// notebook loaded is not perfect as it is re-triggerd on\n",
207 "// revert to checkpoint but this allow extesnsion to be loaded\n",
208 "// late enough to work.\n",
209 "$([IPython.events]).on('notebook_loaded.Notebook', function(){\n",
210 "\n",
211 "\n",
212 " /** Use path to js file relative to /static/ dir without leading slash, or\n",
213 " * js extension.\n",
214 " * Link directly to file is js extension isa simple file\n",
215 " *\n",
216 " * first argument of require is a **list** that can contains several modules if needed.\n",
217 " **/\n",
218 "\n",
219 " //require(['custom/noscroll']);\n",
220 " // require(['custom/clean_start'])\n",
221 " // require(['custom/toggle_all_line_number'])\n",
222 " // require(['custom/gist_it']);\n",
223 " // require(['custom/autosavetime']);\n",
224 "\n",
225 " /**\n",
226 " * Link to entrypoint if extesnsion is a folder.\n",
227 " * to be consistent with commonjs module, the entrypoint is main.js\n",
228 " * here youcan also trigger a custom function on load that will do extra\n",
229 " * action with the module if needed\n",
230 " **/\n",
231 " require(['custom/slidemode/main'])\n",
232 "\n",
233 " // require(['custom/autoscroll']);\n",
234 "\n",
235 " //require(['custom/css_selector/main'])\n",
236 " require(['custom/pre_exec_strip']);\n",
237 " // require(['custom/no_exec_dunder']);\n",
238 " // load_ext('nbviewer_theme')\n",
239 "\n",
240 "\n",
241 " require(['custom/clippytip/main']);\n",
242 "\n",
243 " IPython.toolbar.add_buttons_group([\n",
244 " {\n",
245 " 'label' : 'run qtconsole',\n",
246 " 'icon' : 'icon-paper-clip', // select your icon from http://jqueryui.com/themeroller/\n",
247 " 'callback': function(){\n",
248 " IPython.tooltip.remove_and_cancel_tooltip(true)\n",
249 " $('#tooltip').empty() \n",
250 " $('#tooltip').attr('style','') \n",
251 " IPython.tooltip = new IPython.ClippyTip()\n",
252 " }\n",
253 " },\n",
254 " {\n",
255 " 'label' : 'run qtconsole',\n",
256 " 'icon' : 'icon-th-large', // select your icon from http://jqueryui.com/themeroller/\n",
257 " 'callback': function(){\n",
258 " IPython.tooltip.remove_and_cancel_tooltip(true)\n",
259 " $('#tooltip').empty() \n",
260 " $('#tooltip').attr('style','')\n",
261 " IPython.tooltip = new IPython.Tooltip()\n",
262 " }\n",
263 " }\n",
264 " // add more button here if needed.\n",
265 " ]);\n",
266 " //\n",
267 "\n",
268 "});\n",
269 "\n",
270 "/*\n",
271 "$([IPython.events]).on('notebook_loaded.Notebook', function(){\n",
272 " IPython.toolbar.add_buttons_group([\n",
273 " {\n",
274 " 'label' : 'run qtconsole',\n",
275 " 'icon' : 'ui-icon-calculator',\n",
276 " 'callback': function(){IPython.notebook.kernel.execute('%qtconsole')}\n",
277 " }\n",
278 " ]);\n",
279 "});\n",
280 "*/\n",
281 "\n",
282 "//$([IPython.events]).on('notebook_loaded.Notebook', function(){\n",
283 "// mobile_preset = []\n",
284 "// var edit = function(div, cell) {\n",
285 "// var button_container = $(div);\n",
286 "// var button = $('<div/>').button({icons:{primary:'ui-icon-pencil'}});\n",
287 "// button.click(function(){\n",
288 "// cell.edit()\n",
289 "// })\n",
290 "// button_container.append(button);\n",
291 "// }\n",
292 "//\n",
293 "// IPython.CellToolbar.register_callback('mobile.edit',edit);\n",
294 "// mobile_preset.push('mobile.edit');\n",
295 "//\n",
296 "// IPython.CellToolbar.register_preset('Mobile',mobile_preset);\n",
297 "//});\n"
298 ]
299 }
300 ]
301 },
302 {
303 "cell_type": "markdown",
304 "metadata": {},
305 "source": [
306 "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."
307 ]
308 },
309 {
310 "cell_type": "markdown",
311 "metadata": {},
312 "source": [
313 "**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**."
314 ]
315 },
316 {
317 "cell_type": "heading",
318 "level": 2,
319 "metadata": {},
320 "source": [
321 "Exercise :"
322 ]
323 },
324 {
325 "cell_type": "markdown",
326 "metadata": {},
327 "source": [
328 " - Create a `custom.js` in the right location with the following content:\n",
329 "```javascript\n",
330 "alert(\"hello world from custom.js\")\n",
331 "```\n",
332 "\n",
333 " - Restart your server and open any notebook.\n",
334 " - Be greeted by custom.js"
335 ]
336 },
337 {
338 "cell_type": "markdown",
339 "metadata": {},
340 "source": [
341 "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."
342 ]
343 },
344 {
345 "cell_type": "heading",
346 "level": 4,
347 "metadata": {},
348 "source": [
349 "For the quick ones : "
350 ]
351 },
352 {
353 "cell_type": "markdown",
354 "metadata": {},
355 "source": [
356 "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. "
357 ]
358 },
359 {
360 "cell_type": "markdown",
361 "metadata": {
362 "foo": true
363 },
364 "source": [
365 "Create a dropdow elemement in the toolbar (DOM `IPython.toolbar.element`), you will need \n",
366 "\n",
367 "- `IPython.notebook.set_autosave_interval(miliseconds)`\n",
368 "- know that 1min = 60 sec, and 1 sec = 1000 ms"
369 ]
370 },
371 {
372 "cell_type": "markdown",
373 "metadata": {},
374 "source": [
375 "```javascript\n",
376 "\n",
377 "var label = jQuery('<label/>').text('AutoScroll Limit:');\n",
378 "var select = jQuery('<select/>')\n",
379 " //.append(jQuery('<option/>').attr('value', '2').text('2min (default)'))\n",
380 " .append(jQuery('<option/>').attr('value', undefined).text('disabled'))\n",
381 "\n",
382 " // TODO:\n",
383 " //the_toolbar_element.append(label)\n",
384 " //the_toolbar_element.append(select);\n",
385 " \n",
386 "select.change(function() {\n",
387 " var val = jQuery(this).val() // val will be the value in [2]\n",
388 " // TODO\n",
389 " // this will be called when dropdown changes\n",
390 "\n",
391 "});\n",
392 "\n",
393 "var time_m = [1,5,10,15,30];\n",
394 "for (var i=0; i < time_m.length; i++) {\n",
395 " var ts = time_m[i];\n",
396 " //[2] ____ this will be `val` on [1] \n",
397 " // | \n",
398 " // v \n",
399 " select.append($('<option/>').attr('value', ts).text(thr+'min'));\n",
400 " // this will fill up the dropdown `select` with\n",
401 " // 1 min\n",
402 " // 5 min\n",
403 " // 10 min\n",
404 " // 10 min\n",
405 " // ...\n",
406 "}\n",
407 "```"
408 ]
409 },
410 {
411 "cell_type": "heading",
412 "level": 4,
413 "metadata": {},
414 "source": [
415 "A non interactive example first"
416 ]
417 },
418 {
419 "cell_type": "markdown",
420 "metadata": {},
421 "source": [
422 "I like my cython to be nicely highlighted\n",
423 "\n",
424 "```javascript\n",
425 "IPython.config.cell_magic_highlight['magic_text/x-cython'] = {}\n",
426 "IPython.config.cell_magic_highlight['magic_text/x-cython'].reg = [/^%%cython/]\n",
427 "```\n",
428 "\n",
429 "`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."
430 ]
431 },
432 {
433 "cell_type": "heading",
434 "level": 4,
435 "metadata": {},
436 "source": [
437 "Get more docs"
438 ]
439 },
440 {
441 "cell_type": "markdown",
442 "metadata": {},
443 "source": [
444 "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",
445 "If you have `node` and `yui-doc` installed:"
446 ]
447 },
448 {
449 "cell_type": "markdown",
450 "metadata": {},
451 "source": [
452 "```bash\n",
453 "$ cd ~/ipython/IPython/html/static/notebook/js/\n",
454 "$ yuidoc . --server\n",
455 "warn: (yuidoc): Failed to extract port, setting to the default :3000\n",
456 "info: (yuidoc): Starting YUIDoc@0.3.45 using YUI@3.9.1 with NodeJS@0.10.15\n",
457 "info: (yuidoc): Scanning for yuidoc.json file.\n",
458 "info: (yuidoc): Starting YUIDoc with the following options:\n",
459 "info: (yuidoc):\n",
460 "{ port: 3000,\n",
461 " nocode: false,\n",
462 " paths: [ '.' ],\n",
463 " server: true,\n",
464 " outdir: './out' }\n",
465 "info: (yuidoc): Scanning for yuidoc.json file.\n",
466 "info: (server): Starting server: http://127.0.0.1:3000\n",
467 "```\n",
468 "\n",
469 "and browse http://127.0.0.1:3000 to get docs"
470 ]
471 },
472 {
473 "cell_type": "heading",
474 "level": 4,
475 "metadata": {
476 "foo": true
477 },
478 "source": [
479 "Some convenience methods"
480 ]
481 },
482 {
483 "cell_type": "markdown",
484 "metadata": {},
485 "source": [
486 "By browsing the doc you will see that we have soem convenience methods that avoid to re-invent the UI everytime :\n",
487 "```javascript\n",
488 "IPython.toolbar.add_buttons_group([\n",
489 " {\n",
490 " 'label' : 'run qtconsole',\n",
491 " 'icon' : 'icon-terminal', // select your icon from \n",
492 " // http://fortawesome.github.io/Font-Awesome/icons/\n",
493 " 'callback': function(){IPython.notebook.kernel.execute('%qtconsole')}\n",
494 " }\n",
495 " // add more button here if needed.\n",
496 " ]);\n",
497 "```\n",
498 "with a [lot of icons] you can select from. \n",
499 "\n",
500 "[lot of icons]: http://fortawesome.github.io/Font-Awesome/icons/"
501 ]
502 },
503 {
504 "cell_type": "heading",
505 "level": 2,
506 "metadata": {
507 "foo": true
508 },
509 "source": [
510 "Cell Metadata"
511 ]
512 },
513 {
514 "cell_type": "markdown",
515 "metadata": {
516 "foo": true
517 },
518 "source": [
519 "The most requested feature is generaly to be able to distinguish individual cell in th enotebook, or run specific action with them.\n",
520 "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."
521 ]
522 },
523 {
524 "cell_type": "heading",
525 "level": 3,
526 "metadata": {},
527 "source": [
528 "Cell Toolbar"
529 ]
530 },
531 {
532 "cell_type": "markdown",
533 "metadata": {},
534 "source": [
535 "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."
536 ]
537 },
538 {
539 "cell_type": "markdown",
540 "metadata": {},
541 "source": [
542 "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"
543 ]
544 },
545 {
546 "cell_type": "heading",
547 "level": 4,
548 "metadata": {},
549 "source": [
550 "Register a callback"
551 ]
552 },
553 {
554 "cell_type": "code",
555 "collapsed": false,
556 "input": [
557 "%%javascript\n",
558 "var CellToolbar = IPython.CellToolbar\n",
559 "var toggle = function(div, cell) {\n",
560 " var button_container = $(div)\n",
561 "\n",
562 " // let's create a button that show the current value of the metadata\n",
563 " var button = $('<button/>').addClass('btn btn-mini').text(String(cell.metadata.foo));\n",
564 "\n",
565 " // On click, change the metadata value and update the button label\n",
566 " button.click(function(){\n",
567 " var v = cell.metadata.foo;\n",
568 " cell.metadata.foo = !v;\n",
569 " button.text(String(!v));\n",
570 " })\n",
571 "\n",
572 " // add the button to the DOM div.\n",
573 " button_container.append(button);\n",
574 "}\n",
575 "\n",
576 " // now we register the callback under the name foo to give the\n",
577 " // user the ability to use it later\n",
578 " CellToolbar.register_callback('tuto.foo', toggle);"
579 ],
580 "language": "python",
581 "metadata": {},
582 "outputs": [
583 {
584 "javascript": [
585 "var CellToolbar = IPython.CellToolbar\n",
586 "var toggle = function(div, cell) {\n",
587 " var button_container = $(div)\n",
588 "\n",
589 " // let's create a button that show the current value of the metadata\n",
590 " var button = $('<button/>').addClass('btn btn-mini').text(String(cell.metadata.foo));\n",
591 "\n",
592 " // On click, change the metadata value and update the button label\n",
593 " button.click(function(){\n",
594 " var v = cell.metadata.foo;\n",
595 " cell.metadata.foo = !v;\n",
596 " button.text(String(!v));\n",
597 " })\n",
598 "\n",
599 " // add the button to the DOM div.\n",
600 " button_container.append(button);\n",
601 "}\n",
602 "\n",
603 " // now we register the callback under the name foo to give the\n",
604 " // user the ability to use it later\n",
605 " CellToolbar.register_callback('tuto.foo', toggle);"
606 ],
607 "metadata": {},
608 "output_type": "display_data",
609 "text": [
610 "<IPython.core.display.Javascript at 0x10c94a590>"
611 ]
612 }
613 ],
614 "prompt_number": 51
615 },
616 {
617 "cell_type": "heading",
618 "level": 4,
619 "metadata": {},
620 "source": [
621 "Registering a preset"
622 ]
623 },
624 {
625 "cell_type": "markdown",
626 "metadata": {},
627 "source": [
628 "This function can now be part of many `preset` of the CellToolBar."
629 ]
630 },
631 {
632 "cell_type": "code",
633 "collapsed": false,
634 "input": [
635 "%%javascript\n",
636 "IPython.CellToolbar.register_preset('Tutorial 1',['tuto.foo','default.rawedit'])\n",
637 "IPython.CellToolbar.register_preset('Tutorial 2',['slideshow.select','tuto.foo'])"
638 ],
639 "language": "python",
640 "metadata": {
641 "foo": true,
642 "slideshow": {
643 "slide_type": "subslide"
644 }
645 },
646 "outputs": [
647 {
648 "javascript": [
649 "IPython.CellToolbar.register_preset('Tutorial 1',['tuto.foo','default.rawedit'])\n",
650 "IPython.CellToolbar.register_preset('Tutorial 2',['slideshow.select','tuto.foo'])"
651 ],
652 "metadata": {},
653 "output_type": "display_data",
654 "text": [
655 "<IPython.core.display.Javascript at 0x10c94a510>"
656 ]
657 }
658 ],
659 "prompt_number": 54
660 },
661 {
662 "cell_type": "markdown",
663 "metadata": {},
664 "source": [
665 "You should now have access to two presets :\n",
666 "\n",
667 " - Tutorial 1\n",
668 " - Tutorial 2\n",
669 " \n",
670 "And check that the buttons you defin share state when you toggle preset. \n",
671 "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."
672 ]
673 },
674 {
675 "cell_type": "heading",
676 "level": 4,
677 "metadata": {},
678 "source": [
679 "Exercise:"
680 ]
681 },
682 {
683 "cell_type": "markdown",
684 "metadata": {},
685 "source": [
686 "Try to wrap the all code in a file, put this file in `{profile}/static/custom/<a-name>.js`, and add \n",
687 "\n",
688 "```\n",
689 "require(['custom/<a-name>']);\n",
690 "```\n",
691 "\n",
692 "in `custom.js` to have this script automatically loaded in all your notebooks.\n",
693 "\n"
694 ]
695 },
696 {
697 "cell_type": "markdown",
698 "metadata": {},
699 "source": [
700 "`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",
701 "\n",
702 "In Python lang, \n",
703 "\n",
704 "```javascript\n",
705 "require(['a/b', 'c/d'], function( e, f){\n",
706 " e.something()\n",
707 " f.something()\n",
708 "})\n",
709 "```\n",
710 "\n",
711 "could be read as\n",
712 "```python\n",
713 "import a.b as e\n",
714 "import c.d as f\n",
715 "e.something()\n",
716 "f.something()\n",
717 "```\n",
718 "\n",
719 "\n",
720 "See for example @damianavila [\"ZenMode\" plugin](https://github.com/ipython-contrib/IPython-notebook-extensions/blob/master/custom.example.js#L34) :\n",
721 "\n",
722 "```javascript\n",
723 "\n",
724 "// read that as\n",
725 "// import custom.zenmode.main as zenmode\n",
726 "require(['custom/zenmode/main'],function(zenmode){\n",
727 " zenmode.background('images/back12.jpg');\n",
728 "})\n",
729 "```\n"
730 ]
731 },
732 {
733 "cell_type": "heading",
734 "level": 4,
735 "metadata": {},
736 "source": [
737 "For the quickest"
738 ]
739 },
740 {
741 "cell_type": "markdown",
742 "metadata": {},
743 "source": [
744 "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",
745 "\n",
746 "It should be able to take the 4 following values :\n",
747 "\n",
748 " - `<None>`\n",
749 " - `Easy`\n",
750 " - `Medium`\n",
751 " - `Hard`\n",
752 " \n",
753 "We will use it to customise the output of the converted notebook depending of the tag on each cell"
754 ]
755 },
756 {
757 "cell_type": "code",
758 "collapsed": false,
759 "input": [
760 "%load soln/celldiff.js"
761 ],
762 "language": "python",
763 "metadata": {},
764 "outputs": []
765 }
766 ],
767 "metadata": {}
768 }
769 ]
770 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now