Show More
@@ -32,19 +32,10 b'' | |||||
32 | "cell_type": "markdown", |
|
32 | "cell_type": "markdown", | |
33 | "metadata": {}, |
|
33 | "metadata": {}, | |
34 | "source": [ |
|
34 | "source": [ | |
35 | "<pre style='background-color:#FDD'> Warning, Do use 1.0 or 1.x branch and not master naming have changed.</pre>\n", |
|
|||
36 | "\n", |
|
|||
37 | "<pre style='background-color:#FDD'> Warning, NbConvert is a Tech-Preview, API will change within the next 6 month.</pre>" |
|
|||
38 | ] |
|
|||
39 | }, |
|
|||
40 | { |
|
|||
41 | "cell_type": "markdown", |
|
|||
42 | "metadata": {}, |
|
|||
43 | "source": [ |
|
|||
44 | "Credit, Jonathan Freder (@jdfreder on github)\n", |
|
35 | "Credit, Jonathan Freder (@jdfreder on github)\n", | |
45 | "\n", |
|
36 | "\n", | |
46 | "<center>\n", |
|
37 | "<center>\n", | |
47 |
" ![nbca]( |
|
38 | " ![nbca](images/nbconvert_arch.png)\n", | |
48 | "</center>" |
|
39 | "</center>" | |
49 | ] |
|
40 | ] | |
50 | }, |
|
41 | }, | |
@@ -84,7 +75,7 b'' | |||||
84 | "source": [ |
|
75 | "source": [ | |
85 | "import requests\n", |
|
76 | "import requests\n", | |
86 | "response = requests.get('http://jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb')\n", |
|
77 | "response = requests.get('http://jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb')\n", | |
87 |
"response. |
|
78 | "response.text[0:60]+'...'" | |
88 | ] |
|
79 | ] | |
89 | }, |
|
80 | }, | |
90 | { |
|
81 | { | |
@@ -113,10 +104,9 b'' | |||||
113 | { |
|
104 | { | |
114 | "data": { |
|
105 | "data": { | |
115 | "text/plain": [ |
|
106 | "text/plain": [ | |
116 |
"{ |
|
107 | "{'cell_type': 'markdown',\n", | |
117 |
" |
|
108 | " 'metadata': {},\n", | |
118 | " u'metadata': {},\n", |
|
109 | " 'source': '# XKCD plots in Matplotlib'}" | |
119 | " u'source': u'XKCD plots in Matplotlib'}" |
|
|||
120 | ] |
|
110 | ] | |
121 | }, |
|
111 | }, | |
122 | "execution_count": 2, |
|
112 | "execution_count": 2, | |
@@ -125,9 +115,9 b'' | |||||
125 | } |
|
115 | } | |
126 | ], |
|
116 | ], | |
127 | "source": [ |
|
117 | "source": [ | |
128 |
"from IPython |
|
118 | "from IPython import nbformat\n", | |
129 |
"jake_notebook = nbformat.reads |
|
119 | "jake_notebook = nbformat.reads(response.text, as_version=4)\n", | |
130 |
"jake_notebook. |
|
120 | "jake_notebook.cells[0]" | |
131 | ] |
|
121 | ] | |
132 | }, |
|
122 | }, | |
133 | { |
|
123 | { | |
@@ -155,34 +145,23 b'' | |||||
155 | }, |
|
145 | }, | |
156 | "outputs": [], |
|
146 | "outputs": [], | |
157 | "source": [ |
|
147 | "source": [ | |
158 | "import IPython.nbconvert" |
|
|||
159 | ] |
|
|||
160 | }, |
|
|||
161 | { |
|
|||
162 | "cell_type": "code", |
|
|||
163 | "execution_count": 4, |
|
|||
164 | "metadata": { |
|
|||
165 | "collapsed": false |
|
|||
166 | }, |
|
|||
167 | "outputs": [], |
|
|||
168 | "source": [ |
|
|||
169 | "from IPython.config import Config\n", |
|
148 | "from IPython.config import Config\n", | |
170 | "from IPython.nbconvert import HTMLExporter\n", |
|
149 | "from IPython.nbconvert import HTMLExporter\n", | |
171 | "\n", |
|
150 | "\n", | |
172 | "## I use `basic` here to have less boilerplate and headers in the HTML.\n", |
|
151 | "## I use `basic` here to have less boilerplate and headers in the HTML.\n", | |
173 | "## we'll see later how to pass config to exporters.\n", |
|
152 | "## we'll see later how to pass config to exporters.\n", | |
174 |
"export |
|
153 | "html_exporter = HTMLExporter(config=Config({'HTMLExporter':{'default_template':'basic'}}))" | |
175 | ] |
|
154 | ] | |
176 | }, |
|
155 | }, | |
177 | { |
|
156 | { | |
178 | "cell_type": "code", |
|
157 | "cell_type": "code", | |
179 |
"execution_count": |
|
158 | "execution_count": 4, | |
180 | "metadata": { |
|
159 | "metadata": { | |
181 | "collapsed": false |
|
160 | "collapsed": false | |
182 | }, |
|
161 | }, | |
183 | "outputs": [], |
|
162 | "outputs": [], | |
184 | "source": [ |
|
163 | "source": [ | |
185 |
"(body, resources) = export |
|
164 | "(body, resources) = html_exporter.from_notebook_node(jake_notebook)" | |
186 | ] |
|
165 | ] | |
187 | }, |
|
166 | }, | |
188 | { |
|
167 | { | |
@@ -199,7 +178,7 b'' | |||||
199 | }, |
|
178 | }, | |
200 | { |
|
179 | { | |
201 | "cell_type": "code", |
|
180 | "cell_type": "code", | |
202 |
"execution_count": |
|
181 | "execution_count": 5, | |
203 | "metadata": { |
|
182 | "metadata": { | |
204 | "collapsed": false |
|
183 | "collapsed": false | |
205 | }, |
|
184 | }, | |
@@ -208,22 +187,22 b'' | |||||
208 | "name": "stdout", |
|
187 | "name": "stdout", | |
209 | "output_type": "stream", |
|
188 | "output_type": "stream", | |
210 | "text": [ |
|
189 | "text": [ | |
211 |
"['inlining', 'output_extension |
|
190 | "['raw_mimetypes', 'inlining', 'metadata', 'output_extension']\n", | |
212 | "defaultdict(None, {'name': 'Notebook'})\n", |
|
191 | "defaultdict(None, {'name': 'Notebook'})\n", | |
213 | "html\n" |
|
192 | ".html\n" | |
214 | ] |
|
193 | ] | |
215 | } |
|
194 | } | |
216 | ], |
|
195 | ], | |
217 | "source": [ |
|
196 | "source": [ | |
218 |
"print resources |
|
197 | "print([key for key in resources ])\n", | |
219 |
"print |
|
198 | "print(resources['metadata'])\n", | |
220 |
"print |
|
199 | "print(resources['output_extension'])\n", | |
221 | "# print resources['inlining'] # too lng to be shown" |
|
200 | "# print resources['inlining'] # too lng to be shown" | |
222 | ] |
|
201 | ] | |
223 | }, |
|
202 | }, | |
224 | { |
|
203 | { | |
225 | "cell_type": "code", |
|
204 | "cell_type": "code", | |
226 |
"execution_count": |
|
205 | "execution_count": 6, | |
227 | "metadata": { |
|
206 | "metadata": { | |
228 | "collapsed": false |
|
207 | "collapsed": false | |
229 | }, |
|
208 | }, | |
@@ -232,20 +211,32 b'' | |||||
232 | "name": "stdout", |
|
211 | "name": "stdout", | |
233 | "output_type": "stream", |
|
212 | "output_type": "stream", | |
234 | "text": [ |
|
213 | "text": [ | |
|
214 | "<!DOCTYPE html>\n", | |||
|
215 | "<html>\n", | |||
|
216 | "<head>\n", | |||
235 | "\n", |
|
217 | "\n", | |
236 | "<div class=\"text_cell_render border-box-sizing rendered_html\">\n", |
|
218 | "<meta charset=\"utf-8\" />\n", | |
237 | "<h1 id=\"XKCD-plots-in-Matplotlib\">XKCD plots in Matplotlib<a class=\"anchor-link\" href=\"#XKCD-plots-in-Matplotlib\">¶</a></h1>\n", |
|
219 | "<title>Notebook</title>\n", | |
238 |
" |
|
220 | "\n", | |
|
221 | "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js\"></script>\n", | |||
|
222 | "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js\"></script>\n", | |||
239 | "\n", |
|
223 | "\n", | |
240 | "<div class=\"text_cell_render border-box-sizing rendered_html\">\n", |
|
224 | "<style type=\"text/css\">\n", | |
241 | "<p>This notebook originally appeared as a blog post at <a href=\"http://jakevdp.github.com/blog/2012/10/07/xkcd-style-plots-in-matplotli...\n" |
|
225 | " /*!\n", | |
|
226 | "*\n", | |||
|
227 | "* Twitter Bootstrap\n", | |||
|
228 | "*\n", | |||
|
229 | "*/\n", | |||
|
230 | "/*! normalize.css v3.0.2 | MIT License | git.io/normalize */\n", | |||
|
231 | "html {\n", | |||
|
232 | " fon...\n" | |||
242 | ] |
|
233 | ] | |
243 | } |
|
234 | } | |
244 | ], |
|
235 | ], | |
245 | "source": [ |
|
236 | "source": [ | |
246 | "# Part of the body, here the first Heading\n", |
|
237 | "# Part of the body, here the first Heading\n", | |
247 | "start = body.index('<h1 id', )\n", |
|
238 | "start = body.index('<h1 id', )\n", | |
248 |
"print |
|
239 | "print(body[:400]+'...')" | |
249 | ] |
|
240 | ] | |
250 | }, |
|
241 | }, | |
251 | { |
|
242 | { | |
@@ -271,7 +262,7 b'' | |||||
271 | }, |
|
262 | }, | |
272 | { |
|
263 | { | |
273 | "cell_type": "code", |
|
264 | "cell_type": "code", | |
274 |
"execution_count": |
|
265 | "execution_count": 7, | |
275 | "metadata": { |
|
266 | "metadata": { | |
276 | "collapsed": false |
|
267 | "collapsed": false | |
277 | }, |
|
268 | }, | |
@@ -279,14 +270,14 b'' | |||||
279 | "source": [ |
|
270 | "source": [ | |
280 | "from IPython.nbconvert import RSTExporter\n", |
|
271 | "from IPython.nbconvert import RSTExporter\n", | |
281 | "\n", |
|
272 | "\n", | |
282 | "rst_export = RSTExporter()\n", |
|
273 | "rst_exporter = RSTExporter()\n", | |
283 | "\n", |
|
274 | "\n", | |
284 | "(body,resources) = rst_export.from_notebook_node(jake_notebook)" |
|
275 | "(body,resources) = rst_exporter.from_notebook_node(jake_notebook)" | |
285 | ] |
|
276 | ] | |
286 | }, |
|
277 | }, | |
287 | { |
|
278 | { | |
288 | "cell_type": "code", |
|
279 | "cell_type": "code", | |
289 |
"execution_count": |
|
280 | "execution_count": 8, | |
290 | "metadata": { |
|
281 | "metadata": { | |
291 | "collapsed": false |
|
282 | "collapsed": false | |
292 | }, |
|
283 | }, | |
@@ -299,20 +290,23 b'' | |||||
299 | "XKCD plots in Matplotlib\n", |
|
290 | "XKCD plots in Matplotlib\n", | |
300 | "========================\n", |
|
291 | "========================\n", | |
301 | "\n", |
|
292 | "\n", | |
302 | "\n", |
|
|||
303 | "This notebook originally appeared as a blog post at `Pythonic\n", |
|
293 | "This notebook originally appeared as a blog post at `Pythonic\n", | |
304 | "Perambulations <http://jakevdp.github.com/blog/2012/10/07/xkcd-style-plots-in-matplotlib/>`_\n", |
|
294 | "Perambulations <http://jakevdp.github.com/blog/2012/10/07/xkcd-style-plots-in-matplotlib/>`__\n", | |
305 | "by Jake Vanderplas.\n", |
|
295 | "by Jake Vanderplas.\n", | |
306 | "\n", |
|
296 | "\n", | |
307 | " *Update: the matplotlib pull request has been merged! See* `*This\n", |
|
297 | ".. raw:: html\n", | |
308 | "post* <http://jakevdp.github.io/blog/2013/07/10/XKCD-plots-in-matplotlib/>`_\n", |
|
298 | "\n", | |
|
299 | " <!-- PELICAN_BEGIN_SUMMARY -->\n", | |||
|
300 | "\n", | |||
|
301 | "*Update: the matplotlib pull request has been merged! See* `*This\n", | |||
|
302 | "post* <http://jakevdp.github.io/blog/2013/07/10/XKCD-plots-in-matplotlib/>`__\n", | |||
309 | "*for a description of the XKCD functionality now built-in to\n", |
|
303 | "*for a description of the XKCD functionality now built-in to\n", | |
310 | "matplotlib!*\n", |
|
304 | "matplotlib!*\n", | |
311 | "\n", |
|
305 | "\n", | |
312 | "One of the problems I've had with typical matplotlib figures is that\n", |
|
306 | "One of the problems I've had with typical matplotlib figures is that\n", | |
313 | "everything in them is so precise, so perfect. For an example of what I\n", |
|
307 | "everything in them is so precise, so perfect. For an example of what I\n", | |
314 | "mean, take a look at this figure:\n", |
|
308 | "mean, take a look at this figure:\n", | |
315 |
" |
|
309 | "\n", | |
316 | ".. code:: python\n", |
|
310 | ".. code:: python\n", | |
317 | "\n", |
|
311 | "\n", | |
318 | " from IPython.display import Image\n", |
|
312 | " from IPython.display import Image\n", | |
@@ -320,41 +314,37 b'' | |||||
320 | "\n", |
|
314 | "\n", | |
321 | "\n", |
|
315 | "\n", | |
322 | "\n", |
|
316 | "\n", | |
323 | "\n", |
|
|||
324 | "\n", |
|
|||
325 | ".. image:: output_3_0.png\n", |
|
317 | ".. image:: output_3_0.png\n", | |
326 | "\n", |
|
318 | "\n", | |
327 | "\n", |
|
319 | "\n", | |
328 | "\n", |
|
320 | "\n", | |
329 | "Sometimes when showing schematic plots, this is the type of figure I\n", |
|
321 | "Sometimes when showing schematic plots, this is the type of figure I\n", | |
330 | "want to display. But drawing it by hand is a pain: I'd rather just use\n", |
|
322 | "want to display. But drawing it by hand is a pain: I'd rather just use\n", | |
331 | "matplotlib. The problem is, matplotlib is a bit...\n", |
|
323 | "matpl...\n", | |
332 | "[.....]\n", |
|
324 | "[.....]\n", | |
333 | "owing schematic plots, this is the type of figure I\n", |
|
325 | "mage:: output_3_0.png\n", | |
|
326 | "\n", | |||
|
327 | "\n", | |||
|
328 | "\n", | |||
|
329 | "Sometimes when showing schematic plots, this is the type of figure I\n", | |||
334 | "want to display. But drawing it by hand is a pain: I'd rather just use\n", |
|
330 | "want to display. But drawing it by hand is a pain: I'd rather just use\n", | |
335 | "matplotlib. The problem is, matplotlib is a bit too precise. Attempting\n", |
|
331 | "matplotlib. The problem is, matplotlib is a bit too precise. Attempting\n", | |
336 | "to duplicate this figure in matplotlib leads to something like this:\n", |
|
332 | "to duplicate this figure in matplotlib leads to something like this:\n", | |
337 |
" |
|
333 | "\n", | |
338 | ".. code:: python\n", |
|
334 | ".. code:: python\n", | |
339 | "\n", |
|
335 | "\n", | |
340 | " Image('http://jakevdp.github.com/figures/mpl_version.png')\n", |
|
336 | " Image('http://jakevdp.github.com/figures/mpl_version.png')\n", | |
341 | "\n", |
|
337 | "\n", | |
342 | "\n", |
|
338 | "\n", | |
343 | "\n", |
|
339 | "\n", | |
344 |
"\n" |
|
340 | ".. image:...\n" | |
345 | "\n", |
|
|||
346 | ".. image:: output_5_0.png\n", |
|
|||
347 | "\n", |
|
|||
348 | "\n", |
|
|||
349 | "\n", |
|
|||
350 | "It just doesn'...\n" |
|
|||
351 | ] |
|
341 | ] | |
352 | } |
|
342 | } | |
353 | ], |
|
343 | ], | |
354 | "source": [ |
|
344 | "source": [ | |
355 |
"print |
|
345 | "print(body[:970]+'...')\n", | |
356 |
"print |
|
346 | "print('[.....]')\n", | |
357 |
"print |
|
347 | "print(body[800:1200]+'...')" | |
358 | ] |
|
348 | ] | |
359 | }, |
|
349 | }, | |
360 | { |
|
350 | { | |
@@ -381,7 +371,7 b'' | |||||
381 | }, |
|
371 | }, | |
382 | { |
|
372 | { | |
383 | "cell_type": "code", |
|
373 | "cell_type": "code", | |
384 |
"execution_count": |
|
374 | "execution_count": 9, | |
385 | "metadata": { |
|
375 | "metadata": { | |
386 | "collapsed": false |
|
376 | "collapsed": false | |
387 | }, |
|
377 | }, | |
@@ -389,28 +379,20 b'' | |||||
389 | { |
|
379 | { | |
390 | "data": { |
|
380 | "data": { | |
391 | "text/plain": [ |
|
381 | "text/plain": [ | |
392 |
"[ |
|
382 | "['output_5_0.png',\n", | |
393 |
" |
|
383 | " 'output_16_0.png',\n", | |
394 |
" |
|
384 | " 'output_13_1.png',\n", | |
395 |
" |
|
385 | " 'output_18_1.png',\n", | |
396 |
" |
|
386 | " 'output_3_0.png']" | |
397 | " u'output_5_0.text',\n", |
|
|||
398 | " u'output_5_0.png',\n", |
|
|||
399 | " u'output_13_1.png',\n", |
|
|||
400 | " u'output_16_0.text',\n", |
|
|||
401 | " u'output_13_0.text',\n", |
|
|||
402 | " u'output_18_1.text',\n", |
|
|||
403 | " u'output_3_0.png',\n", |
|
|||
404 | " u'output_16_0.png']" |
|
|||
405 | ] |
|
387 | ] | |
406 | }, |
|
388 | }, | |
407 |
"execution_count": |
|
389 | "execution_count": 9, | |
408 | "metadata": {}, |
|
390 | "metadata": {}, | |
409 | "output_type": "execute_result" |
|
391 | "output_type": "execute_result" | |
410 | } |
|
392 | } | |
411 | ], |
|
393 | ], | |
412 | "source": [ |
|
394 | "source": [ | |
413 |
"resources['outputs'] |
|
395 | "list(resources['outputs'])" | |
414 | ] |
|
396 | ] | |
415 | }, |
|
397 | }, | |
416 | { |
|
398 | { | |
@@ -428,7 +410,7 b'' | |||||
428 | }, |
|
410 | }, | |
429 | { |
|
411 | { | |
430 | "cell_type": "code", |
|
412 | "cell_type": "code", | |
431 |
"execution_count": 1 |
|
413 | "execution_count": 10, | |
432 | "metadata": { |
|
414 | "metadata": { | |
433 | "collapsed": false |
|
415 | "collapsed": false | |
434 | }, |
|
416 | }, | |
@@ -934,10 +916,10 b'' | |||||
934 | "RU5ErkJggg==\n" |
|
916 | "RU5ErkJggg==\n" | |
935 | ], |
|
917 | ], | |
936 | "text/plain": [ |
|
918 | "text/plain": [ | |
937 |
"<IPython.core.display.Image |
|
919 | "<IPython.core.display.Image object>" | |
938 | ] |
|
920 | ] | |
939 | }, |
|
921 | }, | |
940 |
"execution_count": 1 |
|
922 | "execution_count": 10, | |
941 | "metadata": {}, |
|
923 | "metadata": {}, | |
942 | "output_type": "execute_result" |
|
924 | "output_type": "execute_result" | |
943 | } |
|
925 | } | |
@@ -987,7 +969,7 b'' | |||||
987 | "\n", |
|
969 | "\n", | |
988 | " - Get the notebook data and other required files. (you are responsible for that)\n", |
|
970 | " - Get the notebook data and other required files. (you are responsible for that)\n", | |
989 | " - Feed them to the exporter that will\n", |
|
971 | " - Feed them to the exporter that will\n", | |
990 |
" - sequentially feed the data to a number of ` |
|
972 | " - sequentially feed the data to a number of `Preprocessors`. Preprocessor only act on the **structure**\n", | |
991 | " of the notebook, and have access to it all. \n", |
|
973 | " of the notebook, and have access to it all. \n", | |
992 | " - feed the notebook through the jinja templating engine\n", |
|
974 | " - feed the notebook through the jinja templating engine\n", | |
993 | " - the use templates are configurable.\n", |
|
975 | " - the use templates are configurable.\n", | |
@@ -1000,20 +982,20 b'' | |||||
1000 | "cell_type": "markdown", |
|
982 | "cell_type": "markdown", | |
1001 | "metadata": {}, |
|
983 | "metadata": {}, | |
1002 | "source": [ |
|
984 | "source": [ | |
1003 |
"Here we'll be interested in the ` |
|
985 | "Here we'll be interested in the `Preprocessors`. Each `Preprocessor` is applied successively and in order on the notebook before going through the conversion process.\n", | |
1004 | "\n", |
|
986 | "\n", | |
1005 |
"We provide some |
|
987 | "We provide some preprocessor that do some modification on the notebook structure by default.\n", | |
1006 |
"One of them, the `ExtractOutput |
|
988 | "One of them, the `ExtractOutputPreprocessor` is responsible for crawling notebook,\n", | |
1007 | "finding all the figures, and put them into the resources directory, as well as choosing the key\n", |
|
989 | "finding all the figures, and put them into the resources directory, as well as choosing the key\n", | |
1008 | "(`filename_xx_y.extension`) that can replace the figure in the template.\n", |
|
990 | "(`filename_xx_y.extension`) that can replace the figure in the template.\n", | |
1009 | "\n", |
|
991 | "\n", | |
1010 | "\n", |
|
992 | "\n", | |
1011 |
"The `ExtractOutput |
|
993 | "The `ExtractOutputPreprocessor` is special in the fact that it **should** be availlable on all `Exporter`s, but is just inactive by default on some exporter." | |
1012 | ] |
|
994 | ] | |
1013 | }, |
|
995 | }, | |
1014 | { |
|
996 | { | |
1015 | "cell_type": "code", |
|
997 | "cell_type": "code", | |
1016 |
"execution_count": 1 |
|
998 | "execution_count": 11, | |
1017 | "metadata": { |
|
999 | "metadata": { | |
1018 | "collapsed": false |
|
1000 | "collapsed": false | |
1019 | }, |
|
1001 | }, | |
@@ -1021,23 +1003,25 b'' | |||||
1021 | { |
|
1003 | { | |
1022 | "data": { |
|
1004 | "data": { | |
1023 | "text/plain": [ |
|
1005 | "text/plain": [ | |
1024 |
"[<function IPython.nbconvert. |
|
1006 | "[<function IPython.nbconvert.preprocessors.coalescestreams.cell_preprocessor.<locals>.wrappedfunc>,\n", | |
1025 |
" <IPython.nbconvert. |
|
1007 | " <IPython.nbconvert.preprocessors.svg2pdf.SVG2PDFPreprocessor at 0x107d1a630>,\n", | |
1026 |
" <IPython.nbconvert. |
|
1008 | " <IPython.nbconvert.preprocessors.extractoutput.ExtractOutputPreprocessor at 0x107d1a748>,\n", | |
1027 |
" <IPython.nbconvert. |
|
1009 | " <IPython.nbconvert.preprocessors.csshtmlheader.CSSHTMLHeaderPreprocessor at 0x107d1aba8>,\n", | |
1028 |
" <IPython.nbconvert. |
|
1010 | " <IPython.nbconvert.preprocessors.revealhelp.RevealHelpPreprocessor at 0x107d1a710>,\n", | |
1029 |
" <IPython.nbconvert. |
|
1011 | " <IPython.nbconvert.preprocessors.latex.LatexPreprocessor at 0x107daa860>,\n", | |
1030 | " <IPython.nbconvert.transformers.sphinx.SphinxTransformer at 0x10d2a7b90>]" |
|
1012 | " <IPython.nbconvert.preprocessors.clearoutput.ClearOutputPreprocessor at 0x107db7080>,\n", | |
|
1013 | " <IPython.nbconvert.preprocessors.execute.ExecutePreprocessor at 0x107db7160>,\n", | |||
|
1014 | " <IPython.nbconvert.preprocessors.highlightmagics.HighlightMagicsPreprocessor at 0x107db7048>]" | |||
1031 | ] |
|
1015 | ] | |
1032 | }, |
|
1016 | }, | |
1033 |
"execution_count": 1 |
|
1017 | "execution_count": 11, | |
1034 | "metadata": {}, |
|
1018 | "metadata": {}, | |
1035 | "output_type": "execute_result" |
|
1019 | "output_type": "execute_result" | |
1036 | } |
|
1020 | } | |
1037 | ], |
|
1021 | ], | |
1038 | "source": [ |
|
1022 | "source": [ | |
1039 | "# second transformer shoudl be Instance of ExtractFigureTransformer\n", |
|
1023 | "# 3rd one should be <ExtractOutputPreprocessor>\n", | |
1040 | "exportHtml._transformers # 3rd one shouel be <ExtractOutputTransformer>" |
|
1024 | "html_exporter._preprocessors" | |
1041 | ] |
|
1025 | ] | |
1042 | }, |
|
1026 | }, | |
1043 | { |
|
1027 | { | |
@@ -1047,14 +1031,14 b'' | |||||
1047 | "To enable it we will use IPython configuration/Traitlets system. If you are have already set some IPython configuration options, \n", |
|
1031 | "To enable it we will use IPython configuration/Traitlets system. If you are have already set some IPython configuration options, \n", | |
1048 | "this will look pretty familiar to you. Configuration option are always of the form:\n", |
|
1032 | "this will look pretty familiar to you. Configuration option are always of the form:\n", | |
1049 | "\n", |
|
1033 | "\n", | |
1050 | " ClassName.attribute_name = value\n", |
|
1034 | " ClassName.attribute_name = value\n", | |
1051 | " \n", |
|
1035 | " \n", | |
1052 |
"A few ways exist to create such config, like reading a config file in your profile, but you can also do it programatically usign a dictionary. Let's create such a config object, and see the difference if we pass it to our `H |
|
1036 | "A few ways exist to create such config, like reading a config file in your profile, but you can also do it programatically usign a dictionary. Let's create such a config object, and see the difference if we pass it to our `HTMLExporter`" | |
1053 | ] |
|
1037 | ] | |
1054 | }, |
|
1038 | }, | |
1055 | { |
|
1039 | { | |
1056 | "cell_type": "code", |
|
1040 | "cell_type": "code", | |
1057 |
"execution_count": 1 |
|
1041 | "execution_count": 12, | |
1058 | "metadata": { |
|
1042 | "metadata": { | |
1059 | "collapsed": false |
|
1043 | "collapsed": false | |
1060 | }, |
|
1044 | }, | |
@@ -1064,31 +1048,23 b'' | |||||
1064 | "output_type": "stream", |
|
1048 | "output_type": "stream", | |
1065 | "text": [ |
|
1049 | "text": [ | |
1066 | "resources without the \"figures\" key :\n", |
|
1050 | "resources without the \"figures\" key :\n", | |
1067 |
"['inlining', 'output_extension |
|
1051 | "['raw_mimetypes', 'inlining', 'metadata', 'output_extension']\n", | |
1068 | "\n", |
|
1052 | "\n", | |
1069 |
"Here we have one more field |
|
1053 | "Here we have one more field\n", | |
1070 |
"['outputs', 'inlining', 'output_extension |
|
1054 | "['outputs', 'raw_mimetypes', 'inlining', 'metadata', 'output_extension']\n" | |
1071 | ] |
|
1055 | ] | |
1072 | }, |
|
1056 | }, | |
1073 | { |
|
1057 | { | |
1074 | "data": { |
|
1058 | "data": { | |
1075 | "text/plain": [ |
|
1059 | "text/plain": [ | |
1076 |
"[ |
|
1060 | "['output_5_0.png',\n", | |
1077 |
" |
|
1061 | " 'output_16_0.png',\n", | |
1078 |
" |
|
1062 | " 'output_13_1.png',\n", | |
1079 |
" |
|
1063 | " 'output_18_1.png',\n", | |
1080 |
" |
|
1064 | " 'output_3_0.png']" | |
1081 | " u'output_5_0.text',\n", |
|
|||
1082 | " u'output_5_0.png',\n", |
|
|||
1083 | " u'output_13_1.png',\n", |
|
|||
1084 | " u'output_16_0.text',\n", |
|
|||
1085 | " u'output_13_0.text',\n", |
|
|||
1086 | " u'output_18_1.text',\n", |
|
|||
1087 | " u'output_3_0.png',\n", |
|
|||
1088 | " u'output_16_0.png']" |
|
|||
1089 | ] |
|
1065 | ] | |
1090 | }, |
|
1066 | }, | |
1091 |
"execution_count": 1 |
|
1067 | "execution_count": 12, | |
1092 | "metadata": {}, |
|
1068 | "metadata": {}, | |
1093 | "output_type": "execute_result" |
|
1069 | "output_type": "execute_result" | |
1094 | } |
|
1070 | } | |
@@ -1097,22 +1073,22 b'' | |||||
1097 | "from IPython.config import Config\n", |
|
1073 | "from IPython.config import Config\n", | |
1098 | "\n", |
|
1074 | "\n", | |
1099 | "c = Config({\n", |
|
1075 | "c = Config({\n", | |
1100 |
" 'ExtractOutput |
|
1076 | " 'ExtractOutputPreprocessor':{'enabled':True}\n", | |
1101 | " })\n", |
|
1077 | " })\n", | |
1102 | "\n", |
|
1078 | "\n", | |
1103 |
"exportH |
|
1079 | "exportHTML = HTMLExporter()\n", | |
1104 |
"exportH |
|
1080 | "exportHTML_and_figs = HTMLExporter(config=c)\n", | |
1105 | "\n", |
|
1081 | "\n", | |
1106 |
"(_, resources) = exportH |
|
1082 | "(_, resources) = exportHTML.from_notebook_node(jake_notebook)\n", | |
1107 |
"(_, resources_with_fig) = exportH |
|
1083 | "(_, resources_with_fig) = exportHTML_and_figs.from_notebook_node(jake_notebook)\n", | |
1108 | "\n", |
|
1084 | "\n", | |
1109 |
"print |
|
1085 | "print('resources without the \"figures\" key :')\n", | |
1110 |
"print |
|
1086 | "print(list(resources))\n", | |
1111 | "\n", |
|
1087 | "\n", | |
1112 |
"print |
|
1088 | "print('')\n", | |
1113 |
"print |
|
1089 | "print('Here we have one more field')\n", | |
1114 |
"print |
|
1090 | "print(list(resources_with_fig))\n", | |
1115 |
"resources_with_fig['outputs'] |
|
1091 | "list(resources_with_fig['outputs'])" | |
1116 | ] |
|
1092 | ] | |
1117 | }, |
|
1093 | }, | |
1118 | { |
|
1094 | { | |
@@ -1126,27 +1102,27 b'' | |||||
1126 | "cell_type": "markdown", |
|
1102 | "cell_type": "markdown", | |
1127 | "metadata": {}, |
|
1103 | "metadata": {}, | |
1128 | "source": [ |
|
1104 | "source": [ | |
1129 |
"#### Custom |
|
1105 | "#### Custom Preprocessor" | |
1130 | ] |
|
1106 | ] | |
1131 | }, |
|
1107 | }, | |
1132 | { |
|
1108 | { | |
1133 | "cell_type": "markdown", |
|
1109 | "cell_type": "markdown", | |
1134 | "metadata": {}, |
|
1110 | "metadata": {}, | |
1135 | "source": [ |
|
1111 | "source": [ | |
1136 |
"Of course you can imagine many transformation that you would like to apply to a notebook. This is one of the reason we provide a way to register your own |
|
1112 | "Of course you can imagine many transformation that you would like to apply to a notebook. This is one of the reason we provide a way to register your own preprocessors that will be applied to the notebook after the default ones.\n", | |
1137 | "\n", |
|
1113 | "\n", | |
1138 |
"To do so you'll have to pass an ordered list of ` |
|
1114 | "To do so you'll have to pass an ordered list of `Preprocessor`s to the Exporter constructor. \n", | |
1139 | "\n", |
|
1115 | "\n", | |
1140 |
"But what is an |
|
1116 | "But what is an preprocessor ? Preprocessor can be either *decorated function* for dead-simple `Preprocessor`s that apply\n", | |
1141 | "independently to each cell, for more advance transformation that support configurability You have to inherit from\n", |
|
1117 | "independently to each cell, for more advance transformation that support configurability You have to inherit from\n", | |
1142 |
"` |
|
1118 | "`Preprocessor` and define a `call` method as we'll see below.\n", | |
1143 | "\n", |
|
1119 | "\n", | |
1144 | "All transforers have a magic attribute that allows it to be activated/disactivate from the config dict." |
|
1120 | "All transforers have a magic attribute that allows it to be activated/disactivate from the config dict." | |
1145 | ] |
|
1121 | ] | |
1146 | }, |
|
1122 | }, | |
1147 | { |
|
1123 | { | |
1148 | "cell_type": "code", |
|
1124 | "cell_type": "code", | |
1149 |
"execution_count": 1 |
|
1125 | "execution_count": 13, | |
1150 | "metadata": { |
|
1126 | "metadata": { | |
1151 | "collapsed": false |
|
1127 | "collapsed": false | |
1152 | }, |
|
1128 | }, | |
@@ -1157,27 +1133,29 b'' | |||||
1157 | "text": [ |
|
1133 | "text": [ | |
1158 | "Four relevant docstring\n", |
|
1134 | "Four relevant docstring\n", | |
1159 | "=============================\n", |
|
1135 | "=============================\n", | |
1160 |
" A configurable |
|
1136 | " A configurable preprocessor\n", | |
1161 | "\n", |
|
1137 | "\n", | |
1162 | " Inherit from this class if you wish to have configurability for your\n", |
|
1138 | " Inherit from this class if you wish to have configurability for your\n", | |
1163 |
" |
|
1139 | " preprocessor.\n", | |
1164 | "\n", |
|
1140 | "\n", | |
1165 |
" Any configurable traitlets this class exposed will be configurable in |
|
1141 | " Any configurable traitlets this class exposed will be configurable in\n", | |
1166 | " using c.SubClassName.atribute=value\n", |
|
1142 | " profiles using c.SubClassName.attribute = value\n", | |
1167 | "\n", |
|
1143 | "\n", | |
1168 |
" you can overwrite |
|
1144 | " you can overwrite :meth:`preprocess_cell` to apply a transformation\n", | |
1169 | " or __call__ if you prefer your own logic. See corresponding docstring for informations.\n", |
|
1145 | " independently on each cell or :meth:`preprocess` if you prefer your own\n", | |
|
1146 | " logic. See corresponding docstring for informations.\n", | |||
1170 | "\n", |
|
1147 | "\n", | |
1171 | " Disabled by default and can be enabled via the config by\n", |
|
1148 | " Disabled by default and can be enabled via the config by\n", | |
1172 |
" 'c.Your |
|
1149 | " 'c.YourPreprocessorName.enabled = True'\n", | |
1173 | " \n", |
|
1150 | " \n", | |
1174 | "=============================\n", |
|
1151 | "=============================\n", | |
1175 | "\n", |
|
1152 | "\n", | |
1176 |
" |
|
1153 | " Preprocessing to apply on each notebook.\n", | |
1177 | " \n", |
|
1154 | " \n", | |
1178 |
" |
|
1155 | " Must return modified nb, resources.\n", | |
1179 | " If you wish to apply your transform on each cell, you might want to \n", |
|
1156 | " \n", | |
1180 | " overwrite transform_cell method instead.\n", |
|
1157 | " If you wish to apply your preprocessing to each cell, you might want\n", | |
|
1158 | " to override preprocess_cell method instead.\n", | |||
1181 | " \n", |
|
1159 | " \n", | |
1182 | " Parameters\n", |
|
1160 | " Parameters\n", | |
1183 | " ----------\n", |
|
1161 | " ----------\n", | |
@@ -1185,12 +1163,12 b'' | |||||
1185 | " Notebook being converted\n", |
|
1163 | " Notebook being converted\n", | |
1186 | " resources : dictionary\n", |
|
1164 | " resources : dictionary\n", | |
1187 | " Additional resources used in the conversion process. Allows\n", |
|
1165 | " Additional resources used in the conversion process. Allows\n", | |
1188 |
" |
|
1166 | " preprocessors to pass variables into the Jinja engine.\n", | |
1189 | " \n", |
|
1167 | " \n", | |
1190 | "=============================\n", |
|
1168 | "=============================\n", | |
1191 | "\n", |
|
1169 | "\n", | |
1192 |
" Over |
|
1170 | " Override if you want to apply some preprocessing to each cell.\n", | |
1193 |
" |
|
1171 | " Must return modified cell and resource dictionary.\n", | |
1194 | " \n", |
|
1172 | " \n", | |
1195 | " Parameters\n", |
|
1173 | " Parameters\n", | |
1196 | " ----------\n", |
|
1174 | " ----------\n", | |
@@ -1198,7 +1176,7 b'' | |||||
1198 | " Notebook cell being processed\n", |
|
1176 | " Notebook cell being processed\n", | |
1199 | " resources : dictionary\n", |
|
1177 | " resources : dictionary\n", | |
1200 | " Additional resources used in the conversion process. Allows\n", |
|
1178 | " Additional resources used in the conversion process. Allows\n", | |
1201 |
" |
|
1179 | " preprocessors to pass variables into the Jinja engine.\n", | |
1202 | " index : int\n", |
|
1180 | " index : int\n", | |
1203 | " Index of the cell being processed\n", |
|
1181 | " Index of the cell being processed\n", | |
1204 | " \n", |
|
1182 | " \n", | |
@@ -1207,16 +1185,16 b'' | |||||
1207 | } |
|
1185 | } | |
1208 | ], |
|
1186 | ], | |
1209 | "source": [ |
|
1187 | "source": [ | |
1210 |
"from IPython.nbconvert. |
|
1188 | "from IPython.nbconvert.preprocessors import Preprocessor\n", | |
1211 | "import IPython.config\n", |
|
1189 | "import IPython.config\n", | |
1212 |
"print |
|
1190 | "print(\"Four relevant docstring\")\n", | |
1213 |
"print |
|
1191 | "print('=============================')\n", | |
1214 |
"print |
|
1192 | "print(Preprocessor.__doc__)\n", | |
1215 |
"print |
|
1193 | "print('=============================')\n", | |
1216 |
"print |
|
1194 | "print(Preprocessor.preprocess.__doc__)\n", | |
1217 |
"print |
|
1195 | "print('=============================')\n", | |
1218 |
"print |
|
1196 | "print(Preprocessor.preprocess_cell.__doc__)\n", | |
1219 |
"print |
|
1197 | "print('=============================')" | |
1220 | ] |
|
1198 | ] | |
1221 | }, |
|
1199 | }, | |
1222 | { |
|
1200 | { | |
@@ -1224,7 +1202,7 b'' | |||||
1224 | "metadata": {}, |
|
1202 | "metadata": {}, | |
1225 | "source": [ |
|
1203 | "source": [ | |
1226 | "***\n", |
|
1204 | "***\n", | |
1227 |
"We don't provide convenient method to be aplied on each worksheet as the **data structure** for worksheet will be removed. (not the worksheet function |
|
1205 | "We don't provide convenient method to be aplied on each worksheet as the **data structure** for worksheet will be removed. (not the worksheet functionality, which is still on it's way)\n", | |
1228 | "***" |
|
1206 | "***" | |
1229 | ] |
|
1207 | ] | |
1230 | }, |
|
1208 | }, | |
@@ -1239,14 +1217,14 b'' | |||||
1239 | "cell_type": "markdown", |
|
1217 | "cell_type": "markdown", | |
1240 | "metadata": {}, |
|
1218 | "metadata": {}, | |
1241 | "source": [ |
|
1219 | "source": [ | |
1242 |
"I'll now demonstrate a specific example [requested](https://github.com/ipython/nbconvert/pull/137#issuecomment-18658235) while nbconvert 2 was be |
|
1220 | "I'll now demonstrate a specific example [requested](https://github.com/ipython/nbconvert/pull/137#issuecomment-18658235) while nbconvert 2 was being developed. The ability to exclude cell from the conversion process based on their index. \n", | |
1243 | "\n", |
|
1221 | "\n", | |
1244 | "I'll let you imagin how to inject cell, if what you just want is to happend static content at the beginning/end of a notebook, plese refer to templating section, it will be much easier and cleaner." |
|
1222 | "I'll let you imagin how to inject cell, if what you just want is to happend static content at the beginning/end of a notebook, plese refer to templating section, it will be much easier and cleaner." | |
1245 | ] |
|
1223 | ] | |
1246 | }, |
|
1224 | }, | |
1247 | { |
|
1225 | { | |
1248 | "cell_type": "code", |
|
1226 | "cell_type": "code", | |
1249 |
"execution_count": 1 |
|
1227 | "execution_count": 14, | |
1250 | "metadata": { |
|
1228 | "metadata": { | |
1251 | "collapsed": false |
|
1229 | "collapsed": false | |
1252 | }, |
|
1230 | }, | |
@@ -1257,71 +1235,66 b'' | |||||
1257 | }, |
|
1235 | }, | |
1258 | { |
|
1236 | { | |
1259 | "cell_type": "code", |
|
1237 | "cell_type": "code", | |
1260 |
"execution_count": 1 |
|
1238 | "execution_count": 15, | |
1261 | "metadata": { |
|
1239 | "metadata": { | |
1262 | "collapsed": false |
|
1240 | "collapsed": false | |
1263 | }, |
|
1241 | }, | |
1264 | "outputs": [], |
|
1242 | "outputs": [], | |
1265 | "source": [ |
|
1243 | "source": [ | |
1266 |
"class PelicanSubCell( |
|
1244 | "class PelicanSubCell(Preprocessor):\n", | |
1267 |
" \"\"\"A Pelican specific |
|
1245 | " \"\"\"A Pelican specific preprocessor to remove somme of the cells of a notebook\"\"\"\n", | |
1268 | " \n", |
|
1246 | " \n", | |
1269 | " # I could also read the cells from nbc.metadata.pelican is someone wrote a JS extension\n", |
|
1247 | " # I could also read the cells from nbc.metadata.pelican is someone wrote a JS extension\n", | |
1270 | " # But I'll stay with configurable value. \n", |
|
1248 | " # But I'll stay with configurable value. \n", | |
1271 | " start = Integer(0, config=True, help=\"first cell of notebook to be converted\")\n", |
|
1249 | " start = Integer(0, config=True, help=\"first cell of notebook to be converted\")\n", | |
1272 | " end = Integer(-1, config=True, help=\"last cell of notebook to be converted\")\n", |
|
1250 | " end = Integer(-1, config=True, help=\"last cell of notebook to be converted\")\n", | |
1273 | " \n", |
|
1251 | " \n", | |
1274 |
" def |
|
1252 | " def preprocess(self, nb, resources):\n", | |
1275 | "\n", |
|
1253 | "\n", | |
1276 | " #nbc = deepcopy(nb)\n", |
|
1254 | " #nbc = deepcopy(nb)\n", | |
1277 | " nbc = nb\n", |
|
1255 | " nbc = nb\n", | |
1278 |
" # don't print in real |
|
1256 | " # don't print in real preprocessor !!!\n", | |
1279 |
" print |
|
1257 | " print(\"I'll keep only cells from \", self.start, \"to \", self.end, \"\\n\\n\")\n", | |
1280 | " for worksheet in nbc.worksheets :\n", |
|
1258 | " nbc.cells = nb.cells[self.start:self.end] \n", | |
1281 | " cells = worksheet.cells[:]\n", |
|
|||
1282 | " worksheet.cells = cells[self.start:self.end] \n", |
|
|||
1283 | " return nbc, resources" |
|
1259 | " return nbc, resources" | |
1284 | ] |
|
1260 | ] | |
1285 | }, |
|
1261 | }, | |
1286 | { |
|
1262 | { | |
1287 | "cell_type": "code", |
|
1263 | "cell_type": "code", | |
1288 |
"execution_count": 1 |
|
1264 | "execution_count": 16, | |
1289 | "metadata": { |
|
1265 | "metadata": { | |
1290 | "collapsed": false |
|
1266 | "collapsed": false | |
1291 | }, |
|
1267 | }, | |
1292 | "outputs": [], |
|
1268 | "outputs": [], | |
1293 | "source": [ |
|
1269 | "source": [ | |
1294 | "# I create this on the fly, but this could be loaded from a DB, and config object support merging...\n", |
|
1270 | "# I create this on the fly, but this could be loaded from a DB, and config object support merging...\n", | |
1295 |
"c = Config( |
|
1271 | "c = Config()\n", | |
1296 | " 'PelicanSubCell':{\n", |
|
1272 | "c.PelicanSubCell.enabled = True\n", | |
1297 | " 'enabled':True,\n", |
|
1273 | "c.PelicanSubCell.start = 4\n", | |
1298 | " 'start':4,\n", |
|
1274 | "c.PelicanSubCell.end = 6" | |
1299 | " 'end':6,\n", |
|
|||
1300 | " }\n", |
|
|||
1301 | " })" |
|
|||
1302 | ] |
|
1275 | ] | |
1303 | }, |
|
1276 | }, | |
1304 | { |
|
1277 | { | |
1305 | "cell_type": "markdown", |
|
1278 | "cell_type": "markdown", | |
1306 | "metadata": {}, |
|
1279 | "metadata": {}, | |
1307 | "source": [ |
|
1280 | "source": [ | |
1308 |
"I'm creating a pelican exporter that take `PelicanSubCell` extra |
|
1281 | "I'm creating a pelican exporter that take `PelicanSubCell` extra preprocessors and a `config` object as parameter. This might seem redundant, but with configuration system you'll see that one can register an inactive preprocessor on all exporters and activate it at will form its config files and command line. " | |
1309 | ] |
|
1282 | ] | |
1310 | }, |
|
1283 | }, | |
1311 | { |
|
1284 | { | |
1312 | "cell_type": "code", |
|
1285 | "cell_type": "code", | |
1313 |
"execution_count": 1 |
|
1286 | "execution_count": 17, | |
1314 | "metadata": { |
|
1287 | "metadata": { | |
1315 | "collapsed": false |
|
1288 | "collapsed": false | |
1316 | }, |
|
1289 | }, | |
1317 | "outputs": [], |
|
1290 | "outputs": [], | |
1318 | "source": [ |
|
1291 | "source": [ | |
1319 |
"pelican = RSTExporter( |
|
1292 | "pelican = RSTExporter(preprocessors=[PelicanSubCell], config=c)" | |
1320 | ] |
|
1293 | ] | |
1321 | }, |
|
1294 | }, | |
1322 | { |
|
1295 | { | |
1323 | "cell_type": "code", |
|
1296 | "cell_type": "code", | |
1324 |
"execution_count": 1 |
|
1297 | "execution_count": 18, | |
1325 | "metadata": { |
|
1298 | "metadata": { | |
1326 | "collapsed": false |
|
1299 | "collapsed": false | |
1327 | }, |
|
1300 | }, | |
@@ -1338,15 +1311,13 b'' | |||||
1338 | "want to display. But drawing it by hand is a pain: I'd rather just use\n", |
|
1311 | "want to display. But drawing it by hand is a pain: I'd rather just use\n", | |
1339 | "matplotlib. The problem is, matplotlib is a bit too precise. Attempting\n", |
|
1312 | "matplotlib. The problem is, matplotlib is a bit too precise. Attempting\n", | |
1340 | "to duplicate this figure in matplotlib leads to something like this:\n", |
|
1313 | "to duplicate this figure in matplotlib leads to something like this:\n", | |
1341 |
" |
|
1314 | "\n", | |
1342 | ".. code:: python\n", |
|
1315 | ".. code:: python\n", | |
1343 | "\n", |
|
1316 | "\n", | |
1344 | " Image('http://jakevdp.github.com/figures/mpl_version.png')\n", |
|
1317 | " Image('http://jakevdp.github.com/figures/mpl_version.png')\n", | |
1345 | "\n", |
|
1318 | "\n", | |
1346 | "\n", |
|
1319 | "\n", | |
1347 | "\n", |
|
1320 | "\n", | |
1348 | "\n", |
|
|||
1349 | "\n", |
|
|||
1350 | ".. image:: output_5_0.png\n", |
|
1321 | ".. image:: output_5_0.png\n", | |
1351 | "\n", |
|
1322 | "\n", | |
1352 | "\n", |
|
1323 | "\n", | |
@@ -1355,80 +1326,7 b'' | |||||
1355 | } |
|
1326 | } | |
1356 | ], |
|
1327 | ], | |
1357 | "source": [ |
|
1328 | "source": [ | |
1358 |
"print |
|
1329 | "print(pelican.from_notebook_node(jake_notebook)[0])" | |
1359 | ] |
|
|||
1360 | }, |
|
|||
1361 | { |
|
|||
1362 | "cell_type": "markdown", |
|
|||
1363 | "metadata": {}, |
|
|||
1364 | "source": [ |
|
|||
1365 | "### Programatic example of extending templates / cutom filters" |
|
|||
1366 | ] |
|
|||
1367 | }, |
|
|||
1368 | { |
|
|||
1369 | "cell_type": "code", |
|
|||
1370 | "execution_count": 20, |
|
|||
1371 | "metadata": { |
|
|||
1372 | "collapsed": false |
|
|||
1373 | }, |
|
|||
1374 | "outputs": [], |
|
|||
1375 | "source": [ |
|
|||
1376 | "from IPython.nbconvert.filters.highlight import _pygment_highlight\n", |
|
|||
1377 | "from pygments.formatters import HtmlFormatter\n", |
|
|||
1378 | "\n", |
|
|||
1379 | "from IPython.nbconvert.exporters import HTMLExporter\n", |
|
|||
1380 | "from IPython.config import Config\n", |
|
|||
1381 | "\n", |
|
|||
1382 | "from IPython.nbformat import current as nbformat" |
|
|||
1383 | ] |
|
|||
1384 | }, |
|
|||
1385 | { |
|
|||
1386 | "cell_type": "markdown", |
|
|||
1387 | "metadata": {}, |
|
|||
1388 | "source": [ |
|
|||
1389 | "Here we define a dustom 'highlight' filter that apply a custom class to code in css. We register this filter with a already existing name, so it will replace the default one." |
|
|||
1390 | ] |
|
|||
1391 | }, |
|
|||
1392 | { |
|
|||
1393 | "cell_type": "code", |
|
|||
1394 | "execution_count": 21, |
|
|||
1395 | "metadata": { |
|
|||
1396 | "collapsed": false |
|
|||
1397 | }, |
|
|||
1398 | "outputs": [], |
|
|||
1399 | "source": [ |
|
|||
1400 | "def my_highlight(source, language='ipython'):\n", |
|
|||
1401 | " formatter = HtmlFormatter(cssclass='highlight-ipynb')\n", |
|
|||
1402 | " return _pygment_highlight(source, formatter, language)\n", |
|
|||
1403 | " \n", |
|
|||
1404 | "c = Config({'CSSHtmlHeaderTransformer':\n", |
|
|||
1405 | " {'enabled':False, 'highlight_class':'highlight-ipynb'}})\n", |
|
|||
1406 | "\n", |
|
|||
1407 | "exportHtml = HTMLExporter( config=c , filters={'highlight2html': my_highlight} )\n", |
|
|||
1408 | "(body,resources) = exportHtml.from_notebook_node(jake_notebook)" |
|
|||
1409 | ] |
|
|||
1410 | }, |
|
|||
1411 | { |
|
|||
1412 | "cell_type": "code", |
|
|||
1413 | "execution_count": 22, |
|
|||
1414 | "metadata": { |
|
|||
1415 | "collapsed": false |
|
|||
1416 | }, |
|
|||
1417 | "outputs": [ |
|
|||
1418 | { |
|
|||
1419 | "data": { |
|
|||
1420 | "text/plain": [ |
|
|||
1421 | "u'<div class=\"highlight-ipynb\"><pre><span class=\"kn\">from</span>'" |
|
|||
1422 | ] |
|
|||
1423 | }, |
|
|||
1424 | "execution_count": 22, |
|
|||
1425 | "metadata": {}, |
|
|||
1426 | "output_type": "execute_result" |
|
|||
1427 | } |
|
|||
1428 | ], |
|
|||
1429 | "source": [ |
|
|||
1430 | "i = body.index('highlight-ipynb')\n", |
|
|||
1431 | "body[i-12:i+50]" |
|
|||
1432 | ] |
|
1330 | ] | |
1433 | }, |
|
1331 | }, | |
1434 | { |
|
1332 | { | |
@@ -1440,7 +1338,7 b'' | |||||
1440 | }, |
|
1338 | }, | |
1441 | { |
|
1339 | { | |
1442 | "cell_type": "code", |
|
1340 | "cell_type": "code", | |
1443 |
"execution_count": |
|
1341 | "execution_count": 19, | |
1444 | "metadata": { |
|
1342 | "metadata": { | |
1445 | "collapsed": false |
|
1343 | "collapsed": false | |
1446 | }, |
|
1344 | }, | |
@@ -1449,7 +1347,7 b'' | |||||
1449 | "name": "stdout", |
|
1347 | "name": "stdout", | |
1450 | "output_type": "stream", |
|
1348 | "output_type": "stream", | |
1451 | "text": [ |
|
1349 | "text": [ | |
1452 | "<p>This post was written entirely in an IPython Notebook: the notebook file is available for download <a href=\"http://jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb\">here</a>. For more information on blogging with notebooks in octopress, see my <a href=\"http://jakevdp.github.com/blog/2012/10/04/blogging-with-ipython/\">previous post</a> on the subject.</p>\n", |
|
1350 | "</div>\n", | |
1453 | "</div>\n", |
|
1351 | "</div>\n", | |
1454 | "FOOOOOOOOTEEEEER\n", |
|
1352 | "FOOOOOOOOTEEEEER\n", | |
1455 | "\n" |
|
1353 | "\n" | |
@@ -1459,9 +1357,9 b'' | |||||
1459 | "source": [ |
|
1357 | "source": [ | |
1460 | "from jinja2 import DictLoader\n", |
|
1358 | "from jinja2 import DictLoader\n", | |
1461 | "\n", |
|
1359 | "\n", | |
1462 |
"dl = DictLoader({' |
|
1360 | "dl = DictLoader({'full.tpl': \n", | |
1463 | "\"\"\"\n", |
|
1361 | "\"\"\"\n", | |
1464 |
"{%- extends ' |
|
1362 | "{%- extends 'basic.tpl' -%} \n", | |
1465 | "\n", |
|
1363 | "\n", | |
1466 | "{% block footer %}\n", |
|
1364 | "{% block footer %}\n", | |
1467 | "FOOOOOOOOTEEEEER\n", |
|
1365 | "FOOOOOOOOTEEEEER\n", | |
@@ -1469,10 +1367,10 b'' | |||||
1469 | "\"\"\"})\n", |
|
1367 | "\"\"\"})\n", | |
1470 | "\n", |
|
1368 | "\n", | |
1471 | "\n", |
|
1369 | "\n", | |
1472 |
"exportH |
|
1370 | "exportHTML = HTMLExporter(extra_loaders=[dl])\n", | |
1473 |
"(body,resources) = exportH |
|
1371 | "(body,resources) = exportHTML.from_notebook_node(jake_notebook)\n", | |
1474 | "for l in body.split('\\n')[-4:]:\n", |
|
1372 | "for l in body.split('\\n')[-4:]:\n", | |
1475 |
" print |
|
1373 | " print(l)" | |
1476 | ] |
|
1374 | ] | |
1477 | }, |
|
1375 | }, | |
1478 | { |
|
1376 | { | |
@@ -1501,7 +1399,7 b'' | |||||
1501 | "metadata": {}, |
|
1399 | "metadata": {}, | |
1502 | "source": [ |
|
1400 | "source": [ | |
1503 | "<center>\n", |
|
1401 | "<center>\n", | |
1504 |
"<blockquote class=\"twitter-tweet\"><p>As <a href=\"https://twitter.com/Mbussonn\">@Mbussonn</a> requested... easieeeeer! Deploy your Nikola site with just a click in the IPython notebook! <a href=\"http://t.co/860sJunZvj\">http://t.co/860sJunZvj</a> cc <a href=\"https://twitter.com/ralsina\">@ralsina</a></p>— Dami |
|
1402 | "<blockquote class=\"twitter-tweet\"><p>As <a href=\"https://twitter.com/Mbussonn\">@Mbussonn</a> requested... easieeeeer! Deploy your Nikola site with just a click in the IPython notebook! <a href=\"http://t.co/860sJunZvj\">http://t.co/860sJunZvj</a> cc <a href=\"https://twitter.com/ralsina\">@ralsina</a></p>— DamiΓ‘n Avila (@damian_avila) <a href=\"https://twitter.com/damian_avila/statuses/370306057828335616\">August 21, 2013</a></blockquote>\n", | |
1505 | "</center>" |
|
1403 | "</center>" | |
1506 | ] |
|
1404 | ] | |
1507 | }, |
|
1405 | }, | |
@@ -1525,18 +1423,26 b'' | |||||
1525 | "source": [ |
|
1423 | "source": [ | |
1526 | "Jinja blocks use `{% %}`by default which does not play nicely with $\\LaTeX$, hence thoses are replaced by `((* *))` in latex templates." |
|
1424 | "Jinja blocks use `{% %}`by default which does not play nicely with $\\LaTeX$, hence thoses are replaced by `((* *))` in latex templates." | |
1527 | ] |
|
1425 | ] | |
1528 | }, |
|
|||
1529 | { |
|
|||
1530 | "cell_type": "code", |
|
|||
1531 | "execution_count": null, |
|
|||
1532 | "metadata": { |
|
|||
1533 | "collapsed": false |
|
|||
1534 | }, |
|
|||
1535 | "outputs": [], |
|
|||
1536 | "source": [] |
|
|||
1537 | } |
|
1426 | } | |
1538 | ], |
|
1427 | ], | |
1539 |
"metadata": { |
|
1428 | "metadata": { | |
|
1429 | "kernelspec": { | |||
|
1430 | "display_name": "Python 3", | |||
|
1431 | "name": "python3" | |||
|
1432 | }, | |||
|
1433 | "language_info": { | |||
|
1434 | "codemirror_mode": { | |||
|
1435 | "name": "ipython", | |||
|
1436 | "version": 3 | |||
|
1437 | }, | |||
|
1438 | "file_extension": ".py", | |||
|
1439 | "mimetype": "text/x-python", | |||
|
1440 | "name": "python", | |||
|
1441 | "nbconvert_exporter": "python", | |||
|
1442 | "pygments_lexer": "ipython3", | |||
|
1443 | "version": "3.4.2" | |||
|
1444 | } | |||
|
1445 | }, | |||
1540 | "nbformat": 4, |
|
1446 | "nbformat": 4, | |
1541 | "nbformat_minor": 0 |
|
1447 | "nbformat_minor": 0 | |
1542 | } No newline at end of file |
|
1448 | } |
General Comments 0
You need to be logged in to leave comments.
Login now