Skip to content
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

Allow including text/x-rst outputs in rst conversion, transition away from text/restructuredtext #2167

Merged
merged 7 commits into from
Aug 28, 2024

Conversation

takluyver
Copy link
Member

Outputs in a notebook can include arbitrary mimetypes. From IPython this can be done with _repr_mimebundle_ method, for instance. Most of the export formats that nbconvert knows about have a corresponding template block for output already in that format, e.g. data_latex or data_html. However, there's no template block for rst output.

I've partially generalised this, by adding a data_native block to the display priority template rather than a specific data_rst. This is meant to hold output in the exporter's native format (its output_mimetype), iff there isn't already a dedicated block for that format. I admit that's not the tidiest definition, but it seemed the best way to avoid breaking anything.

The exporter class then has to customise filter_data_type to accept its additional mimetype. I've done this in RSTExporter. It could be generalised in TemplateExporter, but that runs more risk of surprising behaviour for subclasses, which are unlikely to have a data_native block in their templates.

@takluyver
Copy link
Member Author

The remaining test failure is because a pip bug broke installing pandocfilters 1.4.1. That's already been fixed in pip 24.2, but the environment that hatch creates still seeems to get pip 24.1. 😮‍💨

@takluyver takluyver force-pushed the rst-native-output branch from c1390ca to bb09112 Compare July 30, 2024 16:06
@takluyver
Copy link
Member Author

I've got all the CI passing now.

@takluyver takluyver added the format:ReST pertains to exporting to the Restructured Text format label Jul 30, 2024
@takluyver
Copy link
Member Author

I don't suppose I can interest anyone in reviewing this? 🙂

@@ -44,6 +44,10 @@
{{ output.text | indent }}
{% endblock stream %}

{% block data_native %}
{{ output.data['text/restructuredtext'] }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it also support text/x-rst and text/prs.fallenstein.rst? text/restructuredtext seems pretty non-standard to me (although I acknowledge that this is what nbconverte is already using for some reason)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, good point. 🤔

I think it's best for users to have 1 MIME type for rst, rather than trying to wrangle several. So maybe we should change RSTExporter.output_mimetype as well - probably to text/x-rst, since that's what docutils suggests.

What would break if we do that?

  • Raw cells in notebooks marked with the mimetype text/restructuredtext would stop showing up - but we could work around that by overriding raw_mimetypes to add back text/restructuredtext for compatibility.
  • There's a mechanism for finding templates based on output_mimetype, so custom templates marked text/restructuredtext would no longer work. I can't imagine that many people have custom rst templates, and it's an easy change, so I'd be inclined to document that as a breaking change rather than adding extra complexity to keep them working.

Does that sound reasonable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is reasonable. If I were doing the change I would probably try to make it as backward compatible as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to ping me for review once you think it is ready

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done what I described, with compatibility for raw cells using the old mimetype but not for custom templates.

I'm generally keen to keep backwards compatibility, but my impression is that it's unusual to use custom templates at all, it's an easy fix for anyone affected, and the logic for finding templates is already complex enough, so I'm reluctant to add yet another dimension to it to allow multiple mimetypes per exporter. If you want to insist, though, let me know and I'll try to add compatibility for that too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I trust your judgement. If your assessment is accurate, I would say it might be ok to release and see if anyone complains and only then add back the extra compatibility layer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@krassowski krassowski changed the title Allow including text/restructuredtext outputs in rst conversion Allow including text/x-rst outputs in rst conversion Aug 28, 2024
@krassowski krassowski changed the title Allow including text/x-rst outputs in rst conversion Allow including text/x-rst outputs in rst conversion, transition away from text/restructuredtext Aug 28, 2024
@krassowski
Copy link
Member

I updated the title to make it explicit in the changelog - please adjust if you find better wording :)

Copy link
Member

@krassowski krassowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @takluyver!

@takluyver takluyver merged commit e159962 into jupyter:main Aug 28, 2024
25 checks passed
@takluyver takluyver deleted the rst-native-output branch August 28, 2024 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement format:ReST pertains to exporting to the Restructured Text format
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants