2.3. Building

This section we will explain various options to build your projects. This options can be grouped into four categories:

  1. Sanity check

    • d2lbook build linkcheck will check if all internal and external links are accessible.

    • d2lbook build outputcheck will check if no notebook will contain code outputs

  2. Building results

    • d2lbook build html: build the HTML version into _build/html

    • d2lbook build pdf: build the PDF version into _build/pdf

    • d2lbook build pkg: build a zip file contains all .ipynb notebooks

  3. Additional features

    • d2lbook build colab: convert all notebooks can be run on Google Colab into _build/colab. See more in Section 2.8

    • d2lbook build lib: build a Python package so we can reuse codes in other notebooks. See more in XXX.

  4. Internal stages, which often are triggered automatically.

    • d2lbook build eval: evaluate all notebooks and save them as .ipynb notebooks into _build/eval

    • d2lbook build rst: convert all notebooks into rst files and create a Sphinx project in _build/rst

2.3.1. Building Cache

We encourage you to evaluate your notebooks to obtain code cell results, instead of keeping these results in the source files for two reasons: 1. These results make code review difficult, especially when they have randomness either due to numerical precision or random number generators. 1. A notebook hasn’t evaluated for a while may be broken due to package upgrading.

But the evaluation costs additional overhead during building. We recommend to limit the runtime for each notebook within a few minutes. And d2lbook will reuse the previous built and only evaluate the modified notebooks.

For example, the average runtime of a notebook (section) in Dive into Deep Learning is about 2 minutes on a GPU machine, due to training neural networks. It contains more than 100 notebooks, which make the total runtime cost 2-3 hours. In reality, each code change will only modify a few notebooks and therefore the build time is often less than 10 minutes.

Let’s see how it works. First create a project as we did in Section 2.1.

!mkdir -p cache
%%writefile cache/index.md
# My Book

The starting page of my book with `d2lbook`.

````toc
get_started
````
Writing cache/index.md
%%writefile cache/get_started.md
# Getting Started

Please first install my favorite package `numpy`.
Writing cache/get_started.md
!cd cache; d2lbook build html
[d2lbook:build.py:L130] INFO   2 notebooks are outdated
[d2lbook:build.py:L132] INFO   [1] ./get_started.md
[d2lbook:build.py:L132] INFO   [2] ./index.md
[d2lbook:build.py:L137] INFO   [1/2, 00:00:00] Evaluating ./get_started.md, save as _build/eval/get_started.ipynb
[d2lbook:execute.py:L404] INFO   Executing notebook with kernel: python
/var/lib/jenkins/miniconda3/envs/d2l-book-build/lib/python3.8/site-packages/jupyter_client/manager.py:357: FutureWarning: Method cleanup(connection_file=True) is deprecated, use cleanup_resources(restart=False).
  warnings.warn("Method cleanup(connection_file=True) is deprecated, use cleanup_resources(restart=False).",
[d2lbook:build.py:L145] INFO   Finished in 00:00:01
[d2lbook:build.py:L137] INFO   [2/2, 00:00:01] Evaluating ./index.md, save as _build/eval/index.ipynb
[d2lbook:execute.py:L404] INFO   Executing notebook with kernel: python
[d2lbook:build.py:L145] INFO   Finished in 00:00:01
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build eval" in 00:00:02
[d2lbook:build.py:L247] INFO   2 rst files are outdated
[d2lbook:build.py:L249] INFO   Convert _build/eval/get_started.ipynb to _build/rst/get_started.rst
[d2lbook:build.py:L249] INFO   Convert _build/eval/index.ipynb to _build/rst/index.rst
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build rst" in 00:00:02
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build ipynb" in 00:00:00
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build colab" in 00:00:00
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build sagemaker" in 00:00:00
Running Sphinx v3.1.1
making output directory... done
/var/lib/jenkins/workspace/d2l-book/docs/_build/eval/user/cache/_build/rst/conf.py:104: RemovedInSphinx40Warning: The app.add_javascript() is deprecated. Please use app.add_js_file() instead.
  app.add_javascript('d2l.js')
/var/lib/jenkins/workspace/d2l-book/docs/_build/eval/user/cache/_build/rst/conf.py:105: RemovedInSphinx40Warning: The app.add_stylesheet() is deprecated. Please use app.add_css_file() instead.
  app.add_stylesheet('d2l.css')
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 2 source files that are out of date
updating environment: [new config] 2 added, 0 changed, 0 removed

looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done

waiting for workers...
generating indices...  genindexdone
writing additional pages...  searchdone
copying static files... ... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded.

The HTML pages are in _build/html.
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build html" in 00:00:03

You can see index.md is evaluated. (Though it doesn’t contain codes, it’s fine to evaluate it as a Jupyter notebook.)

If building again, we will see no notebook will be evaluated.

!cd cache; d2lbook build html
[d2lbook:build.py:L130] INFO   0 notebooks are outdated
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build eval" in 00:00:00
[d2lbook:build.py:L247] INFO   0 rst files are outdated
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build rst" in 00:00:00
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build ipynb" in 00:00:00
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build colab" in 00:00:00
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build sagemaker" in 00:00:00
Running Sphinx v3.1.1
/var/lib/jenkins/workspace/d2l-book/docs/_build/eval/user/cache/_build/rst/conf.py:104: RemovedInSphinx40Warning: The app.add_javascript() is deprecated. Please use app.add_js_file() instead.
  app.add_javascript('d2l.js')
/var/lib/jenkins/workspace/d2l-book/docs/_build/eval/user/cache/_build/rst/conf.py:105: RemovedInSphinx40Warning: The app.add_stylesheet() is deprecated. Please use app.add_css_file() instead.
  app.add_stylesheet('d2l.css')
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 0 source files that are out of date
updating environment: 0 added, 0 changed, 0 removed
looking for now-outdated files... none found
no targets are out of date.
build succeeded.

The HTML pages are in _build/html.
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build html" in 00:00:00

Now let’s modify get_started.md, you will see it will be re-evaluated, but not index.md.

%%writefile cache/get_started.md
# Getting Started

Please first install my favorite package `numpy>=1.18`.
Overwriting cache/get_started.md
!cd cache; d2lbook build html
[d2lbook:build.py:L130] INFO   1 notebooks are outdated
[d2lbook:build.py:L132] INFO   [1] ./get_started.md
[d2lbook:build.py:L137] INFO   [1/1, 00:00:00] Evaluating ./get_started.md, save as _build/eval/get_started.ipynb
[d2lbook:execute.py:L404] INFO   Executing notebook with kernel: python
/var/lib/jenkins/miniconda3/envs/d2l-book-build/lib/python3.8/site-packages/jupyter_client/manager.py:357: FutureWarning: Method cleanup(connection_file=True) is deprecated, use cleanup_resources(restart=False).
  warnings.warn("Method cleanup(connection_file=True) is deprecated, use cleanup_resources(restart=False).",
[d2lbook:build.py:L145] INFO   Finished in 00:00:01
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build eval" in 00:00:01
[d2lbook:build.py:L247] INFO   1 rst files are outdated
[d2lbook:build.py:L249] INFO   Convert _build/eval/get_started.ipynb to _build/rst/get_started.rst
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build rst" in 00:00:01
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build ipynb" in 00:00:00
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build colab" in 00:00:00
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build sagemaker" in 00:00:00
Running Sphinx v3.1.1
/var/lib/jenkins/workspace/d2l-book/docs/_build/eval/user/cache/_build/rst/conf.py:104: RemovedInSphinx40Warning: The app.add_javascript() is deprecated. Please use app.add_js_file() instead.
  app.add_javascript('d2l.js')
/var/lib/jenkins/workspace/d2l-book/docs/_build/eval/user/cache/_build/rst/conf.py:105: RemovedInSphinx40Warning: The app.add_stylesheet() is deprecated. Please use app.add_css_file() instead.
  app.add_stylesheet('d2l.css')
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: 0 added, 1 changed, 0 removed

looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done

waiting for workers...
generating indices...  genindexdone
writing additional pages...  searchdone
copying static files... ... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded.

The HTML pages are in _build/html.
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build html" in 00:00:01

One way to trigger the whole built is removing the saved notebooks in _build/eval, or simply deleting _build. Another way is specifying some dependencies. For example, in the following cell we add config.ini into the dependencies. Every time config.ini is modified, it will invalid the cache of all notebooks and trigger a build from scratch.

%%writefile cache/config.ini

[build]
dependencies = config.ini
Writing cache/config.ini
!cd cache; d2lbook build html
[d2lbook:config.py:L12] INFO   Load configure from config.ini
[d2lbook:build.py:L130] INFO   2 notebooks are outdated
[d2lbook:build.py:L132] INFO   [1] ./get_started.md
[d2lbook:build.py:L132] INFO   [2] ./index.md
[d2lbook:build.py:L137] INFO   [1/2, 00:00:00] Evaluating ./get_started.md, save as _build/eval/get_started.ipynb
[d2lbook:execute.py:L404] INFO   Executing notebook with kernel: python
/var/lib/jenkins/miniconda3/envs/d2l-book-build/lib/python3.8/site-packages/jupyter_client/manager.py:357: FutureWarning: Method cleanup(connection_file=True) is deprecated, use cleanup_resources(restart=False).
  warnings.warn("Method cleanup(connection_file=True) is deprecated, use cleanup_resources(restart=False).",
[d2lbook:build.py:L145] INFO   Finished in 00:00:01
[d2lbook:build.py:L137] INFO   [2/2, 00:00:01] Evaluating ./index.md, save as _build/eval/index.ipynb
[d2lbook:execute.py:L404] INFO   Executing notebook with kernel: python
[d2lbook:build.py:L145] INFO   Finished in 00:00:00
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build eval" in 00:00:02
[d2lbook:build.py:L247] INFO   2 rst files are outdated
[d2lbook:build.py:L249] INFO   Convert _build/eval/get_started.ipynb to _build/rst/get_started.rst
[d2lbook:build.py:L249] INFO   Convert _build/eval/index.ipynb to _build/rst/index.rst
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build rst" in 00:00:02
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build ipynb" in 00:00:00
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build colab" in 00:00:00
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build sagemaker" in 00:00:00
Running Sphinx v3.1.1
/var/lib/jenkins/workspace/d2l-book/docs/_build/eval/user/cache/_build/rst/conf.py:104: RemovedInSphinx40Warning: The app.add_javascript() is deprecated. Please use app.add_js_file() instead.
  app.add_javascript('d2l.js')
/var/lib/jenkins/workspace/d2l-book/docs/_build/eval/user/cache/_build/rst/conf.py:105: RemovedInSphinx40Warning: The app.add_stylesheet() is deprecated. Please use app.add_css_file() instead.
  app.add_stylesheet('d2l.css')
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 2 source files that are out of date
updating environment: 0 added, 2 changed, 0 removed

looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done

waiting for workers...
generating indices...  genindexdone
writing additional pages...  searchdone
copying static files... ... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded.

The HTML pages are in _build/html.
[d2lbook:build.py:L48] INFO   === Finished "d2lbook build html" in 00:00:03

Last, let’s clean our workspace.

!rm -rf cache