-
Notifications
You must be signed in to change notification settings - Fork 568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SVG cleaning should not use HTML cleaner #1894
Comments
I am also seeing the same issue in my nbconvert-ed HTML pages. See #1849 (comment) |
👋 Myself and a colleague have been looking into this as well. It looks like despite this PR although the Our current workaround is to revert back to the old sanitization behavior and define our own lxml.Cleaner() to override default_filters["clean_html"]:
|
I'll also share my hack while we wait for a fix. Change I'm curious why there is an option to turn off HTML cleaning, but it doesn't apply to the SVG output (like line 173). |
I use from nbconvert.exporters import HTMLExporter
from nbconvert.exporters.templateexporter import default_filters
def convert_to_html(notebook_file, html_file):
def custom_clean_html(element):
return element.decode() if isinstance(element, bytes) else str(element)
default_filters["clean_html"] = custom_clean_html
exportHtml = HTMLExporter()
output, _ = exportHtml.from_filename(notebook_file)
open(html_file, mode="w", encoding="utf-8").write(output) This works for me with nbconvert=7.2.5 and my SVG figures are now all back! 🎉 |
Thanks for sharing this fix. For people like me using it trough tan external library (to generate a documentation in my case with mkdocs-jupyter), we will need that to be fixed in |
This is also mentioned in #1863. |
Related to mkdocs-jupyter a consumer of nbconvert: Related to nbconvert: If this solution is not too lazy as to denigrate the character of the changeset author, would the maintainers find it an acceptable solution to introduce a github action that renders the html produced on a headless browser like chrome as a form of validation testing? I ask because it seems like that would quickly outsource to Google and consumers of the library most of the complexity that would come with writing unit tests that cover the entire spectrum of W3 Standards. When a break is found by a consumer, grab their notebook and add it to the array of validation tests. |
I have the same problem on <xlink:href> SVG attribute. (Ex. axes labels in matplotlib objects) So I added bypass_svg() function for strings.py (in lib/python3.11/site-packages/nbconvert/filters) and related files: templateexporter.py (in lib/python3.11/site-packages/nbconvert/exporters) and base.html.j2 (in share/jupyter/nbconvert/templates/lab) as attached patches below. templateexporter.py.patch This work around is so fine for me. |
I don't know if this is correct, but while researching methods to sanitize SVG this site was mentioned on security.stackexchange.com which suggests that SVG loaded via img tags won't execute scripts. Edit: this fixed nbconvert (7.6.0) html and webpdf output for me when using matplotlib with SVG output:
|
Originally posted by @akx in #1890 (comment)
(I just wanted to make an issue out of this so it's searchable...)
The text was updated successfully, but these errors were encountered: