Notebook format conversions with nbconvert
§
During the installation of jupyter_format
(see Installation), so-called “entry points” for nbconvert
are configured automatically.
You can convert .ipynb
notebooks to .jupyter
notebooks with
python3 -m nbconvert --to jupyter my-old-notebook.ipynb
To convert a .jupyter
notebook to any format supported by nbconvert
, just append -from-jupyter
to the desired format.
For example, you can convert a .jupyter
notebook to the traditional .ipynb
format:
python3 -m nbconvert --to ipynb-from-jupyter my-new-notebook.jupyter
Or you can convert a .jupyter
file to an HTML file:
python3 -m nbconvert --to html-from-jupyter my-new-notebook.jupyter
Same for slides-from-jupyter
, latex-from-jupyter
, pdf-from-jupyter
etc.
But enough for the theory, let’s try it with this very notebook, shall we?
[1]:
!python3 -m nbconvert --to ipynb-from-jupyter nbconvert.jupyter --output=my-new-notebook
[NbConvertApp] Converting notebook nbconvert.jupyter to ipynb-from-jupyter
[NbConvertApp] Writing 4326 bytes to my-new-notebook.ipynb
Just to make sure it is actually using Jupyter’s JSON format, let’s peek at the beginning of the file:
[2]:
!head my-new-notebook.ipynb
{
"cells": [
{
"cell_type": "markdown",
"id": "00392e5f",
"metadata": {
"nbsphinx": "hidden"
},
"source": [
"This notebook is part of the `jupyter_format` documentation:\n",
Here’s a link to the new file for your perusal: my-new-notebook.ipynb.
Now let’s convert this back to .jupyter
:
[3]:
!python3 -m nbconvert --to jupyter my-new-notebook.ipynb
[NbConvertApp] Converting notebook my-new-notebook.ipynb to jupyter
[NbConvertApp] ERROR | Notebook JSON is invalid: Additional properties are not allowed ('id' was unexpected)
Failed validating 'additionalProperties' in markdown_cell:
On instance['cells'][0]:
{'cell_type': 'markdown',
'id': '00392e5f',
'metadata': {'nbsphinx': 'hidden'},
'source': 'This notebook is part of the `jupyter_format` documentation:\n'
'htt...'}
[NbConvertApp] Writing 2686 bytes to my-new-notebook.jupyter
Again, we take a peek:
[4]:
!head my-new-notebook.jupyter
nbformat 4
nbformat_minor 2
markdown
This notebook is part of the `jupyter_format` documentation:
https://jupyter-format.readthedocs.io/.
cell_metadata
{
"nbsphinx": "hidden"
}
markdown
And a link for closer inspection: my-new-notebook.jupyter.
Finally, let’s try to convert this .jupyter
file to an HTML page:
[5]:
!python3 -m nbconvert --to html-from-jupyter my-new-notebook.jupyter
[NbConvertApp] Converting notebook my-new-notebook.jupyter to html-from-jupyter
[NbConvertApp] Writing 277891 bytes to my-new-notebook.html
[6]:
from IPython.display import IFrame
IFrame('my-new-notebook.html', width='100%', height=350)
[6]:
And for completeness’ sake, a link: my-new-notebook.html.