Re:Show HN: A Python native engine for web development
by redfox2 on Sunday July 11, 2021 @07:20AM
(#27799974)
'This simplifies things, and you can use an in-memory StringIO object rather than a file. You can then return this string to flask as a text/html response and avoid Jinja2 entirely.'
I didn't think of this until you mentioned this, so might do that. But I don't get the 'avoid Jinja2 entirely' part. Jinja2 is avoided with the current syntax already (which is definitely changing).
About BeautifulSoup, the autoPrettify() function is actually from bs4, it is a dependency.
Yeah I know, camelCase IS unpythonic
Thanks again
Re:Show HN: A Python native engine for web development
by orf on Sunday July 11, 2021 @07:26AM
(#27799995)
When you do “render_template()” Flask uses jinja2 - it will read it, pass it to jinja2 and render the response. This is what I meant by avoiding jinja2 entirely.
Good luck!
Re:Show HN: A Python native engine for web development
by redfox2 on Monday July 12, 2021 @12:10AM
(#27806963)
But the issue when I avoid render_template() is this:
I can't add the CSS file along with the HTML one, unless I create a <style> tag at the end of the file that has all of the CSS content. Can you comment on that?
Also don't use CamelCase, please! It looks very unpythonic.
However you're really trying to create an API for producing a tree of nodes (some of which might logically map to individual tags, some of which might not) which are then serialized to HTML.
I'd recommend looking at how you can use BeautifulSoup for a lot of this. BS4 contains a lot of classes and functions for creating trees of HTML-like nodes, so you could use this rather than a StringIO object and directly writing "<div>" and having to support all of the attributes as method arguments. You could just use "soup.new_tag(element, *kwargs)" and only do something "special" when handling pandas dataframes or other stuff (like ol/ul tags).
Check out the BS4 documentation and methods for tree manipulation here: https://www.crummy.com/software/BeautifulSoup/bs4/doc/