Can chameleon support block caching? - python

I'm evaluating chameleon as a template renderer for Pyramid.
Some feature quite useful from Django, which also works with mako, is caching.
I couldn't find a similar feature by looking at the documentation, neither with a quick google search.
Is there a similar feature with Chameleon ? If not, how can one deal with potentially long template rendering ?

There is no built-in support for caching the rendered result of a Chameleon template.
There seems to be support for Beaker together with Pyramid. The thread below discusses that implementation.
http://groups.google.com/group/pylons-devel/browse_thread/thread/efe8fd52e6643c47/2675c7098bb32413

Related

fragment caching for jinja2 templates

I‘m trying to improve the rendering performance of my jinja2 templates in the context of Django (changing from the django engine to the jinja2 already gave a significant increase of around 5 times quicker rendering time).
For context, I have a quite large template where major sections stay always the same making them suitable to the idea of caching. Then the other remaining sections within the template will almost always be different to the previous call making them less suitable for caching.
I had the idea of splitting my template so that there is a base template (with the non changing sections) and the child template which extends the base template with the dynamic sections. The idea behind this was that the base template is cached, however I could not find any documentation which would validate this idea.
The other approach I came across is template fragment caching (https://docs.djangoproject.com/en/3.2/topics/cache/#template-fragment-caching) which looked promising, but I was not able to get my head around on how to achieve this result using the jinja2 engine.
I would like to do performance tests for both approaches, so was therefore looking for guidance on how to achieve fragment caching with jinja2 and validation if the base template is cached separately.
Is the base template cached separately when using the extend mechanism in jinja2 templates?
edit: changed to more explicit question following comment

Is there a Javadoc equivalent for Django?

I'm hoping to be able to comment code as I go and then have the Documentation generator produce something similar to a Javadoc hopefully sorted by type (views vs model etc).
Sphinx is good for end-user documentation. However, there's a full PEP standard for docstrings as well. Here's an old but good article on the beauty of inline documentation that's standard in Python:
http://www.electricmonk.nl/log/2008/06/22/why-python-rocks-i-inline-documentation/
Docstrings can go a long way for your development and systems teams, but be sure to check out Sphinx (and https://readthedocs.org) for more. Good luck!
If you're looking for api django documentation, we use django REST swagger where I work, it does its best to generate swagger documentation for as much of your api and models as it can. It will also read formatted comments to better the documentation. Django REST swagger is designed for the rest framework, so it would probably only work for you if you're using django-rest-framework.

Python template system like Smarty or Radius

Is there a Python template library similar to Smarty or Radius (Ruby's Movable Type-like template library) out there?
The python wiki entry on this topic is here: http://wiki.python.org/moin/Templating
The two well-known template systems other than Django are cheetah and jinja.
Django's templating system is not especially powerful, by design, because that discourages any logic other than pure presentation logic in the templates. This is something that I value, having used JSP and ASP.
Jinja is pretty much a superset of Django's templates, except that if you wanted you could embed all of your view code in it ( I wouldn't ).
Cheetah looks rather more like JSP.
Any of these can be used with Django (the full stack framework), or you could use one of the microframeworks or "bundled" frameworks. See this wiki page: http://wiki.python.org/moin/WebFrameworks
AFAIK, Django. It has an excellent templating system.
It's slightly different from PHP, because of the following:
Variables and methods must be passed in to the template renderer.
Variables and methods are noted by {{ braces }}.
Tags (Django's version of PHP flow control statements), are denoted like {% if x %}, followed by a loop termination (like {% endif %}.
You can call functions directly from the template, but they will not accept any arguments.
There's a lot more, but I would highly recommend that you read the Django book.
Just one note: from personal experience, Django's ORM isn't very good for legacy database integration, so if you're looking for that, you might want to try SQLalchemy.
EDIT: Marcin had a good summary - Django's templating system, by design, encourages the separation of presentation and processing logic (i.e., loose coupling).
EDIT 2: There's also mako, which has a more PHP-like syntax.

Pyramid: simpleform or deform?

For a new (Python) web application with the Pyramid web framework, I'd like to use a form binding and validation library and so far found simpleform and deform. Does anyone have experience with these, and can tell me why I should pick one or the other? I am not using an ORM, just POPO's so to say.
I think I would prefer the easiest for now.
I've not had extensive experience with either, but so far this is what I've learned.
They both use colander (which I very much like) for definition and validation of forms. In my opinion what really sets them apart is their rendering mechanisms. In this regard, deform is the most straightforward in the sense that it allows you render the whole form by just doing form.render() in your template. On the other hand, with simpleform you must render each field manually. This could be either a good or bad thing depending on what you need.
A drawback with simpleform is currently there is no clear way to handle sequence schemas in templates.
edit: Also, in my opinion, deform has better documentation available.
I haven't used simpleform yet, but I have been using deform for a project I'm currently working on. deform allows you to render templates from a colander schema, which is very handy. Also, if the schema is violated you can simply call ValidationFailure.render() (after catching the ValidationFailure exception) and a message that you can customize is rendered with the form. I'm currently grappling with the choice between rendering the entire form and rendering it piece by piece. It would be really nice if you could group components together for rendering.
Though it's a third alternative, but have you considered ToscaWidgets2?
From a quick glance on simpleform and deform, it seems to me that Toscawidgets2 is the golde middle between those two in case of features and simplicity.
There's even a tutorial for using it with Pyramid, just drop the database part and supply the form values as a dict.
For your information, deform is used by :
Kotti : http://kotti.readthedocs.org/en/latest/
Substance D : https://substanced.readthedocs.org/en/latest/

Untrusted templates in Python - what is a safe library to use?

I am building a library that will be used in several Python applications. It get multilingual e-mail templates from an RMDBS, and then variable replacement will be performed on the template in Python before the e-mail is sent.
In addition to variable replacement, I need the template library to support if, elif, and for statements in the templates.
I use Mako for most my projects, and also looked at Tempita as it doesn't provide a lot of features I don't need.
The concern I have is untrusted code execution - can someone point me at a template solution for Python that either does not support code execution, or will allow me to disable it?
From the Django book:
For that reason, it’s impossible to call Python code directly within Django templates. All “programming” is fundamentally limited to the scope of what template tags can do. It is possible to write custom template tags that do arbitrary things, but the out-of-the-box Django template tags intentionally do not allow for arbitrary Python code execution.
Give Django templates a try. It's a little tricky to set up outside of a Django app -- something to do with DJANGO_SETTINGS_MODULE, search around -- but may be trusted.
Have you checked out Jinja2? It's pretty much what you're talking about, and it's a great mix of powerful while keeping things simple and not giving the designer too much power. :)
If you've used Django's template system, it's very similar (if not based off of?) Jinja.

Categories

Resources