Exercise 8: Add extensions to your book

Exercise 8: Add extensions to your book#

TeachBooks supports many useful extensions, but sometimes you may want more functionality. In this exercise, you’ll learn how to add an external extension to your book that is not already built-in. We will use the Sphinx-Indexed-Definitions extension as an example, which allows you to define glossary-style entries with indexed labels in an easy way.

  1. In your book repository, open the requirements.txt file. This file lists the Python packages that will be installed to build your book.

  2. Add the following line to the bottom of the requirements.txt file:

    sphinx-indexed-definitions
    

    This tells the build system to install the extension from PyPi, a repository which contains Python software

  3. Next, open _config.yml and add the extension to your sphinx configuration block:

    sphinx:
      extra_extensions:
        - indexed_definitions
    

    This ensures Sphinx loads the extension when building your book.

  4. Now you can use the new directive provided by the extension. First, read the documentation to understand how to apply it. In most cases, the documentation can be found in the README of the source repository of the software. For our example that is here: TeachBooks/Sphinx-Indexed-Definitions

  5. Try adding the following to one of your Markdown files in the book/ directory:

    ````{definition} Term
    :label: term-definition
    A word or phrase used to describe a concept or object.
    ````
    

    This will render a labelled, indexed definition that you can refer to from other parts of your book.

  6. Try to reference the definition in another file using {ref} followed by the given label, as shown below:

    For more details, see the definition of {ref}`term-definition`.
    

    This will appear as a hyperlink that takes the reader directly to the definition.

  7. The extension can also automatically index terms based on text formatting. In another .md file, try writing:

    This is a **strong term**, this is an *emphasised term*, and this is a `literal term`.
    

    When the book is built, these terms will be included in the generated index without needing to define them manually. You can view the index in the build output to confirm.

  8. The same steps you followed here can be used to install any other extension of your choice. Simply add the extension to your requirements.txt (using either a GitHub link or PyPI package name), and list it under extra_extensions in _config.yml. Then use its features in your Markdown files according to the extension’s documentation. Check out the ever-growing list of TeachBooks extensions in the TeachBooks manual!

  9. When you are ready, commit your changes to the repository by clicking on the green Commit changes button.

  10. Add a commit message.

  11. To see your changes, go to Actions - The most recent workflow run overview.md / the commit message of the commit you just made - Wait for it to finish - In the summary, click on the link of your book shown in the table Branches deployed and under Primary book at root (getting bored of waiting? There’ll be exercising on doing this locally which prevents you from waiting).

  12. Do you see your change? If you don’t see it click CTRL+F5/Control+F5to refresh the page.

Check your understanding

Before moving on, make sure you understand:

  • How do you add a new extension to a TeachBooks book?

  • What is the purpose of the requirements.txt and _config.yml files?

  • How can you confirm that your extension is working correctly?