Django. Jinja2. Aymeric Augustin DjangoCong 2016

Similar documents
django-gollum Documentation

Let s Talk About Templates. Armin

LECTURE 14. Web Frameworks

The Django Web Framework Part II. Hamid Zarrabi-Zadeh Web Programming Fall 2013

Flask Web Development Course Catalog

django-notifier Documentation

Django Debug Toolbar Documentation

Django_template3d Documentation

Django-Select2 Documentation. Nirupam Biswas

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

nacelle Documentation

django-subdomains Documentation

CSE 115. Introduction to Computer Science I

LECTURE 14. Web Frameworks

django-xross Documentation

Django with Python Course Catalog

Django Test Utils Documentation

Django QR Code Documentation

Building Web Applications

Django: Views, Templates, and Sessions

django-debreach Documentation

django-renderit Documentation

HOW TO FLASK. And a very short intro to web development and databases

welcome to BOILERCAMP HOW TO WEB DEV

wagtailmenus Documentation

CIS192 Python Programming

Amyyon customers can t wait to get their hands on it s new application, developed in Uniface.

Django urls Django Girls Tutorial

The Pyramid Web Application Development Framework

"Stupid Easy" Scaling Tweaks and Settings. AKA Scaling for the Lazy

Django Extra Views Documentation

django-sticky-uploads Documentation

CS61A Notes Week 13: Interpreters

django-contact-form Documentation

Unifer Documentation. Release V1.0. Matthew S

flask-jwt-simple Documentation

These are notes for the third lecture; if statements and loops.

django-stored-messages Documentation

Easy-select2 Documentation

django-baton Documentation

Django-CSP Documentation

Pluggable Patterns. For Reusable Django Applications

micawber Documentation

Application Design and Development: October 30

Bricks Documentation. Release 1.0. Germano Guerrini

TailorDev Contact Documentation

django-baton Documentation

Web Security IV: Cross-Site Attacks

Building a Django Twilio Programmable Chat Application

alphafilter Documentation

Week - 01 Lecture - 04 Downloading and installing Python

django-scaffold Documentation

DJOAuth2 Documentation

django-ad-code Documentation

web.py Tutorial Tom Kelliher, CS 317 This tutorial is the tutorial from the web.py web site, with a few revisions for our local environment.

CS1520 Recitation: Security in Flask

Lesson: Web Programming(6) Omid Jafarinezhad Sharif University of Technology

Release 0.8. Repoze Developers

To Kill a Monolith: Slaying the Demons of a Monolith with Node.js Microservices on CloudFoundry. Tony Erwin,

Django Better Cache Documentation

Programming Robots with ROS, Morgan Quigley, Brian Gerkey & William D. Smart

django-celery Documentation

wagtailmenus Documentation

Intro. Scheme Basics. scm> 5 5. scm>

EXPERIENCES MOVING FROM DJANGO TO FLASK

Q2D8T#YH# BYUQTUQ+ *")#+,-./,01#2../,345,67#869,3#47:#+5;<35<;=!"""""""""""# $& B675;6//=;#CD/645E!

Simple AngularJS thanks to Best Practices

29-27 May 2013 CERN WEB FRAMEWORKS. Adrian Mönnich

CIS192 Python Programming

Web 2.0 and AJAX Security. OWASP Montgomery. August 21 st, 2007

django-cms-search Documentation

wagtailtrans Documentation

CSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern

HTML 5: Fact and Fiction Nathaniel T. Schutta

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Analysis of Hypertext Isolation Techniques for Cross-site Scripting Prevention. Mike Ter Louw Prithvi Bisht V.N. Venkatakrishnan

BOOSTING THE SECURITY OF YOUR ANGULAR 2 APPLICATION

Cascading Style Sheets for layout II CS7026

Web Security: Vulnerabilities & Attacks

Django Wordpress API Documentation

django-amp-tools Documentation Release latest

gocept.selenium Release 3.0

django-ratelimit-backend Documentation

django-dajaxice Documentation

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

Client Side JavaScript and AJAX

Wakanda Architecture. Wakanda is made up of three main components: Wakanda Server Wakanda Studio Wakanda Client Framework

preppy Documentation Release ReportLab

4. Java Project Design, Input Methods

django-embed-video Documentation

Consolidation and Review

Django Part II SPARCS 11 undead. Greatly Inspired by SPARCS 10 hodduc

Senior Technical Specialist, IBM. Charles Price (Primary) Advisory Software Engineer, IBM. Matthias Falkenberg DX Development Team Lead, IBM

Accelerating Information Technology Innovation

Django File Picker Documentation

django-report-tools Documentation

django-facebook-graph Documentation

django-oauth2-provider Documentation

Lotus IT Hub. Module-1: Python Foundation (Mandatory)

django-precise-bbcode Documentation

Transcription:

Django Jinja2 Aymeric Augustin DjangoCong 2016 Jardin des Plantes, Avranches, 9 avril 2016

I m Aymeric Amalfi Core Developer since 2011 Chief Technical Officer since 2015 Time zones Python 3 Transactions App loading Jinja2 First peer-to-peer insurance broker in France We re hiring! 2

Why Jinja2? Aren t Django templates just fine? 3

Jinja2 is fast. 10-20x faster than Django templates. 4 Image from http://www.gentside.com/dragster/wallpaper

Django templates are slow. 10-20x slower than Jinja2. 5 Image from https://commons.wikimedia.org/wiki/file:grand_bi_sur_la_terrasse_dufferin_vers_1900.jpg

Let s try it on the fractalideas.com home page! >>> import statistics, timeit >>> setup = \... "from django.template.loader import render_to_string" >>> round(statistics.median(timeit.repeat(... "render_to_string('home.html', using='django')",... setup, repeat=10, number=1000)), 2) 1.18 # ms >>> round(statistics.median(timeit.repeat(... "render_to_string('home.html', using='jinja2')",... setup, repeat=10, number=1000)), 2) 0.35 # ms 6

Let s try it on the fractalideas.com home page! More than three times faster! Not even a millisecond faster. Page loads 0.004% faster with a cold cache. 7

Let s try it on the fractalideas.com home page! More than three times faster! Not even a millisecond faster. Page loads 0.02% faster with a warm cache. 8

Template rendering speed (often) doesn t matter Light content? Anything is fast enough Heavy static content? Pre-render & cache it Heavy dynamic content? Render in the browser If you re thinking about switching template languages, consider both server-side and client-side options (React) Until you ve optimized everything else, templates are unlikely to be a bottleneck 9

Why Jinja2? Aren t Django templates just fine? 10

Jinja2 is a modern and designer-friendly templating language for Python, modelled after Django s templates. http://jinja.pocoo.org/docs/ 11

Advantages of Jinja2 More comfortable for developers Django expects templates to be written by designers Easier to customize Try writing a Django template tag without the docs Meaningfully faster in some use cases Template-based widget rendering (#15667) 12

Drawbacks of Jinja2 Less common than Django templates Many authors of pluggable apps don t know where to start (yet) Targets experienced developers Docs suggest handling escaping manually for performance Scoping behavior for included blocks is declarative etc. 13

Features of Jinja2 from its docs homepage sandboxed execution powerful automatic HTML escaping system for XSS prevention template inheritance compiles down to the optimal python code just in time optional ahead-of-time template compilation easy to debug ( ) configurable syntax sounds important (to me) 14

Jinja2 is a templating language for Python designed for experienced developers, modelled after Django s templates. paraphrased from http://jinja.pocoo.org/docs/ 15

Third-party solutions Coffin (2008 - reborn in 2015) Alternative APIs such as render() Jingo (2010) Template loader (class-based API since Django 1.2) Django-Jinja (2012) More opinionated and full-featured template loader 16

Templates in Django < 1.8 Discussed for historical purposes only these versions are unsupported! 17

Django < 1.8 - Rendering a template # django/template/loaders.py (simplified) def render_to_string(template_name, dictionary): template = get_template(template_name, dirs) context = Context(dictionary) return template.render(context) 18

Django < 1.8 - Rendering a template # django/template/loaders.py (simplified) def get_template(template_name): template, origin = find_template(template_name) return Template(template, origin, template_name) 19

Django < 1.8 - Rendering a template # django/template/loaders.py (simplified) def find_template(name, dirs=none): global template_source_loaders if template_source_loaders is None:... # init from settings.template_loaders for loader in template_source_loaders:... # try loading with loader(name) raise TemplateDoesNotExist(name) global configuration 20

Django < 1.8 - Actually rendering a template # django/shortcuts.py (drastically simplified) def render(request, template_name, dictionary): template = get_template(template_name, dirs) context = RequestContext(request, dictionary) return HttpResponse(template.render(context)) strong coupling 21

Context processors are a poor API How many context processors depend on request? Aside from the request context processor, that is. How hard is it to make a context processor lazy? In order to avoid overhead in templates that don t need it. Just an API for {{ global_func(request) }}? Django templates cannot call functions with arguments. 22

Multiple templates engines in Django 1.8 If templates weren t boring, they d have been fixed earlier. 23

24

25

26

Plan 1. Write a Django Enhancement Proposal (DEP) Lots of reading; some writing 2. Refactor Django templates as a library (optional) Actually not optional 3. Implement the DEP Sounds easy 27

Concept django.template.* django.template.loader d.t.backends.django django.template.* d.t.backends.jinja2 jinja2 28

this project avoids encoding the legacy of the Django template language in APIs DEP 182 29

Jinja2 for Djangonauts Wake up this is the just do this section. 30

Install Jinja2 $ pip install jinja2 31

Define a Jinja2 environment # project/jinja2.py from jinja2 import Environment def environment(**options): options.setdefault('extensions', []).extend([...]) env = Environment(**options) env.filters.update({... }) env.globals.update({... }) env.tests.update({... }) return env 32

Install translations (optional) # project/jinja2.py from django.utils import translation def environment(**options): #... env.install_gettext_translations(translation, newstyle=true) #... return env 33

Point the TEMPLATES setting to the environment TEMPLATES = [{ 'BACKEND': 'django.template.backends' '.jinja2.jinja2', 'DIRS': [os.path.join(base_dir, 'templates')], 'OPTIONS': { 'environment': 'project.jinja2.environment', }, }, { 'BACKEND': 'django.template.backends' '.django.djangotemplates', 'APP_DIRS': True, }] 34

Globals # project/jinja2.py from django.core.urlresolvers import reverse env.globals.update({ }) 'url': reverse, # templates/header.html {{ url('account_login') }} 35

Filters # project/jinja2.py from urllib.parse import quote env.filters.update({ }) 'urlquote': quote, # templates/header.html?next={{ request.get_full_path() urlquote }} 36

Tests # project/jinja2.py from django.utils.timezone import is_aware env.tests.update({ }) 'aware': is_aware, # templates/datetime.html {% if at is aware %}{{ at.tzname() }}{% endif %} 37

Extensions # project/jinja2.py options.setdefault('extensions', []).extend([ 'jinja2.ext.i18n', ]) # or, alternatively: env.add_extension('jinja2.ext.i18n') # templates/header.html {{ gettext("log in") }} 38

Jinja2 tips You can go back to sleep the slides will be online. 39

Calling methods # Django templates {{ qotd }} # Jinja2 {{ qotd() }} 40

Inserting the CSRF token in a form # Django templates {% csrf_token %} # Jinja2 {{ csrf_input }} # Both <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}" /> 41

Replacing a context processor # project/jinja2.py from django.contrib.messages import get_messages env.globals.update({ }) 'get_messages': get_messages, 42

Replacing a context processor # templates/header.html {% for message in get_messages(request) %} <div>{{ message }}</div> {% endfor %} 43

Replacing a context processor - alternative # project/jinja2.py from django.contrib.messages import get_messages @jinja2.contextfunction def _get_messages(context): return get_messages(context['request']) env.globals.update({ }) 'get_messages': _get_messages, 44

Replacing a context processor - alternative # templates/header.html {% for message in get_messages() %} <div>{{ message }}</div> {% endfor %} 45

Replacing a template tag # spam/templatetags.py from django import template register = template.library() @register.simple_tag(takes_context=true) def eat_spam(context, arg1, arg2): #... return html 46

Replacing a template tag # templates/spam.html {% load spam %} {% eat_spam arg1 arg2 %} 47

Replacing a template tag # spam/jinja2.py @jinja2.contextfunction def eat_spam(context, arg1, arg2): #... return html # and register it in env.globals as shown above. 48

Replacing a template tag # templates/spam.html {{ eat_spam(arg1, arg2) }} 49

Most extensions only add globals # eat/jinja2.py class EatExtension(Extension): def init (self, env): env.globals['eat'] = {'spam': eat_spam} # templates/eat.html {{ eat.spam() }} 50

Thank you! Questions? Estuaire de la Sée, Avranches, 9 avril 2016