Blog of a data person

Parsing Python web components

2025-04-13

I have added a fun party trick to my blog. I can now write stuff like this:

<terminal theme="dark">
web components (sortof) from Python!
</terminal>

And it will look like this:

terminal
web components (sortof) from Python!

Notice that theme? I can change it.

<terminal theme="light">
web components (sortof) from Python!
</terminal>

And when I do:

terminal
web components (sortof) from Python!

It's all thanks to a new feature I am trying out in mohtml. Under the hood there's a registry and a parser that understand that when it sees HTML like above that it should call a Python function with the following call:

terminal(
    "web components (sortof) from Python!", 
    theme="light"
)

This works via a registrar pattern. So ahead of time you would run something like this:

from mohtml.parser import CustomHTMLParser
from mohtml.components import terminal 

parser = CustomHTMLParser()

# You would normally use this as a decorator. May change this. 
parser.register("terminal")(terminal)

# Parser has components configured, can now post process rendered markdown
parser(blogpost_rendered)

The terminal Python function will effectively handle the rest. It can either use the functions in mohtml but it can also use Jinja2 under the hood. The really cool thing about the approach is that effectively I get some MDX-style features without writing any javascript what-so-ever.

This is super early days for this project, but so much fun!