Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python templates for huge HTML/XML

Recently I needed to generate a huge HTML page containing a report with several thousand row table. And, obviously, I did not want to build the whole HTML (or the underlying tree) in memory. As result, I built the page with the old good string interpolation, but I do not like the solution.

Thus, I wonder whether there are Python templating engines that can yield resulting page content by parts.

UPD 1: I am not interested in listing all available frameworks and templating engines. I am interested in templating solutions that I can use separately from any framework and which can yield content by portions instead of building the whole result in memory.

I understand the usability enhancements from partial content loading with client scripting, but that is out of the scope of my current question. Say, I want to generate a huge HTML/XML and stream it into a local file.

like image 877
newtover Avatar asked Dec 28 '22 17:12

newtover


2 Answers

Most popular template engines have a way to generate or write rendered result to file objects with chunks. For example:

  • Template.generate() in Jinja2
  • Template.render_context() in Mako
  • Stream.serialize() in Genshi
like image 97
Denis Otkidach Avatar answered Feb 16 '23 19:02

Denis Otkidach


It'd be more user-friendly (assuming they have javascript enabled) to build the table via javascript by using e.g. a jQuery plugin which allows automatical loading of contents as soon as you scroll down. Then only few rows are loaded initially and when the user scrolls down more rows are loaded on demand.

If that's not a solution, you could use three templates: one for everything before the rows, one for everything after the rows and a third one for the rows. Then you first send the before-rows template, then generate the rows and send them immediately, then the after-rows template. Then you will have only one block/row in memory instead of the whole table.

like image 32
ThiefMaster Avatar answered Feb 16 '23 18:02

ThiefMaster