nbconvert
nbconvert copied to clipboard
Disable the implicit load of additional libs (mathjax, jquery, requirejs)
Hello,
I'm currently using nbconvert but, given the raise of some security concerns related to Content Security Policy (CSP), I've decided to switch to the alternative: Katex.
For that, I've attempted to disable the implicit load of Mathjax, by using the configuration option HTMLExporter.mathjax_url intended to customize the Mathjax load (amongst other unneeded libs):
config.HTMLExporter.mathjax_url = ''
config.HTMLExporter.jquery_url = ''
config.HTMLExporter.require_js_url = ''
The problem is that this workaround does not disable the script, it instead renders it as <script src=""></script>.
I would like to ask what would be the possibility of providing the option to set these as undefined in order to completely disable them.
In addition, I did not find any evidence for the need of jquery and requirejs while using the classic template (the docs only mention them for sliders and not the standard HTML export).
See Programmatically creating templates: https://nbconvert.readthedocs.io/en/latest/nbconvert_library.html
from jinja2 import DictLoader
dl = DictLoader(
{
'lab_remove_js': """
{%- extends 'lab/index.html.j2' -%}
{%- block html_head_js_requirejs -%}
{%- endblock html_head_js_requirejs -%}
{%- block html_head_js_mathjax -%}
{%- endblock html_head_js_mathjax -%}
""",
}
)
This is for the lab template which does not use jquery. Similar can be done for classic.
Dear @lorenzncode ,
I'm a user of mkdocs-jupyter, which invokes nbconvert. In order to enable CSP conform htmls by using locally installed MathJax, I need to switch off the implicit load. Currently, I simply remove/empty the URL in html.py of my venv. It works, but...
Could you please provide a hint, how to correctly tell nbconvert, not to use the default URLs? The audience of your answer would be IMHO the developer(s) of mkdocs-jupyter. Thanks!
@dr-br If this was vanilla nbconvert, I would create a customized HTMLExporter as (with the template override from the previous comment):
HTMLExporter(config=c, extra_loaders=[dl], template_file="lab_remove_js")
I haven't used mkdocs-jupyter. It looks like the HTMLExporter is here: https://github.com/danielfrg/mkdocs-jupyter/blob/74892c0b790f076126967850f175aec559474ec7/src/mkdocs_jupyter/nbconvert2.py#L119
Try to modify the HTMLExporter or the template in mkdocs-jupyter.
Thanks for the hint, I will try to get mkdocs-jupyter in that direction!
Edit: Thanks @lorenzncode , your hint was helpful. I modified the HTMLExporter.