|
| 1 | +.. _customize: |
| 2 | + |
| 3 | +================= |
| 4 | +Customizing Voila |
| 5 | +================= |
| 6 | + |
| 7 | +There are many ways you can customize Voila to control the look and feel |
| 8 | +of the dashboards you create. |
| 9 | + |
| 10 | +Controlling the nbconvert template |
| 11 | +================================== |
| 12 | + |
| 13 | +Voila uses **nbconvert** to convert your Jupyter Notebook into an HTML dashboard. |
| 14 | +nbconvert has a rich templating system that allows you to customize the way in |
| 15 | +which your Jupyter Notebook is converted into HTML. |
| 16 | + |
| 17 | +By default, Voila will render the HTML from your notebook in the same linear fashion |
| 18 | +that the notebook follows. If you'd like to use a different layout, this can be |
| 19 | +controlled by creating a new nbconvert template, registering it with Voila, |
| 20 | +and calling it from the command-line like so: |
| 21 | + |
| 22 | +.. code-block:: bash |
| 23 | +
|
| 24 | + voila <path-to-notebook> --template=<name-of-template> |
| 25 | +
|
| 26 | +For example, Voila includes one other template that uses a Javascript library and |
| 27 | +an alternate ``<div>`` layout in order to let the user drag and drop cells. |
| 28 | + |
| 29 | +To use this template when creating your dashboard, use the following command: |
| 30 | + |
| 31 | +.. code-block:: bash |
| 32 | +
|
| 33 | + voila <path-to-notebook> --template=gridstack |
| 34 | +
|
| 35 | +Creating your own template |
| 36 | +========================== |
| 37 | + |
| 38 | +You can create your own nbconvert template for use with Voila. This allows you |
| 39 | +to control the look and feel of your dashboard. |
| 40 | + |
| 41 | +In order to create your own template, first familiarize yourself with **Jinja**, |
| 42 | +**HTML**, and **CSS**. Each of these is used in creating custom templates. |
| 43 | +For more information, see |
| 44 | +`the nbconvert templates documentation <https://nbconvert.readthedocs.io/en/latest/customizing.html#Custom-Templates>`_. |
| 45 | +For one example, `check out the nbconvert basic HTML template <https://github.com/jupyter/nbconvert/blob/master/nbconvert/templates/html/basic.tpl>`_. |
| 46 | + |
| 47 | +Where are Voila templates located? |
| 48 | +---------------------------------- |
| 49 | + |
| 50 | +All voila templates are stored as folders with particular configuration/template files inside. |
| 51 | +These folders can exist in the standard Jupyter configuration locations, in a folder called ``voila/template``. |
| 52 | +For example: |
| 53 | + |
| 54 | +.. code-block:: bash |
| 55 | +
|
| 56 | + ~/.local/share/jupyter/voila/template |
| 57 | + ~/path/to/env/dev/share/jupyter/voila/template |
| 58 | + /usr/local/share/jupyter/voila/template |
| 59 | + /usr/share/jupyter/voila/template |
| 60 | +
|
| 61 | +Voila will search these locations for a folder, one per template, where |
| 62 | +the folder name defines the template name. |
| 63 | + |
| 64 | +The Voila template structure |
| 65 | +---------------------------- |
| 66 | + |
| 67 | +Within each template folder, you can provide your own nbconvert templates, static |
| 68 | +files, and HTML templates (for pages such as a 404 error). For example, here is |
| 69 | +the folder structure of the base Voila template (called "default"): |
| 70 | + |
| 71 | +.. code-block:: bash |
| 72 | +
|
| 73 | + tree path/to/env/share/jupyter/voila/template/default/ |
| 74 | + ├── nbconvert_templates |
| 75 | + │ ├── base.tpl |
| 76 | + │ └── voila.tpl |
| 77 | + └── templates |
| 78 | + ├── 404.html |
| 79 | + ├── error.html |
| 80 | + ├── page.html |
| 81 | + └── tree.html |
| 82 | +
|
| 83 | +**To customize the nbconvert template**, store it in a folder called ``templatename/nbconvert_templates/voila.tpl``. |
| 84 | +In the case of the default template, we also provide a ``base.tpl`` that our custom template uses as a base. |
| 85 | +The name ``voila.tpl`` is special - you cannot name your custom nbconvert something else. |
| 86 | + |
| 87 | +**To customize the HTML page templates**, store them i na folder called ``templatename/templates/<name>.html``. |
| 88 | +These are files that Voila can serve as standalone HTML (for example, the ``tree.html`` template defines how |
| 89 | +folders/files are displayed in ``localhost:8866/voila/tree``). You can override the defaults by providing your |
| 90 | +own HTML files of the same name. |
| 91 | + |
| 92 | +**To configure your Voila template**, you should add a ``config.json`` file to the root of your template |
| 93 | +folder. |
| 94 | + |
| 95 | +.. todo: Add information on config.json |
| 96 | +
|
| 97 | +
|
| 98 | +An example custom template |
| 99 | +-------------------------- |
| 100 | + |
| 101 | +To show how to create your own custom template, let's create our own nbconvert template. |
| 102 | +We'll have two goals: |
| 103 | + |
| 104 | +1. Add an ``<h1>>`` header displaying "Our awesome template" to the voila dashboard. |
| 105 | +2. Add a custom 404.html page that displays an image. |
| 106 | + |
| 107 | +First, we'll create a folder in ``~/.local/share/jupyter/voila/template`` called ``mytemplate``:: |
| 108 | + |
| 109 | + mkdir ~/.local/share/jupyter/voila/template/mytemplate |
| 110 | + cd ~/.local/share/jupyter/voila/template/mytemplate |
| 111 | + |
| 112 | +Next, we'll copy over the base template files for voila, which we'll modify:: |
| 113 | + |
| 114 | + cp -r path/to/env/share/jupyter/voila/template/default/nbconvert_templates ./ |
| 115 | + cp -r path/to/env/share/jupyter/voila/template/default/templates ./ |
| 116 | + |
| 117 | +We should now have a folder structure like this:: |
| 118 | + |
| 119 | + tree . |
| 120 | + ├── nbconvert_templates |
| 121 | + │ ├── base.tpl |
| 122 | + │ └── voila.tpl |
| 123 | + └── templates |
| 124 | + ├── 404.html |
| 125 | + ├── error.html |
| 126 | + ├── page.html |
| 127 | + └── tree.html |
| 128 | + |
| 129 | +Now, we'll edit ``nbconvert_templates/voila.tpl`` to include a custom H1 header. |
| 130 | + |
| 131 | +As well as ``templates/tree.html`` to include an image. |
| 132 | + |
| 133 | +Finally, we can tell ``Voila`` to use this custom template the next time we use it on |
| 134 | +a Jupyter notebook by using the name of the folder in the ``--template`` parameter:: |
| 135 | + |
| 136 | +voila mynotebook.ipynb --template=mytemplate |
| 137 | + |
| 138 | +The result should be a Voila dashboard with your custom modifications made! |
0 commit comments