3 ms·
How can you call a function that does this? html'<div id={id:int}>{content:HTML|str}</div>'. html() is not going to be equivalent.
by Too 2y ago
How can you call a function that does this? html'<div id={id:int}>{content:HTML|str}</div>'.
html() is not going to be equivalent.
- deleted 2y ago[deleted]
- mrweasel 2y agoThat is probably a much better example than any of those present in the PEP. I quite like your example. I'm not sure I'd want to write code like that, but it shows the usefulness much more clearly.
- pauleveritt 2y agoWe originally had a long HTML tutorial in the PEP. It was extracted. It's here, if anyone is interested: https://pauleveritt.github.io/tagstr-site/htmlbuilder.html https://pauleveritt.github.io/tagstr-site/htmlbuilder.html Companion repo with: JupyterLite playground, Docker images, other material. https://github.com/pauleveritt/tagstr-site https://github.com/pauleveritt/tagstr-site
- throwitaway1123 2y agoFor a practical example of this technique used in JS take a look at libraries like htm and lit-html: https://github.com/developit/htm https://github.com/developit/htm
- pauleveritt 2y ago(PEP co-author here) The htm folks did a Python implementation: https://pypi.org/project/htm/ https://pypi.org/project/htm/ It required a janky workaround for the absence of this PEP: https://pypi.org/project/tagged/ https://pypi.org/project/tagged/ I used these to investigate ideas about component-driven development: https://viewdom.readthedocs.io/en/latest/ https://viewdom.readthedocs.io/en/latest/
- prpl 2y agoHow is that a Janky workaround? It's not particularly ugly or verbose, nor does it seem to violate any python tenets.
- daveFNbuck 2y agoCan you explain the difference?
- deleted 2y ago[deleted]
- pauleveritt 2y agoDoing `html('<div>Hello {name}</div>')` would be possible. I have a system that's based on it. Two issues: - No tooling in Python will do anything with the string, so DX suffers. - Evaluating the variable requires frame evaluation, which is...problematic. You could do `html(f'<div>Hello {name}</div>')` and get f-string coding assistance. But you'd also get immediate evaluation. There's nothing the `html` function can do with the `{name}` part.
- zelphirkalt 2y agoI would probably use Jinja2.