TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Universal Jinja: a crazy idea for a Python-ready front end

171 pointsby coffeefirstalmost 8 years ago

17 comments

skywhopperalmost 8 years ago
Ugh, Jinja is not an "excellent" templating language. It's a terrible broken mix of pythonisms and arbitrary-feeling restrictions and oddly missing pieces. It becomes hard to read very quickly and some of the looping idioms are just nuts. Sorry I don't have anything constructive to say here but I'm genuinely taken aback that there are apparently big fans of Jinja.
评论 #14777896 未加载
评论 #14777961 未加载
评论 #14778362 未加载
评论 #14777736 未加载
评论 #14777864 未加载
评论 #14781891 未加载
评论 #14779370 未加载
sametmaxalmost 8 years ago
I was wondering if we should not code a Python renderer for one JS framework and be done with it.<p>We already have one JS engine in Python (<a href="https:&#x2F;&#x2F;github.com&#x2F;kovidgoyal&#x2F;dukpy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kovidgoyal&#x2F;dukpy</a>). We even have pluggable renderers (<a href="https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;PyExecJS" rel="nofollow">https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;PyExecJS</a>). Later we will have webassembly renderers in Python, so you will be able to take frontend frameworks, compile them and execute them in Python.<p>Finally, we have some fantastic framework that are powerful, yet not that big, such as Vue.js, that already have a pluggable architecture for rendering without a DOM on the server side (<a href="https:&#x2F;&#x2F;ssr.vuejs.org&#x2F;en&#x2F;basic.html" rel="nofollow">https:&#x2F;&#x2F;ssr.vuejs.org&#x2F;en&#x2F;basic.html</a>)<p>The Python community has done much more complicated projects already, and half the work is there, so my guess the reason it hasn&#x27;t happened yet is we REALLY hate js and most of us just touch it to get the job done and go to the next task.<p>It&#x27;s a bit like the async situation. We have the tools, but there is no Django of the asyncio because we love our tools as their are so much. I&#x27;m so guilty of this.<p>The problem is: the rest of the Web is moving ahead, and while it&#x27;s still relevant to do DRF + Vue.JS right now, one day stuff like meteorjs will be so crazy yet stable it will be hard to compete.<p>I was hopping webassembly would let us eventually run Python on the client side. But I doubt it will happen. However, I&#x27;m thinking more and more than it will allow JS tooling to be plugged in many other languages on the server side.
评论 #14777973 未加载
评论 #14777780 未加载
评论 #14778625 未加载
评论 #14778015 未加载
stevebmarkalmost 8 years ago
&quot;Isomorphic&quot; templates are not enough. You also need isomorphic data fetching for client code. If you have to manually reinvent the same data fetching on the client and server, you don&#x27;t get the full benefits of isomorphic code. You only have brittle templates with two different ways to glue data to view.<p>So far isomorphic Node + React has been the best (but not perfect) way I&#x27;ve seen to do this. You abstract out the API layer, and now you can define components and their data sources that have the exact same code on the client and server, and it just works. Apollo&#x2F;GraphQL is probably a giant leap forward in this as well.<p>Having an option to do hand-glued &quot;isomorphic&quot; data in a few places is probably nicer than Javascript spaghetti on the front end, but it doesn&#x27;t get you all the way there. Rails has the same problem by the way, the &quot;Rails way&quot; is coffeescript glue to get any kind of modern front end application functionality. Even with React on Rails you have to have to reinvent data fetching, and lose the benefit of a well structured isomorphic codebase.<p>Until browsers support a different language natively other than Javascript (which will never happen), Node + React is currently the most straightforward path to get there.
评论 #14779407 未加载
zepolenalmost 8 years ago
I&#x27;ve done this many years ago with <a href="https:&#x2F;&#x2F;github.com&#x2F;wkral&#x2F;jinja2js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;wkral&#x2F;jinja2js</a><p>It works - but there are gotchas:<p>1. You can&#x27;t depend on useful pythonisms&#x2F;javascriptisms such as<p><pre><code> {{thing or &quot;&quot;}} vs {{thing || &quot;&quot;} </code></pre> 2. Template filter code still has to be duplicated in both languages - i18n such as {{_(&#x27;String&#x27;)}} for example - which sort of makes sense - but then you have to do the same for some other, really trivial, filters.<p>3. Testing becomes annoying because a template can work in one, and not the other - you have to test both.<p>4. You don&#x27;t get the newer html diff style virtual dom rendering which can cause issues with stuff like select boxes. You end up with special case code to handle that, which is more of a hassle.<p>The conclusions from it all were:<p>a) You <i>do</i> save time over doing it once in python, once in js<p>b) You <i>don&#x27;t</i> save time over doing it all in js - especially if you use a newer functional style js template framework (react&#x2F;vue&#x2F;polymer)<p>c) You can still get the best of both worlds by hooking up your templates in js to be rendered by your backend - eg like <a href="https:&#x2F;&#x2F;github.com&#x2F;reactjs&#x2F;react-rails" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;reactjs&#x2F;react-rails</a> does.<p>In the end for my next project I decided to do an api for the backend in python and the website&#x2F;frontend in react with SSR, and am really thrilled with the results - very maintainable, testable and simple overall.
bastawhizalmost 8 years ago
I see a few problems.<p>The biggest problem is testability. Testing that a template renders correctly means parsing the markup into a DOM and asserting that certain conditions are met. This is exacerbated by the differences between Jinja and Nunjucks, which can be non-trivial and unexpected (e.g., Jinja will render synchronously, while Nunjucks can render asynchronously--weird async behavior may appear). Since your application doesn&#x27;t have the same entry point to the code (they&#x27;re in two different languages!), you really need to write two different sets of tests for the same template to get proper coverage. Templates being templates, you also don&#x27;t get the granular testability of a React component out-of-the-box, which can be pulled out and tested individually.<p>Templates alone also don&#x27;t include anything in the way of handling events, component lifecycle, and other things that make your code actually do stuff. You&#x27;ll need to bind event listeners manually, and those will of course need their own separate tests.<p>Making Jinja useful also usually involves adding your own plugins and filters. This happens in Python, which means you&#x27;ll need to write all of those a second time in JavaScript, and test them. Doing this right means comparing the output of the two versions, which isn&#x27;t necessarily straightforward. Given the complexity of this, I wouldn&#x27;t bet money that other engineers would go through the hassle of making sure this is done correctly.<p>I&#x27;d encourage the author to write more about the pitfalls of this approach. It&#x27;s easy to write {{ foo.strip() }} in Jinja and forget that Nunjucks requires {{ foo.trim() }}, or empty arrays being falsey in Jinja and truthy in Nunjucks.
评论 #14777524 未加载
评论 #14777998 未加载
Walkmanalmost 8 years ago
Mike Bayer wrote a really interesting article about this topic 5 years ago: <a href="http:&#x2F;&#x2F;techspot.zzzeek.org&#x2F;category&#x2F;mako&#x2F;" rel="nofollow">http:&#x2F;&#x2F;techspot.zzzeek.org&#x2F;category&#x2F;mako&#x2F;</a><p>These techniques are easier with the Pyramid framework.
cyberpantheralmost 8 years ago
I usually ignore server side rendering or isomorphic rendering. I&#x27;m not saying all projects should ignore it but you&#x27;re really talking about a high level of optimization if you need it. And if you&#x27;re reaching that level of optimization you probably shouldn&#x27;t use Python.<p>Server side rendering only makes the first view faster. After that, everything is rendered on the client and faster that way. And if you use caching with progressive web apps or just file caching in general, the first view will be fast after. So you&#x27;re only optimizing for the first of the first. You can then provide the initial data in the page request so that saves a round trip on the first request while still rendering on the frontend.<p>I say all this to just point out that there are many ways to speed up your frontend without doing SSR and they are much easier to implement with Python. So I&#x27;d rather exhaust all these techniques before implementing SSR. If I get to that point though, I suspect Python might be the slowest part of your stack. And lastly if you are doing SSR for SEO purposes then just use a prebuilt prerender service.<p>With all that said if there was an easier way to SSR with Python&#x2F;Django, I&#x27;d definitely use it. But Jinja on the frontend doesn&#x27;t sound like that answer.
评论 #14778537 未加载
评论 #14778132 未加载
chickenfriesalmost 8 years ago
Would not recommend using Nunjucks, unless you would like to contribute to maintaining it.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;mozilla&#x2F;nunjucks&#x2F;blob&#x2F;master&#x2F;CONTRIBUTING.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mozilla&#x2F;nunjucks&#x2F;blob&#x2F;master&#x2F;CONTRIBUTING...</a>
评论 #14779805 未加载
dochtmanalmost 8 years ago
I actually wrote a JavaScript code generator for Jinja, which allows you to use the Jinja parser and AST and generate pretty readable and fairly sane JavaScript code (for a pretty large subset of full Jinja). I actually used this in my startup (now long dead) where we were able to reuse quite a lot of template code both on the server and the client.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;djc&#x2F;jasinja" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;djc&#x2F;jasinja</a><p>This is in some (conceptual&#x2F;experiential) ways a precursor to my recent attempt to create a Rusty Jinja-like, which tries hard to take the best ideas from Jinja and mash it up with the best ideas from Rust:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;djc&#x2F;askama" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;djc&#x2F;askama</a>
ivanbakelalmost 8 years ago
The post reads somewhat rambly, which I suppose is unavoidable if you&#x27;re doing a quick whistle-stop of a bunch of related technologies showcasing a new idea.<p>The only thing that doesn&#x27;t seem clear is why you&#x27;d need to combine server- and client-side rendering in a single instance. Jinja is an incredibly useful and well-made tool, and my experience of it with Flask always leaves me with the best impressions of it being exactly what quick webdev should be, but why would you combine it with JS? Surely the use cases are so different that a separate JS library would be just as applicable, and perhaps more fit for purpose.
评论 #14777421 未加载
erlehmann_almost 8 years ago
Templates cause bugs. The solitary appropriate solution is an unparser – a component that walks an AST and serializes it.<p>Making sure the result conforms to the grammar of the output language without any unparser would always involve a parser.<p>&gt; As soon as I&#x27;m looking at more than one programming or markup language in the same file, I&#x27;m looking at spaghetti code.<p>Iain Dooley, December 2011<p><a href="http:&#x2F;&#x2F;www.workingsoftware.com.au&#x2F;page&#x2F;Your_templating_engine_sucks_and_everything_you_have_ever_written_is_spaghetti_code_yes_you" rel="nofollow">http:&#x2F;&#x2F;www.workingsoftware.com.au&#x2F;page&#x2F;Your_templating_engin...</a>
评论 #14777549 未加载
评论 #14777713 未加载
dchukalmost 8 years ago
I&#x27;ve been working on a site recently, in Rails, and just been using Turbolinks and SJR (Server Javascript Responses). Granted, it&#x27;s a content heavy site with fairly light ajax interactions, but it&#x27;s a pretty delightful process.<p>So many people jump to heavy JS libs&#x2F;frameworks to do stuff that can be solved in much more boring&#x2F;simple ways most of the time. You don&#x27;t need React&#x2F;Angular&#x2F;Vue to submit a form over ajax.
评论 #14777557 未加载
omegaworksalmost 8 years ago
Ugh. After living in frontend land (native mobile and react) I&#x27;m really not looking forward to going back to Django.<p>It feels so irrelevant in the world of backend-as-a-service and GraphQL.<p>...Please forgive my cynicism. I am looking for a reason to like this. Maybe I should just move on.
评论 #14777580 未加载
评论 #14777902 未加载
amirouchealmost 8 years ago
I don&#x27;t understand people still using Django for their web based stuff. I was under the impression that ReactJS + aiohttp was state of the art. Please explain why I am wrong.
评论 #14789890 未加载
pbreitalmost 8 years ago
Has anyone had good experience with turbolinks on python?
uptownhralmost 8 years ago
Try nuxt for vuejs
ben_jonesalmost 8 years ago
Can someone explain to me why we haven&#x27;t normalized html templating with a Pug-like syntax (formally Jade)?<p>My perceived benefits of Pug&#x2F;Jade:<p>* Significantly fewer LOC<p>* Improved Readability<p>* A lot of existing tooling thanks to its popularity in the NodeJS community
评论 #14777916 未加载
评论 #14777924 未加载
评论 #14778744 未加载
评论 #14778291 未加载