Skip to content

Support _repr_html_ and __html__ in both directions#1

Merged
niklasf merged 4 commits intoniklasf:masterfrom
SamHames:bidirectional_interoperability
Jan 11, 2025
Merged

Support _repr_html_ and __html__ in both directions#1
niklasf merged 4 commits intoniklasf:masterfrom
SamHames:bidirectional_interoperability

Conversation

@SamHames
Copy link
Copy Markdown
Contributor

Hello, thanks for the library - it's really neat to be able to read a single short file and see immediately how everything works.

This PR is a small suggestion to extend the ideas already there around _repr_html_ in two ways:

  • by also supporting the similar __html__ convention, so tinyhtml fragments can be included directly in something like a Jinja template (and vice-versa for objects annotated with __html__).
  • by supporting rendering of objects providing _repr_html_ as part of a template. This is intended to allow inclusion of things that are already renderable in Jupyter notebooks without any additional work.

For context, I'd like to use tinyhtml as part of a library where 1.) I don't want to be too prescriptive about what templating engine people use and 2.) I expect many people will be using Jupyter notebooks, and may already be working with objects that have _repr_html_ that they want to be rendered as part of a bigger template.

Obviously it's not a huge deal to have people wrap things in raw() if they're using something else, it just turns out almost all of the supporting infrastructure is already in tinyhtml so it didn't seem too big a change to propose.

(I also extended tests and metadata to 3.13 while I was here :)

_repr_html_ is already supported to allow fragments to be rendered
in a jupyter notebook - this change extends that support in two
ways:

- it allows fragments to also render objects that support this
  protocol, so objects that already display html in a notebook
  can be rendered directly.
- in addition to _repr_html_, fragments also allow rendering and
  being rendered by __html__. This means that tinyhtml fragments
  can be rendered in other templates like Jinja.
@niklasf niklasf merged commit 9537b24 into niklasf:master Jan 11, 2025
@niklasf
Copy link
Copy Markdown
Owner

niklasf commented Jan 11, 2025

Awesome. Will release as 1.3.0 in a moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants