R
R
REDkiy2016-02-26 11:23:33
Jinja
REDkiy, 2016-02-26 11:23:33

How to improve html rendering performance with Jinja2 templating engine?

I don't have much experience in software development. Not in the sense of riveting a script with an interface, but in the sense of designing a fairly complex application and then implementing it. Well, this is the preamble.
So, I'm developing a web application, the task is to take events over a period of time (usually a month) and display them with minute detail. There can be about 1000 events, time intervals are attached to each, there can be a lot of these intervals, while I operate about 2000 date records for each event. The "Events" table is connected by a many-to-many relationship (through a third table) to the "Date-Time" table. In the tables "Events" and "Date-Time" records are unique. That is, one and the same date can be tied to different events. Date and time are stored in datetime format.
For web application development I use Python, Flask, SQLAlchemy. Jinja2 template engine. DB - PostgreSQL.
I make a query using SQLAlchemy, in it joins I collect tables in a heap and filter by the required date interval.
I give its result to the template engine. In the template engine, 4 (!) nested loops parse this request depending on events, time, date and build a grid of events. Moreover, the request is completely bypassed twice in the uppermost loop and in the lowermost one. In general, everything is bad.
The main problem is that the event grid should only be rendered with html and css, without any JavaScript involved. And at the same time it is updated every 10-15 seconds.
The source file is obtained from 130 to 180 megabytes in size. And about 70% consists of empty lines, indents and other invisible garbage. After pitting django-htmlmin on it, it shrunk to an acceptable 1.5mb. Moreover, minification also requires a decent amount of time.
The main problem in this chain is that generating html with jinja2 takes an unrealistic amount of time.
What can generally be done in this situation? Change the approach directly to solving the problem? If yes, how should it look like?
Well, regarding the generation of html, can you do something here, maybe somehow get away from this terrible cycle pipeline?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
REDkiy, 2018-03-07
@REDkiy

I will answer myself. :-) Two years have already passed after all.
1. Use template expression constructions in the template as follows:
{% - if bla-bla -%}
That is, put a dash after and before the percent sign. This removes blank lines and indents.
2. Minifier to trash. In reality, it only slows down, although if you cache, then in principle you can achieve a win.
3. Parse only very simple queries in the template. Complex and with a large number of nesting levels, turn into JSON and parse with Javascript on the client side.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question