Advanced HTML outputs

Custom CSS or JavaScript

If you’d like to include custom CSS rules or JavaScript scripts in your book, add them to a folder called _static in your book’s folder. Any files that end in .css or .js in this folder will automatically be copied into your built book HTML and linked in the header of each page.

For example, to include a custom CSS file myfile.css in a Jupyter Book folder with the following structure:

mybook/
├── _config.yml
├── _toc.yml
└── page1.md

Add the static file here:

├── _config.yml
├── _toc.yml
├── page1.md
└── _static
    └── myfile.css

The rules should then automatically be applied to your site. In general, these CSS and JS files will be loaded after others are loaded on your page, so they should overwrite pre-existing rules and behaviour.

An example: justify the text

If you want the text of you book to be justified instead of left aligned then create myfile.css under mybook/_static with the following CSS:

p {
    text-align: justify;
}

Enable Google Analytics

If you have a Google account, you can use Google Analytics to collect some information on the traffic to your Jupyter Book. With this tool, you can find out how many people are using your book, where they come from and how they access it, whether they are using the desktop or the mobile version etc.

To add Google Analytics to your Jupyter Book, navigate to Google Analytics, create a new Google Analytics account and add the url of your Jupyter Book to a new property. Once you have set everything up, your Google Analytics property will have a so-called Tracking-ID, that typically starts with the letters UA. All that you need to do is to copy this ID and paste it into your configuration file:

html:
  google_analytics_id: UA-XXXXXXXXX-X

Use raw HTML in Markdown

Jupyter Notebook Markdown allows you to use raw HTML in Markdown cells. This is discouraged in most cases, because it will usually just be passed through the build process as raw text, and so will not be subject to processes like:

  • relative path corrections

  • copying of assets to the build folder

  • multiple output type formatting (e.g. it will not show in PDFs!).

So, for instance, below we add:

<a href="../intro.md">Go Home HTML!</a>

[Go Home Markdown!](../intro.md)

and you will find that the HTML link is broken:

Go Home HTML!

Go Home Markdown!

小技巧

Note that MyST Markdown now has some extended syntax features, which can allow you to use certain HTML elements in the correct manner.

For example, the raw HTML image tag

<img src="../images/fun-fish.png" alt="the fun fish!" width="200px"/>

becomes

the fun fish!

See the image section for details.

Adding extra HTML to your book

There are a few places in Jupyter Book where you can add extra arbitrary HTML. In all cases, this is done with a configuration value in your _config.yml file.

Extra HTML to your left navbar

To add extra HTML in your book’s left navbar, use the following configuration:

html:
    extra_navbar: |
        <div>
            your html
        </div>

The contents of extra_navbar will be inserted into your page’s HTML after all other HTML content.

Manually specify extra files/folders to be included in a website

Jupyter Book will copy over any files that are linked from within its pages so that the links work in the built website. However, sometimes you’d like to manually ensure that files and folders are included in your built website. For example, if you’d like to link to them from outside your built documentation, but not from within your built documentation.

To manually specify items to copy over, use the html_extra_path Sphinx configuration. You can configure this with Jupyter Book like so:

sphinx:
  config:
    html_extra_path: ['folder1', 'folder2']

When you build your book’s HTML, Jupyter Book will ensure that all files and folders inside the folders specified in html_extra_path will be copied over to your built website.

For example, if you have a folder structure in your book like so:

assets
└── data
    └── mydataset.csv

and the following Jupyter Book configuration:

sphinx:
  config:
    html_extra_path: ['assets']

Then the dataset will be accessible at yourwebsite.com/data/mydataset.csv.

Configuring to Improve Accessibility

Declaring the primary language used in your book assists screen reader and browser translation tools.

Language can be configured by providing the appropriate language code to the language option, under sphinx configuration in your _config.yml file:

sphinx:
  config:
    language: en

This example will set the book language to English, which would be represented in your book’s HTML as <html lang="en">...</html>.