Template

Overview:

  • Teaching: 10 min
  • Exercises: 0 min

Questions

  • How can I change the look of NBfancy cells?

Objectives

  • Understand that a new style can be defined in a notebook

If NBfancy is initialised with the flag --extra_config it will give the user access to two additional configuration files: keywords.cfg and box.ipynb. The file keywords.cfg is discussed in the previous section. The second file, box.ipynb, is a notebook which contains two cells, and allows the user to change the template that NBfancy uses for keyword cells when rendering notebooks.

In [1]:
!nbfancy init example2 --extra_conf
!ls example2/config
box.ipynb  footer.ipynb  header.ipynb  keywords.cfg

The first cell give a description of how you can modify the second and read:

You can replace the existing template by modifying the next cell. When the notebook is rendered the following will happen:

  1. The keyword does not need to appear in this template and will be automatically removed, unless specified in default.cfg.
  2. The colour w3-green will be substitued with the colour in default.cfg.
  3. The colour w3-pale-green will be substituted with the corresponding pale background colour.
  4. {index} is used internally, don't remove it or you will break things.
  5. The symbol fa-star will be substitued with the symbol in default.cfg.
  6. TITLE will be substituted with the marked up title which follows the colon after the keyword.
  7. BODY will be substituted with the marked up content of the cell after the first line.

In the plain notebook, this markdown would generate the next cell:

markdown
## Keyword: TITLE
BODY

Note the spacing may not be exactly the same since the TITLE and BODY will have additional HTML tags added when marked up.

The second cell contains the HTML code seen below. You can modify to suit the new style you want to use.

In [2]:
%%HTML
<div class="w3-panel w3-leftbar w3-border-green w3-pale-green w3-padding-small">
    <h3 id="{index}"><i class="fa fa-star"></i> TITLE</h3>
    BODY
</div>

TITLE

BODY

Key Points:

  • The template that NBfancy uses can be modified by the user
  • The header and footer for the notebooks can also be changed