##// END OF EJS Templates
update nbconvert-as-library notebook...
Min RK -
Show More
@@ -32,19 +32,10 b''
32 32 "cell_type": "markdown",
33 33 "metadata": {},
34 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 35 "Credit, Jonathan Freder (@jdfreder on github)\n",
45 36 "\n",
46 37 "<center>\n",
47 " ![nbca](files/nbconvert_arch.png)\n",
38 " ![nbca](images/nbconvert_arch.png)\n",
48 39 "</center>"
49 40 ]
50 41 },
@@ -84,7 +75,7 b''
84 75 "source": [
85 76 "import requests\n",
86 77 "response = requests.get('http://jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb')\n",
87 "response.content[0:60]+'...'"
78 "response.text[0:60]+'...'"
88 79 ]
89 80 },
90 81 {
@@ -113,10 +104,9 b''
113 104 {
114 105 "data": {
115 106 "text/plain": [
116 "{u'cell_type': u'heading',\n",
117 " u'level': 1,\n",
118 " u'metadata': {},\n",
119 " u'source': u'XKCD plots in Matplotlib'}"
107 "{'cell_type': 'markdown',\n",
108 " 'metadata': {},\n",
109 " 'source': '# XKCD plots in Matplotlib'}"
120 110 ]
121 111 },
122 112 "execution_count": 2,
@@ -125,9 +115,9 b''
125 115 }
126 116 ],
127 117 "source": [
128 "from IPython.nbformat import current as nbformat\n",
129 "jake_notebook = nbformat.reads_json(response.content)\n",
130 "jake_notebook.worksheets[0].cells[0]"
118 "from IPython import nbformat\n",
119 "jake_notebook = nbformat.reads(response.text, as_version=4)\n",
120 "jake_notebook.cells[0]"
131 121 ]
132 122 },
133 123 {
@@ -155,34 +145,23 b''
155 145 },
156 146 "outputs": [],
157 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 148 "from IPython.config import Config\n",
170 149 "from IPython.nbconvert import HTMLExporter\n",
171 150 "\n",
172 151 "## I use `basic` here to have less boilerplate and headers in the HTML.\n",
173 152 "## we'll see later how to pass config to exporters.\n",
174 "exportHtml = HTMLExporter(config=Config({'HTMLExporter':{'default_template':'basic'}}))"
153 "html_exporter = HTMLExporter(config=Config({'HTMLExporter':{'default_template':'basic'}}))"
175 154 ]
176 155 },
177 156 {
178 157 "cell_type": "code",
179 "execution_count": 5,
158 "execution_count": 4,
180 159 "metadata": {
181 160 "collapsed": false
182 161 },
183 162 "outputs": [],
184 163 "source": [
185 "(body, resources) = exportHtml.from_notebook_node(jake_notebook)"
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 180 "cell_type": "code",
202 "execution_count": 6,
181 "execution_count": 5,
203 182 "metadata": {
204 183 "collapsed": false
205 184 },
@@ -208,22 +187,22 b''
208 187 "name": "stdout",
209 188 "output_type": "stream",
210 189 "text": [
211 "['inlining', 'output_extension', 'metadata']\n",
190 "['raw_mimetypes', 'inlining', 'metadata', 'output_extension']\n",
212 191 "defaultdict(None, {'name': 'Notebook'})\n",
213 "html\n"
192 ".html\n"
214 193 ]
215 194 }
216 195 ],
217 196 "source": [
218 "print resources.keys()\n",
219 "print resources['metadata']\n",
220 "print resources['output_extension']\n",
197 "print([key for key in resources ])\n",
198 "print(resources['metadata'])\n",
199 "print(resources['output_extension'])\n",
221 200 "# print resources['inlining'] # too lng to be shown"
222 201 ]
223 202 },
224 203 {
225 204 "cell_type": "code",
226 "execution_count": 7,
205 "execution_count": 6,
227 206 "metadata": {
228 207 "collapsed": false
229 208 },
@@ -232,20 +211,32 b''
232 211 "name": "stdout",
233 212 "output_type": "stream",
234 213 "text": [
214 "<!DOCTYPE html>\n",
215 "<html>\n",
216 "<head>\n",
235 217 "\n",
236 "<div class=\"text_cell_render border-box-sizing rendered_html\">\n",
237 "<h1 id=\"XKCD-plots-in-Matplotlib\">XKCD plots in Matplotlib<a class=\"anchor-link\" href=\"#XKCD-plots-in-Matplotlib\">&#182;</a></h1>\n",
238 "</div>\n",
218 "<meta charset=\"utf-8\" />\n",
219 "<title>Notebook</title>\n",
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 223 "\n",
240 "<div class=\"text_cell_render border-box-sizing rendered_html\">\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"
224 "<style type=\"text/css\">\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 236 "source": [
246 237 "# Part of the body, here the first Heading\n",
247 238 "start = body.index('<h1 id', )\n",
248 "print body[:400]+'...'"
239 "print(body[:400]+'...')"
249 240 ]
250 241 },
251 242 {
@@ -271,7 +262,7 b''
271 262 },
272 263 {
273 264 "cell_type": "code",
274 "execution_count": 8,
265 "execution_count": 7,
275 266 "metadata": {
276 267 "collapsed": false
277 268 },
@@ -279,14 +270,14 b''
279 270 "source": [
280 271 "from IPython.nbconvert import RSTExporter\n",
281 272 "\n",
282 "rst_export = RSTExporter()\n",
273 "rst_exporter = RSTExporter()\n",
283 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 279 "cell_type": "code",
289 "execution_count": 9,
280 "execution_count": 8,
290 281 "metadata": {
291 282 "collapsed": false
292 283 },
@@ -299,20 +290,23 b''
299 290 "XKCD plots in Matplotlib\n",
300 291 "========================\n",
301 292 "\n",
302 "\n",
303 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 295 "by Jake Vanderplas.\n",
306 296 "\n",
307 " *Update: the matplotlib pull request has been merged! See* `*This\n",
308 "post* <http://jakevdp.github.io/blog/2013/07/10/XKCD-plots-in-matplotlib/>`_\n",
297 ".. raw:: html\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 303 "*for a description of the XKCD functionality now built-in to\n",
310 304 "matplotlib!*\n",
311 305 "\n",
312 306 "One of the problems I've had with typical matplotlib figures is that\n",
313 307 "everything in them is so precise, so perfect. For an example of what I\n",
314 308 "mean, take a look at this figure:\n",
315 "In[1]:\n",
309 "\n",
316 310 ".. code:: python\n",
317 311 "\n",
318 312 " from IPython.display import Image\n",
@@ -320,41 +314,37 b''
320 314 "\n",
321 315 "\n",
322 316 "\n",
323 "\n",
324 "\n",
325 317 ".. image:: output_3_0.png\n",
326 318 "\n",
327 319 "\n",
328 320 "\n",
329 321 "Sometimes when showing schematic plots, this is the type of figure I\n",
330 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 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 330 "want to display. But drawing it by hand is a pain: I'd rather just use\n",
335 331 "matplotlib. The problem is, matplotlib is a bit too precise. Attempting\n",
336 332 "to duplicate this figure in matplotlib leads to something like this:\n",
337 "In[2]:\n",
333 "\n",
338 334 ".. code:: python\n",
339 335 "\n",
340 336 " Image('http://jakevdp.github.com/figures/mpl_version.png')\n",
341 337 "\n",
342 338 "\n",
343 339 "\n",
344 "\n",
345 "\n",
346 ".. image:: output_5_0.png\n",
347 "\n",
348 "\n",
349 "\n",
350 "It just doesn'...\n"
340 ".. image:...\n"
351 341 ]
352 342 }
353 343 ],
354 344 "source": [
355 "print body[:970]+'...'\n",
356 "print '[.....]'\n",
357 "print body[800:1200]+'...'"
345 "print(body[:970]+'...')\n",
346 "print('[.....]')\n",
347 "print(body[800:1200]+'...')"
358 348 ]
359 349 },
360 350 {
@@ -381,7 +371,7 b''
381 371 },
382 372 {
383 373 "cell_type": "code",
384 "execution_count": 10,
374 "execution_count": 9,
385 375 "metadata": {
386 376 "collapsed": false
387 377 },
@@ -389,28 +379,20 b''
389 379 {
390 380 "data": {
391 381 "text/plain": [
392 "[u'output_13_1.text',\n",
393 " u'output_18_0.text',\n",
394 " u'output_3_0.text',\n",
395 " u'output_18_1.png',\n",
396 " u'output_12_0.text',\n",
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']"
382 "['output_5_0.png',\n",
383 " 'output_16_0.png',\n",
384 " 'output_13_1.png',\n",
385 " 'output_18_1.png',\n",
386 " 'output_3_0.png']"
405 387 ]
406 388 },
407 "execution_count": 10,
389 "execution_count": 9,
408 390 "metadata": {},
409 391 "output_type": "execute_result"
410 392 }
411 393 ],
412 394 "source": [
413 "resources['outputs'].keys()"
395 "list(resources['outputs'])"
414 396 ]
415 397 },
416 398 {
@@ -428,7 +410,7 b''
428 410 },
429 411 {
430 412 "cell_type": "code",
431 "execution_count": 11,
413 "execution_count": 10,
432 414 "metadata": {
433 415 "collapsed": false
434 416 },
@@ -934,10 +916,10 b''
934 916 "RU5ErkJggg==\n"
935 917 ],
936 918 "text/plain": [
937 "<IPython.core.display.Image at 0x10d2bbf90>"
919 "<IPython.core.display.Image object>"
938 920 ]
939 921 },
940 "execution_count": 11,
922 "execution_count": 10,
941 923 "metadata": {},
942 924 "output_type": "execute_result"
943 925 }
@@ -987,7 +969,7 b''
987 969 "\n",
988 970 " - Get the notebook data and other required files. (you are responsible for that)\n",
989 971 " - Feed them to the exporter that will\n",
990 " - sequentially feed the data to a number of `Transformers`. Transformer only act on the **structure**\n",
972 " - sequentially feed the data to a number of `Preprocessors`. Preprocessor only act on the **structure**\n",
991 973 " of the notebook, and have access to it all. \n",
992 974 " - feed the notebook through the jinja templating engine\n",
993 975 " - the use templates are configurable.\n",
@@ -1000,20 +982,20 b''
1000 982 "cell_type": "markdown",
1001 983 "metadata": {},
1002 984 "source": [
1003 "Here we'll be interested in the `Transformers`. Each `Transformer` is applied successively and in order on the notebook before going through the conversion process.\n",
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 986 "\n",
1005 "We provide some transformer that do some modification on the notebook structure by default.\n",
1006 "One of them, the `ExtractOutputTransformer` is responsible for crawling notebook,\n",
987 "We provide some preprocessor that do some modification on the notebook structure by default.\n",
988 "One of them, the `ExtractOutputPreprocessor` is responsible for crawling notebook,\n",
1007 989 "finding all the figures, and put them into the resources directory, as well as choosing the key\n",
1008 990 "(`filename_xx_y.extension`) that can replace the figure in the template.\n",
1009 991 "\n",
1010 992 "\n",
1011 "The `ExtractOutputTransformer` is special in the fact that it **should** be availlable on all `Exporter`s, but is just inactive by default on some exporter."
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 997 "cell_type": "code",
1016 "execution_count": 12,
998 "execution_count": 11,
1017 999 "metadata": {
1018 1000 "collapsed": false
1019 1001 },
@@ -1021,23 +1003,25 b''
1021 1003 {
1022 1004 "data": {
1023 1005 "text/plain": [
1024 "[<function IPython.nbconvert.transformers.coalescestreams.wrappedfunc>,\n",
1025 " <IPython.nbconvert.transformers.svg2pdf.SVG2PDFTransformer at 0x10d2a7490>,\n",
1026 " <IPython.nbconvert.transformers.extractoutput.ExtractOutputTransformer at 0x10d2a7ad0>,\n",
1027 " <IPython.nbconvert.transformers.csshtmlheader.CSSHTMLHeaderTransformer at 0x10d2a7b50>,\n",
1028 " <IPython.nbconvert.transformers.revealhelp.RevealHelpTransformer at 0x10d29dd90>,\n",
1029 " <IPython.nbconvert.transformers.latex.LatexTransformer at 0x10d29db50>,\n",
1030 " <IPython.nbconvert.transformers.sphinx.SphinxTransformer at 0x10d2a7b90>]"
1006 "[<function IPython.nbconvert.preprocessors.coalescestreams.cell_preprocessor.<locals>.wrappedfunc>,\n",
1007 " <IPython.nbconvert.preprocessors.svg2pdf.SVG2PDFPreprocessor at 0x107d1a630>,\n",
1008 " <IPython.nbconvert.preprocessors.extractoutput.ExtractOutputPreprocessor at 0x107d1a748>,\n",
1009 " <IPython.nbconvert.preprocessors.csshtmlheader.CSSHTMLHeaderPreprocessor at 0x107d1aba8>,\n",
1010 " <IPython.nbconvert.preprocessors.revealhelp.RevealHelpPreprocessor at 0x107d1a710>,\n",
1011 " <IPython.nbconvert.preprocessors.latex.LatexPreprocessor at 0x107daa860>,\n",
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": 12,
1017 "execution_count": 11,
1034 1018 "metadata": {},
1035 1019 "output_type": "execute_result"
1036 1020 }
1037 1021 ],
1038 1022 "source": [
1039 "# second transformer shoudl be Instance of ExtractFigureTransformer\n",
1040 "exportHtml._transformers # 3rd one shouel be <ExtractOutputTransformer>"
1023 "# 3rd one should be <ExtractOutputPreprocessor>\n",
1024 "html_exporter._preprocessors"
1041 1025 ]
1042 1026 },
1043 1027 {
@@ -1047,14 +1031,14 b''
1047 1031 "To enable it we will use IPython configuration/Traitlets system. If you are have already set some IPython configuration options, \n",
1048 1032 "this will look pretty familiar to you. Configuration option are always of the form:\n",
1049 1033 "\n",
1050 " ClassName.attribute_name = value\n",
1034 " ClassName.attribute_name = value\n",
1051 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 `HtmlExporter`"
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 1040 "cell_type": "code",
1057 "execution_count": 13,
1041 "execution_count": 12,
1058 1042 "metadata": {
1059 1043 "collapsed": false
1060 1044 },
@@ -1064,31 +1048,23 b''
1064 1048 "output_type": "stream",
1065 1049 "text": [
1066 1050 "resources without the \"figures\" key :\n",
1067 "['inlining', 'output_extension', 'metadata']\n",
1051 "['raw_mimetypes', 'inlining', 'metadata', 'output_extension']\n",
1068 1052 "\n",
1069 "Here we have one more field \n",
1070 "['outputs', 'inlining', 'output_extension', 'metadata']\n"
1053 "Here we have one more field\n",
1054 "['outputs', 'raw_mimetypes', 'inlining', 'metadata', 'output_extension']\n"
1071 1055 ]
1072 1056 },
1073 1057 {
1074 1058 "data": {
1075 1059 "text/plain": [
1076 "[u'output_13_1.text',\n",
1077 " u'output_18_0.text',\n",
1078 " u'output_3_0.text',\n",
1079 " u'output_18_1.png',\n",
1080 " u'output_12_0.text',\n",
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']"
1060 "['output_5_0.png',\n",
1061 " 'output_16_0.png',\n",
1062 " 'output_13_1.png',\n",
1063 " 'output_18_1.png',\n",
1064 " 'output_3_0.png']"
1089 1065 ]
1090 1066 },
1091 "execution_count": 13,
1067 "execution_count": 12,
1092 1068 "metadata": {},
1093 1069 "output_type": "execute_result"
1094 1070 }
@@ -1097,22 +1073,22 b''
1097 1073 "from IPython.config import Config\n",
1098 1074 "\n",
1099 1075 "c = Config({\n",
1100 " 'ExtractOutputTransformer':{'enabled':True}\n",
1076 " 'ExtractOutputPreprocessor':{'enabled':True}\n",
1101 1077 " })\n",
1102 1078 "\n",
1103 "exportHtml = HTMLExporter()\n",
1104 "exportHtml_and_figs = HTMLExporter(config=c)\n",
1079 "exportHTML = HTMLExporter()\n",
1080 "exportHTML_and_figs = HTMLExporter(config=c)\n",
1105 1081 "\n",
1106 "(_, resources) = exportHtml.from_notebook_node(jake_notebook)\n",
1107 "(_, resources_with_fig) = exportHtml_and_figs.from_notebook_node(jake_notebook)\n",
1082 "(_, resources) = exportHTML.from_notebook_node(jake_notebook)\n",
1083 "(_, resources_with_fig) = exportHTML_and_figs.from_notebook_node(jake_notebook)\n",
1108 1084 "\n",
1109 "print 'resources without the \"figures\" key :'\n",
1110 "print resources.keys()\n",
1085 "print('resources without the \"figures\" key :')\n",
1086 "print(list(resources))\n",
1111 1087 "\n",
1112 "print ''\n",
1113 "print 'Here we have one more field '\n",
1114 "print resources_with_fig.keys()\n",
1115 "resources_with_fig['outputs'].keys() "
1088 "print('')\n",
1089 "print('Here we have one more field')\n",
1090 "print(list(resources_with_fig))\n",
1091 "list(resources_with_fig['outputs'])"
1116 1092 ]
1117 1093 },
1118 1094 {
@@ -1126,27 +1102,27 b''
1126 1102 "cell_type": "markdown",
1127 1103 "metadata": {},
1128 1104 "source": [
1129 "#### Custom transformer"
1105 "#### Custom Preprocessor"
1130 1106 ]
1131 1107 },
1132 1108 {
1133 1109 "cell_type": "markdown",
1134 1110 "metadata": {},
1135 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 transformers that will be applied to the notebook after the default ones.\n",
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 1113 "\n",
1138 "To do so you'll have to pass an ordered list of `Transformer`s to the Exporter constructor. \n",
1114 "To do so you'll have to pass an ordered list of `Preprocessor`s to the Exporter constructor. \n",
1139 1115 "\n",
1140 "But what is an transformer ? Transformer can be either *decorated function* for dead-simple `Transformer`s that apply\n",
1116 "But what is an preprocessor ? Preprocessor can be either *decorated function* for dead-simple `Preprocessor`s that apply\n",
1141 1117 "independently to each cell, for more advance transformation that support configurability You have to inherit from\n",
1142 "`Transformer` and define a `call` method as we'll see below.\n",
1118 "`Preprocessor` and define a `call` method as we'll see below.\n",
1143 1119 "\n",
1144 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 1124 "cell_type": "code",
1149 "execution_count": 14,
1125 "execution_count": 13,
1150 1126 "metadata": {
1151 1127 "collapsed": false
1152 1128 },
@@ -1157,27 +1133,29 b''
1157 1133 "text": [
1158 1134 "Four relevant docstring\n",
1159 1135 "=============================\n",
1160 " A configurable transformer\n",
1136 " A configurable preprocessor\n",
1161 1137 "\n",
1162 1138 " Inherit from this class if you wish to have configurability for your\n",
1163 " transformer.\n",
1139 " preprocessor.\n",
1164 1140 "\n",
1165 " Any configurable traitlets this class exposed will be configurable in profiles\n",
1166 " using c.SubClassName.atribute=value\n",
1141 " Any configurable traitlets this class exposed will be configurable in\n",
1142 " profiles using c.SubClassName.attribute = value\n",
1167 1143 "\n",
1168 " you can overwrite transform_cell to apply a transformation independently on each cell\n",
1169 " or __call__ if you prefer your own logic. See corresponding docstring for informations.\n",
1144 " you can overwrite :meth:`preprocess_cell` to apply a transformation\n",
1145 " independently on each cell or :meth:`preprocess` if you prefer your own\n",
1146 " logic. See corresponding docstring for informations.\n",
1170 1147 "\n",
1171 1148 " Disabled by default and can be enabled via the config by\n",
1172 " 'c.YourTransformerName.enabled = True'\n",
1149 " 'c.YourPreprocessorName.enabled = True'\n",
1173 1150 " \n",
1174 1151 "=============================\n",
1175 1152 "\n",
1176 " Transformation to apply on each notebook.\n",
1153 " Preprocessing to apply on each notebook.\n",
1177 1154 " \n",
1178 " You should return modified nb, resources.\n",
1179 " If you wish to apply your transform on each cell, you might want to \n",
1180 " overwrite transform_cell method instead.\n",
1155 " Must return modified nb, resources.\n",
1156 " \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 1159 " \n",
1182 1160 " Parameters\n",
1183 1161 " ----------\n",
@@ -1185,12 +1163,12 b''
1185 1163 " Notebook being converted\n",
1186 1164 " resources : dictionary\n",
1187 1165 " Additional resources used in the conversion process. Allows\n",
1188 " transformers to pass variables into the Jinja engine.\n",
1166 " preprocessors to pass variables into the Jinja engine.\n",
1189 1167 " \n",
1190 1168 "=============================\n",
1191 1169 "\n",
1192 " Overwrite if you want to apply a transformation on each cell. You \n",
1193 " should return modified cell and resource dictionary.\n",
1170 " Override if you want to apply some preprocessing to each cell.\n",
1171 " Must return modified cell and resource dictionary.\n",
1194 1172 " \n",
1195 1173 " Parameters\n",
1196 1174 " ----------\n",
@@ -1198,7 +1176,7 b''
1198 1176 " Notebook cell being processed\n",
1199 1177 " resources : dictionary\n",
1200 1178 " Additional resources used in the conversion process. Allows\n",
1201 " transformers to pass variables into the Jinja engine.\n",
1179 " preprocessors to pass variables into the Jinja engine.\n",
1202 1180 " index : int\n",
1203 1181 " Index of the cell being processed\n",
1204 1182 " \n",
@@ -1207,16 +1185,16 b''
1207 1185 }
1208 1186 ],
1209 1187 "source": [
1210 "from IPython.nbconvert.transformers import Transformer\n",
1188 "from IPython.nbconvert.preprocessors import Preprocessor\n",
1211 1189 "import IPython.config\n",
1212 "print \"Four relevant docstring\"\n",
1213 "print '============================='\n",
1214 "print Transformer.__doc__\n",
1215 "print '============================='\n",
1216 "print Transformer.call.__doc__\n",
1217 "print '============================='\n",
1218 "print Transformer.transform_cell.__doc__\n",
1219 "print '============================='"
1190 "print(\"Four relevant docstring\")\n",
1191 "print('=============================')\n",
1192 "print(Preprocessor.__doc__)\n",
1193 "print('=============================')\n",
1194 "print(Preprocessor.preprocess.__doc__)\n",
1195 "print('=============================')\n",
1196 "print(Preprocessor.preprocess_cell.__doc__)\n",
1197 "print('=============================')"
1220 1198 ]
1221 1199 },
1222 1200 {
@@ -1224,7 +1202,7 b''
1224 1202 "metadata": {},
1225 1203 "source": [
1226 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 functionnality, which is still on it's way)\n",
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 1217 "cell_type": "markdown",
1240 1218 "metadata": {},
1241 1219 "source": [
1242 "I'll now demonstrate a specific example [requested](https://github.com/ipython/nbconvert/pull/137#issuecomment-18658235) while nbconvert 2 was beeing developped. The ability to exclude cell from the conversion process based on their index. \n",
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 1221 "\n",
1244 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 1226 "cell_type": "code",
1249 "execution_count": 15,
1227 "execution_count": 14,
1250 1228 "metadata": {
1251 1229 "collapsed": false
1252 1230 },
@@ -1257,71 +1235,66 b''
1257 1235 },
1258 1236 {
1259 1237 "cell_type": "code",
1260 "execution_count": 16,
1238 "execution_count": 15,
1261 1239 "metadata": {
1262 1240 "collapsed": false
1263 1241 },
1264 1242 "outputs": [],
1265 1243 "source": [
1266 "class PelicanSubCell(Transformer):\n",
1267 " \"\"\"A Pelican specific transformer to remove somme of the cells of a notebook\"\"\"\n",
1244 "class PelicanSubCell(Preprocessor):\n",
1245 " \"\"\"A Pelican specific preprocessor to remove somme of the cells of a notebook\"\"\"\n",
1268 1246 " \n",
1269 1247 " # I could also read the cells from nbc.metadata.pelican is someone wrote a JS extension\n",
1270 1248 " # But I'll stay with configurable value. \n",
1271 1249 " start = Integer(0, config=True, help=\"first cell of notebook to be converted\")\n",
1272 1250 " end = Integer(-1, config=True, help=\"last cell of notebook to be converted\")\n",
1273 1251 " \n",
1274 " def call(self, nb, resources):\n",
1252 " def preprocess(self, nb, resources):\n",
1275 1253 "\n",
1276 1254 " #nbc = deepcopy(nb)\n",
1277 1255 " nbc = nb\n",
1278 " # don't print in real transformer !!!\n",
1279 " print \"I'll keep only cells from \", self.start, \"to \", self.end, \"\\n\\n\"\n",
1280 " for worksheet in nbc.worksheets :\n",
1281 " cells = worksheet.cells[:]\n",
1282 " worksheet.cells = cells[self.start:self.end] \n",
1256 " # don't print in real preprocessor !!!\n",
1257 " print(\"I'll keep only cells from \", self.start, \"to \", self.end, \"\\n\\n\")\n",
1258 " nbc.cells = nb.cells[self.start:self.end] \n",
1283 1259 " return nbc, resources"
1284 1260 ]
1285 1261 },
1286 1262 {
1287 1263 "cell_type": "code",
1288 "execution_count": 17,
1264 "execution_count": 16,
1289 1265 "metadata": {
1290 1266 "collapsed": false
1291 1267 },
1292 1268 "outputs": [],
1293 1269 "source": [
1294 1270 "# I create this on the fly, but this could be loaded from a DB, and config object support merging...\n",
1295 "c = Config({\n",
1296 " 'PelicanSubCell':{\n",
1297 " 'enabled':True,\n",
1298 " 'start':4,\n",
1299 " 'end':6,\n",
1300 " }\n",
1301 " })"
1271 "c = Config()\n",
1272 "c.PelicanSubCell.enabled = True\n",
1273 "c.PelicanSubCell.start = 4\n",
1274 "c.PelicanSubCell.end = 6"
1302 1275 ]
1303 1276 },
1304 1277 {
1305 1278 "cell_type": "markdown",
1306 1279 "metadata": {},
1307 1280 "source": [
1308 "I'm creating a pelican exporter that take `PelicanSubCell` extra transformers and a `config` object as parameter. This might seem redundant, but with configuration system you'll see that one can register an inactive transformer on all exporters and activate it at will form its config files and command line. "
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 1285 "cell_type": "code",
1313 "execution_count": 18,
1286 "execution_count": 17,
1314 1287 "metadata": {
1315 1288 "collapsed": false
1316 1289 },
1317 1290 "outputs": [],
1318 1291 "source": [
1319 "pelican = RSTExporter(transformers=[PelicanSubCell], config=c)"
1292 "pelican = RSTExporter(preprocessors=[PelicanSubCell], config=c)"
1320 1293 ]
1321 1294 },
1322 1295 {
1323 1296 "cell_type": "code",
1324 "execution_count": 19,
1297 "execution_count": 18,
1325 1298 "metadata": {
1326 1299 "collapsed": false
1327 1300 },
@@ -1338,15 +1311,13 b''
1338 1311 "want to display. But drawing it by hand is a pain: I'd rather just use\n",
1339 1312 "matplotlib. The problem is, matplotlib is a bit too precise. Attempting\n",
1340 1313 "to duplicate this figure in matplotlib leads to something like this:\n",
1341 "In[2]:\n",
1314 "\n",
1342 1315 ".. code:: python\n",
1343 1316 "\n",
1344 1317 " Image('http://jakevdp.github.com/figures/mpl_version.png')\n",
1345 1318 "\n",
1346 1319 "\n",
1347 1320 "\n",
1348 "\n",
1349 "\n",
1350 1321 ".. image:: output_5_0.png\n",
1351 1322 "\n",
1352 1323 "\n",
@@ -1355,80 +1326,7 b''
1355 1326 }
1356 1327 ],
1357 1328 "source": [
1358 "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]"
1329 "print(pelican.from_notebook_node(jake_notebook)[0])"
1432 1330 ]
1433 1331 },
1434 1332 {
@@ -1440,7 +1338,7 b''
1440 1338 },
1441 1339 {
1442 1340 "cell_type": "code",
1443 "execution_count": 23,
1341 "execution_count": 19,
1444 1342 "metadata": {
1445 1343 "collapsed": false
1446 1344 },
@@ -1449,7 +1347,7 b''
1449 1347 "name": "stdout",
1450 1348 "output_type": "stream",
1451 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 1351 "</div>\n",
1454 1352 "FOOOOOOOOTEEEEER\n",
1455 1353 "\n"
@@ -1459,9 +1357,9 b''
1459 1357 "source": [
1460 1358 "from jinja2 import DictLoader\n",
1461 1359 "\n",
1462 "dl = DictLoader({'html_full.tpl': \n",
1360 "dl = DictLoader({'full.tpl': \n",
1463 1361 "\"\"\"\n",
1464 "{%- extends 'html_basic.tpl' -%} \n",
1362 "{%- extends 'basic.tpl' -%} \n",
1465 1363 "\n",
1466 1364 "{% block footer %}\n",
1467 1365 "FOOOOOOOOTEEEEER\n",
@@ -1469,10 +1367,10 b''
1469 1367 "\"\"\"})\n",
1470 1368 "\n",
1471 1369 "\n",
1472 "exportHtml = HTMLExporter( config=None , filters={'highlight': my_highlight}, extra_loaders=[dl] )\n",
1473 "(body,resources) = exportHtml.from_notebook_node(jake_notebook)\n",
1370 "exportHTML = HTMLExporter(extra_loaders=[dl])\n",
1371 "(body,resources) = exportHTML.from_notebook_node(jake_notebook)\n",
1474 1372 "for l in body.split('\\n')[-4:]:\n",
1475 " print l"
1373 " print(l)"
1476 1374 ]
1477 1375 },
1478 1376 {
@@ -1501,7 +1399,7 b''
1501 1399 "metadata": {},
1502 1400 "source": [
1503 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>&mdash; Dami\u00e1n Avila (@damian_avila) <a href=\"https://twitter.com/damian_avila/statuses/370306057828335616\">August 21, 2013</a></blockquote>\n",
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>&mdash; Damián Avila (@damian_avila) <a href=\"https://twitter.com/damian_avila/statuses/370306057828335616\">August 21, 2013</a></blockquote>\n",
1505 1403 "</center>"
1506 1404 ]
1507 1405 },
@@ -1525,18 +1423,26 b''
1525 1423 "source": [
1526 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 1446 "nbformat": 4,
1541 1447 "nbformat_minor": 0
1542 1448 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now