File: usage.rst

package info (click to toggle)
nbconvert 5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,360 kB
  • sloc: python: 6,192; makefile: 198
file content (290 lines) | stat: -rw-r--r-- 9,272 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
Using as a command line tool
============================

The command-line syntax to run the ``nbconvert`` script is::

  $ jupyter nbconvert --to FORMAT notebook.ipynb

This will convert the Jupyter notebook file ``notebook.ipynb`` into the output
format given by the ``FORMAT`` string.

Default output format - HTML
----------------------------
The default output format is HTML, for which the ``--to`` argument may be
omitted::

  $ jupyter nbconvert notebook.ipynb

.. _supported_output:

Supported output formats
------------------------
The currently supported output formats are:

    - :ref:`HTML <convert_html>`,
    - :ref:`LaTeX <convert_latex>`,
    - :ref:`PDF <convert_pdf>`,
    - :ref:`Reveal.js HTML slideshow <convert_revealjs>`,
    - :ref:`Markdown <convert_markdown>`,
    - :ref:`reStructuredText <convert_rst>`,
    - :ref:`executable script <convert_script>`,
    - :ref:`notebook <convert_notebook>`.

Jupyter also provides a few templates for output formats. These can be
specified via an additional ``--template`` argument and are listed in the
sections below.

.. _convert_html:

HTML
~~~~
* ``--to html``

  - ``--template full`` (default)

    A full static HTML render of the notebook.
    This looks very similar to the interactive view.

  - ``--template basic``

    Simplified HTML, useful for embedding in webpages, blogs, etc.
    This excludes HTML headers.

.. _convert_latex:

LaTeX
~~~~~
* ``--to latex``

  Latex export.  This generates ``NOTEBOOK_NAME.tex`` file,
  ready for export.

  - ``--template article`` (default)

    Latex article, derived from Sphinx's howto template.

  - ``--template report``

    Latex report, providing a table of contents and chapters.

  - ``--template basic``

    Very basic latex output - mainly meant as a starting point for custom
    templates.

  .. note::

    nbconvert uses pandoc_ to convert between various markup languages,
    so pandoc is a dependency when converting to latex or reStructuredText.

.. _convert_pdf:

PDF
~~~
* ``--to pdf``

  Generates a PDF via latex. Supports the same templates as ``--to latex``.

.. _convert_revealjs:

Reveal.js HTML slideshow
~~~~~~~~~~~~~~~~~~~~~~~~
* ``--to slides``

  This generates a Reveal.js HTML slideshow.
  
Running this slideshow requires a copy of reveal.js (version 3.x).
  
By default, this will include a script tag in the html that will directly load 
reveal.js from a public CDN.

This means that if you include your slides on a webpage, they should work as
expected. However, some features (specifically, speaker notes & timers) will not
work on website because they require access to a local copy of reveal.js.

Speaker notes require a local copy of reveal.js. Then, you need to tell
``nbconvert`` how to find that local copy.

Timers only work if you already have speaker notes, but also require a local
https server. You can read more about this in ServePostProcessorExample_. 

To make this clearer, let's look at an example of how to get speaker notes
working with a local copy of reveal.js: SlidesWithNotesExample_. 

.. note:: 

  In order to designate a mapping from notebook cells to Reveal.js slides, 
  from within the Jupyter notebook, select menu item 
  View --> Cell Toolbar --> Slideshow. That will reveal a drop-down menu 
  on the upper-right of each cell.  From it, one may choose from 
  "Slide," "Sub-Slide", "Fragment", "Skip", and "Notes."  On conversion, 
  cells designated as "skip" will not be included, "notes" will be included 
  only in presenter notes, etc.

.. _SlidesWithNotesExample:

Example: creating slides w/ speaker notes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Let's suppose you have a notebook ``your_talk.ipynb`` that you want to convert
to slides. For this example, we'll assume that you are working in the same
directory as the notebook you want to convert (i.e., when you run ``ls .``,
``your_talk.ipynb`` shows up amongst the list of files). 

First, we need a copy of reveal.js in the same directory as your slides. One
way to do this is to use the following commands in your terminal:

.. code-block:: shell

  git clone https://github.com/hakimel/reveal.js.git
  cd reveal.js
  git checkout 3.5.0
  cd ..

Then we need to tell nbconvert to point to this local copy. To do that we use
the ``--reveal-prefix`` command line flag to point to the local copy.

.. code-block:: shell 

  jupyter nbconvert your_talk.ipynb --to slides --reveal-prefix reveal.js

This will create file ``your_talk.slides.html``, which you should be able to 
access with ``open your_talk.slides.html``. To access the speaker notes, press 
``s`` after the slides load and they should open in a new window.

Note: This does not enable slides that run completely offline. While you have a
local copy of reveal.js, by default, the slides need to access mathjax, require,
and jquery via a public CDN. Addressing this use case is an open issue and `PRs
<https://github.com/jupyter/nbconvert/pulls>`_ are always encouraged.

.. _ServePostProcessorExample:

Serving slides with an https server: ``--post serve``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Once you have speaker notes working you may notice that your timers don't work.
Timers require a bit more infrastructure; you need to serve your local copy of
reveal.js from a local https server.

Fortunately, ``nbconvert`` makes this fairly straightforward through the use of
the ``ServePostProcessor``. To activate this server, we append the command line
flag ``--post serve`` to our call to nbconvert. 

.. code-block:: shell 

  jupyter nbconvert your_talk.ipynb --to slides --reveal-prefix reveal.js --post serve

This will run the server, which will occupy the terminal that you ran the
command in until you stop it. You can stop the server by pressing ``ctrl C``
twice.

.. _convert_markdown:

Markdown
~~~~~~~~
* ``--to markdown``

  Simple markdown output.  Markdown cells are unaffected,
  and code cells indented 4 spaces.

.. _convert_rst:

reStructuredText
~~~~~~~~~~~~~~~~
* ``--to rst``

  Basic reStructuredText output. Useful as a starting point for embedding
  notebooks in Sphinx docs.

  .. note::

    nbconvert uses pandoc_ to convert between various markup languages,
    so pandoc is a dependency when converting to latex or reStructuredText.

.. _convert_script:

Executable script
~~~~~~~~~~~~~~~~~
* ``--to script``

  Convert a notebook to an executable script.
  This is the simplest way to get a Python (or other language, depending on
  the kernel) script out of a notebook. If there were any magics in an
  Jupyter notebook, this may only be executable from a Jupyter session.

  For example, to convert a Julia notebook to a Julia executable script::

      jupyter nbconvert --to script my_julia_notebook.ipynb

.. _convert_notebook:

Notebook and preprocessors
~~~~~~~~~~~~~~~~~~~~~~~~~~
* ``--to notebook``

  .. versionadded:: 3.0

  This doesn't convert a notebook to a different format *per se*,
  instead it allows the running of nbconvert preprocessors on a notebook,
  and/or conversion to other notebook formats. For example::

      jupyter nbconvert --to notebook --execute mynotebook.ipynb

This will open the notebook, execute it, capture new output, and save the
result in :file:`mynotebook.nbconvert.ipynb`. Specifying ``--inplace`` will
overwrite the input file instead of writing a new file. By default,
``nbconvert`` will abort conversion if any exceptions occur during
execution of a cell. If you specify ``--allow-errors`` (in addition to the
`--execute`` flag) then conversion will continue and the output from any
exception will be included in the cell output.

The following command::

      jupyter nbconvert --to notebook --nbformat 3 mynotebook

will create a copy of :file:`mynotebook.ipynb` in :file:`mynotebook.v3.ipynb`
in version 3 of the notebook format.

If you want to convert a notebook in-place, you can specify the output file
to be the same as the input file::

      jupyter nbconvert --to notebook mynb --output mynb

Be careful with that, since it will replace the input file.

.. note::

  nbconvert uses pandoc_ to convert between various markup languages,
  so pandoc is a dependency when converting to latex or reStructuredText.

.. _pandoc: http://pandoc.org/

The output file created by ``nbconvert`` will have the same base name as
the notebook and will be placed in the current working directory. Any
supporting files (graphics, etc) will be placed in a new directory with the
same base name as the notebook, suffixed with ``_files``::

  $ jupyter nbconvert notebook.ipynb
  $ ls
  notebook.ipynb   notebook.html    notebook_files/

For simple single-file output, such as html, markdown, etc.,
the output may be sent to standard output with::

  $ jupyter nbconvert --to markdown notebook.ipynb --stdout

Converting multiple notebooks
-----------------------------
Multiple notebooks can be specified from the command line::

  $ jupyter nbconvert notebook*.ipynb
  $ jupyter nbconvert notebook1.ipynb notebook2.ipynb

or via a list in a configuration file, say ``mycfg.py``, containing the text::

  c = get_config()
  c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"]

and using the command::

  $ jupyter nbconvert --config mycfg.py