spatialaudio / nbsphinx

:ledger: Sphinx source parser for Jupyter notebooks
https://nbsphinx.readthedocs.io/
MIT License
449 stars 128 forks source link

configure nbsphinx to generate html tags from notebook metadata? #722

Open tylerflex opened 1 year ago

tylerflex commented 1 year ago

Our .ipynb files contain basic metadata that we want to embed in the eventual html in our sphinx-generated docs.

As an example:

# Example.ipynb
{
    "metadata": {
        "title": "A Well Written Title.",
        "description": "A meta tag description.",
        ...
    },
    "cells" : [...]
}
# Example.html
<title>A Well Written Title.</title>
<meta name="description"  content=A meta tag description. />

How can I accomplish this with nbsphinx?

With nbconvert, I am able to use a custom template with the following lines in it

{% set nb_title = nb.metadata.get('title', '') or resources['metadata']['name'] %}
<title>{{nb_title}}</title>

{% set nb_descr = nb.metadata.get('description', '') %}
<meta name="description"  content={{nb_descr}} />

Is it possible to specify nbsphinx to use this template in my conf.py? Or is there a way to replicate this behavior in nbsphinx configuration?

Any other approaches you can recommend? Thanks!

mgeier commented 1 year ago

Is is not possible to use nbconvert templates with nbsphinx, because it doesn't use nbconvert to create the HTML output. Instead, Sphinx is using its own templates, see e.g. https://github.com/sphinx-doc/sphinx/blob/master/sphinx/themes/basic/layout.html

I'm sure this can be implemented rather easily. The template uses the title and titlesuffix Jinja variables, which I guess can be somehow set during the build process. And there is a metatags variable which can probably be used for the description thing.