Python PEP for more convenient HTML generation

74 views
Skip to first unread message

guettli

unread,
Jun 11, 2021, 3:52:56 AM6/11/21
to Django developers (Contributions to Django itself)
Hi,

Since I use the html-fragments-over-the-wire pattern (htmx) I create
small function based views returning small html fragments.

Up to now I use format_html().

I am tired of passing in the variables like `foo=foo, bar=bar` into
format_html().

I created a Python PEP to introduce a new way.

The goal is to have something like the convenient f-string syntax
combined with conditional_escape().

The draft is here: 


I am looking for feedback. Either here or on python-ideas.

If Python would implement this PEP, would you accept patches
to Django to support Template Literals?

Background: I don't like the JS-Frontend madness. Django can
do more than just provide an http API.

Regards,
  Thomas

Adam Johnson

unread,
Jun 11, 2021, 4:17:02 AM6/11/21
to django-d...@googlegroups.com
Hi Thomas

As I understand there wouldn't be much required by Django to support this - just one function. So if it came to Python, I think we'd like to see a working snippet, maybe in a third party package, before merging support.

In terms of the PEP, - there are already "too many" ways to template a string in Python, so I'm not sure it will be very popular. A few alternatives spring to mind, beyond those discussed in your related forum thread ( https://forum.djangoproject.com/t/rethinking-how-to-create-html-f-string-like-with-conditional-escape/8212 ).

First, there's syntactic macros in PEP 638 ( https://www.python.org/dev/peps/pep-0638/ ). They would provide some extensibility to Python's grammar, so features like your HTML string would not need to go through PEP's and Python releases. They could just be installable packages. Definitely check that out - it seems like it's still in draft phase though.

Second, a related way to emulate such macros currently is with a codec. Check out the future-fstrings package, which backported f-strings to older versions of Python: https://github.com/asottile/future-fstrings . As a "wrapper" text codec, it gets to pre-process the source code before passing it on to Python to execute. You could do your template string transformation with the current version of Python like this.

Third, stack inspection would also allow you to emulate template strings. Take this example:

import inspect

from django.utils.html import format_html


def html(string):
    locals_ = inspect.currentframe().f_back.f_locals
    return format_html(string, **locals_)


def main():
    name = "Adam"
    statement = "I'm < 120 years old"
    print(html("<strong>{name}</strong> says <em>{statement}</em>"))


if __name__ == "__main__":
    main()


Running it:

$ python ex.py
<strong>Adam</strong> says <em>I&#x27;m &lt; 120 years old</em>


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/39137657-4b16-42ad-a2ea-4d97ba9382d4n%40googlegroups.com.

guettli

unread,
Jun 11, 2021, 6:36:05 AM6/11/21
to Django developers (Contributions to Django itself)
Adam Johnson schrieb am Freitag, 11. Juni 2021 um 10:17:02 UTC+2:
Hi Thomas

As I understand there wouldn't be much required by Django to support this - just one function. So if it came to Python, I think we'd like to see a working snippet, maybe in a third party package, before merging support.


Thank you Adam. The change in Django would be tiny. I think only HttpResponse and conditional_escape would need a change. I will provide the PR if the PEP gets accepted.

 
In terms of the PEP, - there are already "too many" ways to template a string in Python, so I'm not sure it will be very popular. A few alternatives spring to mind, beyond those discussed in your related forum thread ( https://forum.djangoproject.com/t/rethinking-how-to-create-html-f-string-like-with-conditional-escape/8212 ).

First, there's syntactic macros in PEP 638 ( https://www.python.org/dev/peps/pep-0638/ ). They would provide some extensibility to Python's grammar, so features like your HTML string would not need to go through PEP's and Python releases. They could just be installable packages. Definitely check that out - it seems like it's still in draft phase though.


I will have a look at PEP 638.

 
Second, a related way to emulate such macros currently is with a codec. Check out the future-fstrings package, which backported f-strings to older versions of Python: https://github.com/asottile/future-fstrings . As a "wrapper" text codec, it gets to pre-process the source code before passing it on to Python to execute. You could do your template string transformation with the current version of Python like this.


I will  have a loot at this. I would be happy if no PEP would be needed.

But one thing is important for me: IDE and linters should understand it.

 
Third, stack inspection would also allow you to emulate template strings. Take this example:

import inspect

from django.utils.html import format_html


def html(string):
    locals_ = inspect.currentframe().f_back.f_locals
    return format_html(string, **locals_)


def main():
    name = "Adam"
    statement = "I'm < 120 years old"
    print(html("<strong>{name}</strong> says <em>{statement}</em>"))


if __name__ == "__main__":
    main()


Because of the lack of IDE/linters support I added this to the "Rejected Ideas".

Thank you for your feedback, I will look into your hints.

Regards,
  Thomas

Reply all
Reply to author
Forward
0 new messages