##// END OF EJS Templates
Merge pull request #8190 from minrk/sticky-tooltip-description...
Merge pull request #8190 from minrk/sticky-tooltip-description update description of sticky tooltip

File last commit:

r20540:0e53a88c
r21005:12f54add merge
Show More
Converting Notebooks With nbconvert.ipynb
317 lines | 8.1 KiB | text/plain | TextLexer
/ examples / Notebook / Converting Notebooks With nbconvert.ipynb
Brian E. Granger
Moving things around.
r17497 {
Min RK
upate exmaple notebooks to nbformat v4
r18669 "cells": [
Brian E. Granger
Moving things around.
r17497 {
Min RK
upate exmaple notebooks to nbformat v4
r18669 "cell_type": "markdown",
"metadata": {},
"source": [
"# NbConvert"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Command line usage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "`NbConvert` is both a library and command line tool that allows you to convert notebooks to other formats. It ships with many common formats: `html`, `latex`, `markdown`, `python`, `rst`, and `slides`\n",
"NbConvert relys on the Jinja templating engine, so implementing a new format or tweeking an existing one is easy."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "You can invoke nbconvert by running\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
"```bash\n",
"$ ipython nbconvert <options and arguments>\n",
"```\n",
"\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Call `ipython nbconvert` with the `--help` flag or without any aruments to display the basic help. For detailed configuration help, use the `--help-all` flag."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Basic export"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "As a test, the `Index.ipynb` notebook in the directory will be convert. \n",
"\n",
Jonathan Frederic
@Carreau 's review comments.
r20539 "If you're converting a notebook with code in it, make sure to run the code cells that you're interested in before attempting to convert the notebook. Unless explicitly requested, nbconvert **does not execute the code cells** of the notebooks that it converts."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
"%%bash\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "ipython nbconvert 'Index.ipynb'"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Html is the (configurable) default value. The verbose form of the same command as above is "
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
"%%bash\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "ipython nbconvert --to=html 'Index.ipynb'"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "You can also convert to latex, which will extract the embeded images. If the embeded images are SVGs, inkscape is used to convert them to pdf:"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
"%%bash\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "ipython nbconvert --to=latex 'Index.ipynb'"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Note that the latex conversion creates latex, not a PDF. To create a PDF you need the required third party packages to compile the latex.\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "A `--post` flag is provided for convinience which allows you to have nbconvert automatically compile a PDF for you from your output."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
"%%bash\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "ipython nbconvert --to=latex 'Index.ipynb' --post=pdf"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "## Custom templates"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Look at the first 20 lines of the `python` exporter"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "pyfile = !ipython nbconvert --to python 'Index.ipynb' --stdout\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "for l in pyfile[20:40]:\n",
" print l"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "From the code, you can see that non-code cells are also exported. If you want to change this behavior, you can use a custom template. The custom template inherits from the Python template and overwrites the markdown blocks so that they are empty."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
"%%writefile simplepython.tpl\n",
"{% extends 'python.tpl'%}\n",
"\n",
"{% block markdowncell -%}\n",
"{% endblock markdowncell %}\n",
"\n",
"## we also want to get rig of header cell\n",
"{% block headingcell -%}\n",
"{% endblock headingcell %}\n",
"\n",
"## and let's change the appearance of input prompt\n",
"{% block in_prompt %}\n",
"# This was input cell with prompt number : {{ cell.prompt_number if cell.prompt_number else ' ' }}\n",
"{%- endblock in_prompt %}"
]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "pyfile = !ipython nbconvert --to python 'Index.ipynb' --stdout --template=simplepython.tpl\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
"for l in pyfile[4:40]:\n",
" print l\n",
"print '...'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
@Carreau 's review comments.
r20539 "For details about the template syntax, refer to [Jinja's manual](http://jinja2.readthedocs.org/en/latest/intro.html)."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Template that use cells metadata"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "The notebook file format supports attaching arbitrary JSON metadata to each cell. Here, as an exercise, you will use the metadata to tags cells."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
More cleaner is more better
r20540 "First you need to choose another notebook you want to convert to html, and tag some of the cells with metadata. You can refere to the file `soln/celldiff.js` as an example or follow the Javascript tutorial to figure out how do change cell metadata. Assuming you have a notebook with some of the cells tagged as `Easy`|`Medium`|`Hard`|`<None>`, the notebook can be converted specially using a custom template. Design your template in the cells provided below.\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "\n",
"The following, unorganized lines of code, may be of help:"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"{% extends 'html_full.tpl'%}\n",
"{% block any_cell %}\n",
"{{ super() }}\n",
"<div style=\"background-color:red\">\n",
"<div style='background-color:orange'>\n",
"```\n",
"\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "If your key name under `cell.metadata.example.difficulty`, the following code would get the value of it:\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
"`cell['metadata'].get('example',{}).get('difficulty','')`\n",
"\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Tip: Use `%%writefile` to edit the template in the notebook."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%bash\n",
"# ipython nbconvert --to html <your chosen notebook.ipynb> --template=<your template file>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%loadpy soln/coloreddiff.tpl"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# ipython nbconvert --to html '04 - Custom Display Logic.ipynb' --template=soln/coloreddiff.tpl"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Get rid of all command line flags."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
@Carreau 's review comments.
r20539 "IPython nbconvert can be configured using the default profile or a profile specified via the `--profile` flag. Additionally, if a `config.py` file exist in current working directory, nbconvert will use that as config."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
Brian E. Granger
Moving things around.
r17497 }
Min RK
upate exmaple notebooks to nbformat v4
r18669 ],
Min RK
add kernel metadata to example notebooks
r20278 "metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "version": "3.4.3"
Min RK
add kernel metadata to example notebooks
r20278 }
},
Min RK
upate exmaple notebooks to nbformat v4
r18669 "nbformat": 4,
"nbformat_minor": 0
Min RK
add kernel metadata to example notebooks
r20278 }