Rendering¶
Rendering content.
This document explains content generation with the Pyjoyment
renderer.
Concepts¶
Essentials every Pyjoyment developer should know.
Renderer¶
The renderer is a tiny black box turning stash data into actual responses utilizing multiple template systems and data encoding modules.
{'text': 'Hello.'} -> 200 OK, text/html, 'Hello.'
{'json': {'x': 3}} -> 200 OK, application/json, '{"x":3}'
{'text': 'Oops.', 'status': '410'} -> 410 Gone, text/html, 'Oops.'
Templates can be automatically detected if enough information is provided by
the developer or routes. Template names are expected to follow the
template.format.handler scheme, with template defaulting to
controller/action or the route name, format defaulting to html
and handler to ep.
{'controller': 'users', 'action': 'list'} -> 'users/list.html.ep'
{'template': 'foo', 'format': 'txt'} -> 'foo.txt.ep'
{'template': 'foo', 'handler': 'epl'} -> 'foo.html.epl'
The controller value gets converted from CamelCase to snake_case
using decamelize() and - characters replaced with
/.
{'controller': 'My.Users', 'action': 'add'} -> 'my/users/add.html.ep'
{'controller': 'my-users', 'action': 'show'} -> 'my/users/show.html.ep'
All templates should be in the templates directories of the application,
which can be customized with
paths, or one of the
__DATA__ sections from
classes.
"""
__DATA__
@@ clock.html.ep
% import time
% now = time.localtime()
<!DOCTYPE html>
<html>
<head><title>Time</title></head>
<body>The time is <%= time.strftime('%H:%M:%S', now) %>.</body>
</html>
@@ hello.txt.ep
Hello <%= name %>.
"""
from Pyjoyment.Lite import *
get('/clock')
get('/hello', {'format': 'txt', 'name': 'World'})
app.start()
The renderer can be easily extended to support additional template systems with plugins, but more about that later.
Embedded Python¶
Pyjoyment includes a minimalistic but very powerful template system
out of the box called Embedded Python or ep for short. It is based on
Pyjo.Template and allows the embedding of Python code right into
actual content using a small set of special tags and line start characters.
<% Python code %>
<%= Python expression, replaced with XML escaped result %>
<%== Python expression, replaced with result %>
<%# Comment, useful for debugging %>
<%% Replaced with "<%", useful for generating templates %>
% Python code line, treated as "<% line =%>" (explained later)
%= Python expression line, treated as "<%= line %>"
%== Python expression line, treated as "<%== line %>"
%# Comment line, useful for debugging
%% Replaced with "%", useful for generating templates
Tags and lines work pretty much the same, but depending on context one will
usually look a bit better. A line that ends with : opens a block, which
end closes, as the indentation of a template does not matter.
<% i = 10 %>
<ul>
<% for j in range(1, i + 1): %>
<li>
<%= j %>
</li>
<% end %>
</ul>
% i = 10
<ul>
% for j in range(1, i + 1):
<li>
%= j
</li>
% end
</ul>
Aside from differences in whitespace handling, both examples generate similar Python code, a naive translation could look like this.
output = ''
i = 10
output += '<ul>'
for j in range(1, i + 1):
output += '<li>'
output += xml_escape(str(j))
output += '</li>'
output += '</ul>'
return output
An additional equal sign can be used to disable escaping of the characters
<, >, &, ' and " in results from Python expressions,
which is the default to prevent XSS attacks against your application.
<%= 'I ♥ Pyjoyment!' %>
<%== '<p>I ♥ Pyjoyment!</p>' %>
Only markup, an object with the method __html__, as
Pyjo.String.HTML, is excluded from automatic escaping.
% from Pyjo.String.HTML import html
<%= html('<p>I ♥ Pyjoyment!</p>') %>
Whitespace characters around tags can be trimmed by adding an additional equal sign to the end of a tag.
<% for i in range(1, 4): %>
<%= 'Trim all whitespace characters around this expression' =%>
<% end %>
Newline characters can be escaped with a backslash.
This is <%= 1 + 1 %> a\
single line
And a backslash in front of a newline character can be escaped with another backslash.
This will <%= 1 + 1 %> result\\
in multiple\\
lines
A newline character gets appended automatically to every template, unless the last character is a backslash. And empty lines at the end of a template are ignored.
There is <%= 1 + 1 %> no newline at the end here\
At the beginning of the template, stash values that are valid names of
Python get automatically initialized as normal variables, and the controller
object as both self and c.
c.stash(name='tester')
Hello <%= name %> from <%= c.tx.remote_address %>.
A prefix like myapp.* is commonly used for stash values that you don’t
want to expose in templates.
c.stash({'myapp.name': 'tester'})
There are also many helper functions available, but more about that later.
<%= dumper({'foo': 'bar'}) %>
Basics¶
Most commonly used features every Pyjoyment developer should know
about.
Automatic rendering¶
The renderer can be manually started by calling the method
render(), but that’s
usually not necessary, because it will get automatically called if nothing
has been rendered after the router finished its work. This also means you can
have routes pointing only to templates without actual actions.
c.render()
There is one big difference though, by calling it manually you can make sure
that templates use the current controller object, and not the default
controller specified with the attribute
controller_class.
c.render_later()
You can also disable automatic rendering with the method
render_later(), which can
be very useful to delay rendering when a non-blocking operation has to be
performed first.
Rendering templates¶
The renderer will always try to detect the right template, but you can also
use the template stash value to render a specific one. Everything before
the last slash will be interpreted as the subdirectory path in which to find
the template.
# foo/bar/baz.*.*
c.render(template='foo/bar/baz')
Choosing a specific format and handler is just as easy.
# foo/bar/baz.txt.epl
c.render(template='foo/bar/baz', format='txt', handler='epl')
Because rendering a specific template is the most common task it also has a shortcut.
c.render('foo/bar/baz')
If you’re not sure in advance if a template actually exists, you can also use
the method render_maybe()
to try multiple alternatives.
c.render_maybe('localized/baz') or c.render('foo/bar/baz')
Rendering to strings¶
Sometimes you might want to use the rendered result directly instead of
generating a response, for example, to send emails, this can be done with
render_to_string().
html = c.render_to_string('mail')
No encoding will be performed, making it easy to reuse the result in other templates or to generate binary data.
pdf = c.render_to_string('invoice', format='pdf')
c.render(data=pdf, format='pdf')
All arguments passed will get localized automatically and are only available during this render operation.
Template variants¶
To make your application look great on many different devices you can also
use the variant stash value to choose between different variants of your
templates.
# foo/bar/baz.html+phone.ep
# foo/bar/baz.html.ep
c.render('foo/bar/baz', variant='phone')
This can be done very liberally since it only applies when a template with the correct name actually exists and falls back to the generic one otherwise.
Rendering inline templates¶
Some renderers such as ep allow templates to be passed inline.
c.render(inline='The result is <%= 1 + 1 %>.')
Since auto-detection depends on a path you might have to supply a
handler too.
c.render(inline="<%= args[0].param('foo') %>", handler='epl')
Rendering text¶
Characters can be rendered to bytes with the text stash value, the given
content will be automatically encoded with
encoding.
c.render(text='I ♥ Pyjoyment!')
Rendering data¶
Bytes can be rendered with the data stash value, no encoding will be
performed.
c.render(data=data)
Rendering JSON¶
The json stash value allows you to pass Python data structures to the
renderer which get directly encoded to JSON with Pyjo.JSON.
c.render(json={'foo': [1, 'test', 3]})
Status code¶
Response status codes can be changed with the status stash value.
c.render(text='Oops.', status=500)
Content type¶
The Content-Type header of the response is actually based on the MIME
type mapping of the format stash value.
# Content-Type: text/plain
c.render(text='Hello.', format='txt')
# Content-Type: image/png
c.render(data=data, format='png')
These mappings can be easily extended or changed with
types.
# Add new MIME type
app.types.type('md', 'text/markdown')
Stash data¶
Any of the native Python data types can be passed to templates through the
stash().
c.stash(description='web framework')
c.stash(frameworks=['Catalyst', 'Mojolicious', 'Pyjoyment'])
c.stash(spinoffs={'minion': 'job queue'})
%= description
%= frameworks[1]
%= spinoffs['minion']
Since everything is just Python normal control structures just work.
% for framework in frameworks:
<%= framework %> is a <%= description %>.
% end
% if (description := spinoffs.get('minion')):
Minion is a <%= description %>.
% end
For templates that might get rendered in different ways and where you’re not
sure if a stash value will actually be set, you can just use the helper
stash of Pyjoyment.Plugin.DefaultHelpers.
% if (spinoffs := stash('spinoffs')):
Minion is a <%= spinoffs['minion'] %>.
% end
Helpers¶
Helpers are little functions you can use in templates as well as application and controller code.
# Template
%= dumper([1, 2, 3])
# Application
serialized = app.dumper([1, 2, 3])
# Controller
serialized = c.dumper([1, 2, 3])
We differentiate between default helpers, which are more general purpose like
dumper of Pyjoyment.Plugin.DefaultHelpers, and tag helpers like
link_to of Pyjoyment.Plugin.TagHelpers, which are template
specific and mostly used to generate HTML tags.
%= link_to('Pyjoyment', 'https://pyjoyment.readthedocs.io')
In controllers you can also use the attribute
helpers to fully qualify
helper calls and ensure that they don’t conflict with existing methods you
may already have.
serialized = c.helpers.dumper([1, 2, 3])
A list of all built-in helpers can be found in
Pyjoyment.Plugin.DefaultHelpers and
Pyjoyment.Plugin.TagHelpers.
Static files¶
Static files are automatically served from the public directories of the
application, which can be customized with
paths, or one of the __DATA__
sections from classes. And if
that’s not enough you can also serve them manually with the helpers
reply.static and reply.file of
Pyjoyment.Plugin.DefaultHelpers.
from Pyjoyment.Lite import *
@get('/')
def index(c):
c.reply.static('index.html')
@get('/some_download')
def some_download(c):
c.res.headers.content_disposition = 'attachment; filename=bar.png;'
c.reply.static('foo/bar.png')
@get('/leak')
def leak(c):
c.reply.file('/etc/passwd')
app.start()
By default static files do not use a prefix and are simply served with a
higher precedence than routes. This can be changed with
prefix.
# Serve static files only with a "/static" prefix
app.static.prefix = '/static'
To allow for the prefix to be configured dynamically, you can use
url_for_file() to generate
URLs with it. Many tag helpers will also use the prefix automatically when it
makes sense.
Static assets¶
While Pyjoyment does not have any special support for frontend frameworks
like Vue.js and React, the
public/assets directory is reserved for static assets created by
bundlers like Webpack and Rollup.js ahead of time. Asset files can be of any type, they
just have to follow the [name].[checksum].[extensions] naming scheme,
like myapp.ab1234cd5678ef.js. You can then use
url_for_asset() or the
helper asset_tag of Pyjoyment.Plugin.TagHelpers to generate URLs
without having to know the checksum.
# "/assets/myapp.ab1234cd5678ef.js"
c.url_for_asset('/myapp.js')
# "<script src="/assets/myapp.ab1234cd5678ef.js"></script>"
c.asset_tag('/myapp.js')
If your application runs in development mode, all assets will be served
with a Cache-Control: no-cache header, to speed up development by
preventing browser caching. Additionally all assets following the
[name].development.[extensions] naming scheme, like
myapp.development.js, have a higher precedence than assets with
checksums. That way you can just overwrite your assets during development,
instead of having to manually delete them each time they are rebuilt with a
different checksum.
# "/assets/foo/bar.development.js"
c.url_for_asset('/foo/bar.js')
Webpack configuration example
(webpack.config.js):
import Path from '@mojojs/path';
const isDev = process.env.PYJO_MODE === 'development';
export default {
output: {
filename: isDev ? '[name].development.js' : '[name].[chunkhash].js',
path: Path.currentFile().sibling('public', 'assets').toString(),
publicPath: ''
},
// Add your own rules and entry point here
};
Rollup configuration
example (rollup.config.js):
import Path from '@mojojs/path';
const isDev = process.env.PYJO_MODE === 'development';
export default {
output: {
entryFileNames: isDev ? '[name].development.[ext]' : '[name].[hash].[ext]',
dir: Path.currentFile().sibling('public', 'assets').toString(),
format: 'iife'
},
// Add your own rules and entry point here
};
Vite configuration example
(vite.config.js):
import Path from '@mojojs/path';
const isDev = process.env.PYJO_MODE === 'development';
export default {
// Pyjoyment already serves the "public" directory
publicDir: false,
build: {
outDir: Path.currentFile().sibling('public', 'assets').toString(),
rollupOptions: {
output: {
entryFileNames: isDev ? '[name].development.js' : '[name].[hash].js',
assetFileNames: isDev ? '[name].development.[ext]' : '[name].[hash].[ext]'
}
}
},
// Add your own plugins and entry point here
};
During development you can let Pyjo.Server.Reload start your bundler
in watch mode, so it rebuilds assets whenever their sources change. The
PYJO_MODE environment variable will be set for it too, and because assets
are found again for every request in development mode and not cached by
browsers, there is no need to restart your application.
$ pyjo-reload -x 'npx webpack --watch' ./myapp.py
$ pyjo-reload -x 'npx rollup -c -w' ./myapp.py
$ pyjo-reload -x 'npx vite build --watch' ./myapp.py
For production you just run the bundler once before deploying your
application, with PYJO_MODE set accordingly.
$ PYJO_MODE=production npx webpack
Everything else is up to your bundler of choice, so you need to consult its
documentation for further information. And where you keep your asset
sources, such as .vue and .jsx files, is not important, as long as
your bundler can find them. Using a directory named assets or
frontend in your application root directory is a good best practice
though.
Static files are served with Last-Modified and ETag headers, so that
browsers revalidate them, and with the Cache-Control header of
cache_control if you set one. The
files whose paths match
immutable, like the assets with a
checksum in their names, which never change, are cached by browsers for a year
without revalidation, except in development mode.
# Revalidate static files after a minute, and never the assets with checksums
app.static.cache_control = 'public, max-age=60'
app.static.immutable = r'^assets/.+\.[0-9a-f]{8,}\.[^/]+$'
Precompressed static files¶
Static files are served as they are, the
compress of the renderer only
compresses content that is generated dynamically. Files that are built ahead
of time, like the assets of a bundler, can be compressed ahead of time too,
with the highest levels of compression, which would be too slow for every
request. A variant next to the file, with the extension of its encoding in
encodings, like app.css.br,
app.css.zst and app.css.gz, is served to the clients that accept its
encoding, with a Content-Encoding header and the Content-Type of the
original file.
$ find public -type f -size +1k \( -name '*.css' -o -name '*.js' -o -name '*.svg' \) \
-exec gzip -k -f -9 -n {} + -exec brotli -k -f -q 11 {} + -exec zstd -q -k -f -19 {} +
Compress the formats of text, which shrink a lot: CSS, JavaScript, JSON, source maps, SVG, HTML, plain text, XML, WebAssembly, and fonts in TrueType and OpenType. Leave out the formats that are compressed already, as the PNG, JPEG, GIF, WebP and AVIF images, video, audio, the WOFF and WOFF2 fonts, PDF documents and archives, whose variants would only be larger, and files of less than about a kilobyte, which gain nothing. A variant that is not smaller than its file is better removed.
Variants are preferred in the order of
encodings and checked for every
request, so there is nothing to restart when they change. A variant older
than its file is ignored, so that a build that changes a file without
compressing it again does not serve the old content, and a Range request
always gets a part of the original file. Every response of a file with
variants has a Vary: Accept-Encoding header, so that caches keep them
apart, and each variant has an ETag of its own.
# Look for gzip variants only
app.static.encodings = {'gzip': '.gz'}
# Serve static files only as they are
app.static.encodings = {}
The Dockerfile of
Pyjoyment.Command.Author.generate.dockerfile writes the variants
in the image with --compress, for the formats of text or the patterns of
--compress-pattern: gzip of the image, and brotli and zstd if
the packages of --apt-package install them.
$ ./myapp.py generate dockerfile --compress public
$ ./myapp.py generate dockerfile --apt-package brotli --apt-package zstd --compress public
The ZIP of the function of
Pyjoyment.Command.Author.generate.lambda_zip has them too with
--compress: gzip of the standard library, zstd of the one of
Python 3.14 or of the command zstd, and brotli of its command, on the
machine that builds it.
$ ./myapp.py generate lambda-zip --compress public
With --compress-encoding, both generators write the variants of the
encodings that the clients get only, as br and gzip behind CloudFront,
which forwards no other encoding of Accept-Encoding to the application.
$ ./myapp.py generate lambda-zip --compress public --compress-encoding br --compress-encoding gzip
Content negotiation¶
For resources with different representations and that require truly RESTful
content negotiation you can also use the helper respond_to of
Pyjoyment.Plugin.DefaultHelpers instead of
render().
# /hello (Accept: application/json) -> "json"
# /hello (Accept: application/xml) -> "xml"
# /hello.json -> "json"
# /hello.xml -> "xml"
# /hello?_format=json -> "json"
# /hello?_format=xml -> "xml"
c.respond_to(json={'json': {'hello': 'world'}}, xml={'text': '<hello>world</hello>'})
The best possible representation will be automatically selected from the
_format GET/POST parameter, format stash value or Accept
request header and stored in the format stash value. To change MIME type
mappings for the Accept request header or the Content-Type response
header you can use types.
def html(c):
c.content_for('head', '<meta name="author" content="sri">')
c.render(template='hello', message='world')
c.respond_to(json={'json': {'hello': 'world'}}, html=html)
Callbacks can be used for representations that are too complex to fit into a single render call.
# /hello (Accept: application/json) -> "json"
# /hello (Accept: text/html) -> "html"
# /hello (Accept: image/png) -> "any"
# /hello.json -> "json"
# /hello.html -> "html"
# /hello.png -> "any"
# /hello?_format=json -> "json"
# /hello?_format=html -> "html"
# /hello?_format=png -> "any"
c.respond_to(
json={'json': {'hello': 'world'}},
html={'template': 'hello', 'message': 'world'},
any={'text': '', 'status': 204},
)
And if no viable representation could be found, the any fallback will be
used or an empty 204 response rendered automatically.
# /hello -> "html"
# /hello (Accept: text/html) -> "html"
# /hello (Accept: text/xml) -> "xml"
# /hello (Accept: text/plain) -> None
# /hello.html -> "html"
# /hello.xml -> "xml"
# /hello.txt -> None
# /hello?_format=html -> "html"
# /hello?_format=xml -> "xml"
# /hello?_format=txt -> None
if format := c.accepts('html', 'xml'):
...
For even more advanced negotiation logic you can also use the helper
accepts of Pyjoyment.Plugin.DefaultHelpers.
Rendering exception and not_found pages¶
By now you’ve probably already encountered the built-in 404 (Not Found)
and 500 (Server Error) pages, that get rendered automatically when you
make a mistake. Those are fallbacks for when your own exception handling
fails, which can be especially helpful during development. You can also
render them manually with the helpers reply.exception and
reply.not_found of Pyjoyment.Plugin.DefaultHelpers.
from Pyjoyment.Lite import *
def looks_like_number(value):
try:
float(value)
except ValueError:
return False
return True
@get('/divide/:dividend/by/:divisor')
def divide(c):
dividend = c.param('dividend')
divisor = c.param('divisor')
# 404
if not looks_like_number(dividend) or not looks_like_number(divisor):
return c.reply.not_found()
# 500
if float(divisor) == 0:
return c.reply.exception('Division by zero!')
# 200
c.render(text=f'{float(dividend) / float(divisor):g}')
app.start()
You can change the templates of those pages, since you most likely want to
show your users something more closely related to your application in
production. The renderer will always try to find
exception.$mode.$format.* or not_found.$mode.$format.* before
falling back to the built-in default templates.
"""
__DATA__
@@ exception.production.html.ep
<!DOCTYPE html>
<html>
<head><title>Server error</title></head>
<body>
<h1>Exception</h1>
<p><%= exception.message %></p>
<h1>Stash</h1>
<pre><%= dumper(snapshot) %></pre>
</body>
</html>
"""
from Pyjoyment.Lite import *
@get('/dies')
def dies(c):
raise Exception('Intentional error')
app.start()
The default exception format is html, but that can be changed at
application and controller level. By default there are handlers for
html, txt and json available. There are also various exception
helpers in Pyjoyment.Plugin.DefaultHelpers for you to overload to
change the default behavior.
from Pyjoyment.Lite import *
app.exception_format = 'json'
@get('/json')
def json(c):
raise Exception('Just a test')
@get('/txt')
def txt(c):
c.exception_format('txt')
raise Exception('Just a test')
app.start()
The hook before_render of hook() makes
even more advanced customizations possible by allowing you to intercept and
modify the arguments passed to the renderer.
from Pyjoyment.Lite import *
@hook('before_render')
def before_render(c, args):
# Make sure we are rendering the exception template
if (template := args.get('template')) is None:
return
if template != 'exception':
return
# Switch to JSON rendering if content negotiation allows it
if not c.accepts('json'):
return
args['json'] = {'exception': str(c.stash('exception'))}
@get('/')
def index(c):
raise Exception('This should not happen!')
app.start()
Layouts¶
Most of the time when using ep templates you will want to wrap your
generated content in an HTML skeleton, thanks to layouts that’s absolutely
trivial.
"""
__DATA__
@@ foo/bar.html.ep
% layout('mylayout')
Hello World!
@@ layouts/mylayout.html.ep
<!DOCTYPE html>
<html>
<head><title>MyApp</title></head>
<body><%= content() %></body>
</html>
"""
from Pyjoyment.Lite import *
get('/', {'template': 'foo/bar'})
app.start()
You just select the right layout template with the helper layout and
place the result of the current template with the helper content of
Pyjoyment.Plugin.DefaultHelpers. You can also pass along normal stash
values to the layout helper.
"""
__DATA__
@@ foo/bar.html.ep
% layout('mylayout', title='Hi there')
Hello World!
@@ layouts/mylayout.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content() %></body>
</html>
"""
from Pyjoyment.Lite import *
get('/', {'template': 'foo/bar'})
app.start()
Instead of the layout helper you could also just use the layout stash
value, or call render()
with the layout argument.
c.render(template='mytemplate', layout='mylayout')
To set a layout stash value application-wide you can use
defaults().
app.defaults(layout='mylayout')
Layouts can also be used with
render_to_string(), but the
layout value needs to be passed as a render argument (not a stash value).
html = c.render_to_string('reminder', layout='mail')
Partial templates¶
You can break up bigger templates into smaller, more manageable chunks. These
partial templates can also be shared with other templates. Just use the
helper include of Pyjoyment.Plugin.DefaultHelpers to include one
template into another.
"""
__DATA__
@@ foo/bar.html.ep
<!DOCTYPE html>
<html>
%= include('_header', title='Howdy')
<body>Bar</body>
</html>
@@ _header.html.ep
<head><title><%= title %></title></head>
"""
from Pyjoyment.Lite import *
get('/', {'template': 'foo/bar'})
app.start()
You can name partial templates however you like, but a leading underscore is a commonly used naming convention.
Reusable template blocks¶
It’s never fun to repeat yourself, that’s why you can build reusable template
blocks in ep that work very similar to normal Python functions, with the
begin and end keywords. Just be aware that both keywords are part of
the surrounding tag and not actual Python code, so there can only be
whitespace after begin and before end.
"""
__DATA__
@@ welcome.html.ep
<% block = begin %>
% name = args[0]
Hello <%= name %>.
<% end %>
<%= block('Wolfgang') %>
<%= block('Baerbel') %>
"""
from Pyjoyment.Lite import *
get('/', 'welcome')
app.start()
A naive translation of the template to Python code could look like this.
output = ''
def block(*args):
name = args[0]
output = ''
output += 'Hello '
output += xml_escape(str(name))
output += '.'
return Pyjo.String.HTML.new(output)
output += xml_escape(block('Wolfgang'))
output += xml_escape(block('Baerbel'))
return output
While template blocks cannot be shared between templates, they are most commonly used to pass parts of a template to helpers.
Adding helpers¶
You should always try to keep your actions small and reuse as much code as possible. Helpers make this very easy, they get passed the current controller object as first argument, and you can use them to do pretty much anything an action could do.
"""
__DATA__
@@ index.html.ep
% debug('Hello from a template!')
"""
from Pyjoyment.Lite import *
@helper('debug')
def debug(c, string):
c.app.log.debug(string)
@get('/', 'index')
def index(c):
c.debug('Hello from an action!')
app.start()
Helpers can also accept template blocks as last argument, this for example,
allows very pleasant to use tag helpers and filters. Wrapping the helper
result into a Pyjo.String.HTML object can prevent accidental double
escaping.
"""
__DATA__
@@ index.html.ep
%= trim_newline(begin
Some text.
%= 1 + 1
More text.
% end)
"""
from Pyjo.String.HTML import html
from Pyjoyment.Lite import *
@helper('trim_newline')
def trim_newline(c, block):
result = str(block()).replace('\n', '')
return html(result)
get('/', 'index')
app.start()
Similar to stash values, you can use a prefix like myapp.* to keep
helpers from getting exposed in templates as functions, and to organize them
into namespaces as your application grows. Every prefix automatically
becomes a helper that returns a proxy object containing the current
controller object and on which you can call the nested helpers.
from Pyjoyment.Lite import *
@helper('cache_control.no_caching')
def no_caching(c):
c.res.headers.cache_control = 'private, max-age=0, no-cache'
@helper('cache_control.five_minutes')
def five_minutes(c):
c.res.headers.cache_control = 'public, max-age=300'
@get('/news')
def news(c):
c.cache_control.no_caching()
c.render(text='Always up to date.')
@get('/some_older_story')
def some_older_story(c):
c.cache_control.five_minutes()
c.render(text='This one can be cached for a bit.')
app.start()
While helpers can also be redefined, this should only be done very carefully to avoid conflicts.
Content blocks¶
The helper content_for of Pyjoyment.Plugin.DefaultHelpers allows
you to pass whole blocks of content from one template to another. This can
be very useful when your layout has distinct sections, such as sidebars,
where content should be inserted by the template.
"""
__DATA__
@@ foo.html.ep
% layout('mylayout')
% content_for('header', begin
<meta http-equiv="Content-Type" content="text/html">
% end)
<div>Hello World!</div>
% content_for('header', begin
<meta http-equiv="Pragma" content="no-cache">
% end)
@@ layouts/mylayout.html.ep
<!DOCTYPE html>
<html>
<head><%= content('header') %></head>
<body><%= content() %></body>
</html>
"""
from Pyjoyment.Lite import *
get('/', 'foo')
app.start()
Forms¶
To build HTML forms more efficiently you can use tag helpers like
form_for of Pyjoyment.Plugin.TagHelpers, which can automatically
select a request method for you if a route name is provided. And since most
browsers only allow forms to be submitted with GET and POST, but not
request methods like PUT or DELETE, they are spoofed with an
_method query parameter.
"""
__DATA__
@@ form.html.ep
<!DOCTYPE html>
<html>
<body>
% if (confirmation := flash('confirmation')):
<p><%= confirmation %></p>
% end
%= form_for('nothing', begin
%= text_field('whatever', 'I ♥ Pyjoyment!')
%= submit_button()
% end)
</body>
</html>
"""
from Pyjoyment.Lite import *
get('/', 'form')
# PUT /nothing
# POST /nothing?_method=PUT
@put('/nothing')
def nothing(c):
# Prevent double form submission with redirect
value = c.param('whatever')
c.flash(confirmation=f'We did nothing with your value ({value}).')
c.redirect_to('form')
app.start()
The helpers flash and redirect_to of
Pyjoyment.Plugin.DefaultHelpers are often used together to prevent
double form submission, allowing users to receive a confirmation message that
will vanish if they decide to reload the page they’ve been redirected to.
Form validation¶
You can use the helper validation of
Pyjoyment.Plugin.DefaultHelpers to validate GET and POST
parameters submitted to your application. All unknown fields will be ignored
by default, so you have to decide which should be
required()
or
optional()
before you can perform checks on their values. Every check is performed right
away, so you can use the results immediately to build more advanced
validation logic with methods like
is_valid().
"""
__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html>
<head>
<style>
label.field-with-error { color: #dd7e5e }
input.field-with-error { background-color: #fd9e7e }
</style>
</head>
<body>
%= form_for('index', begin
%= label_for('user', 'Username (required, 1-20 characters, a-z/0-9)')
<br>
%= text_field('user', id='user')
%= submit_button()
<br>
%= label_for('pass', 'Password (optional, 7-500 characters)')
<br>
%= password_field('pass', id='pass')
<br>
%= label_for('pass_again', 'Password again (equal to the value above)')
<br>
%= password_field('pass_again', id='pass_again')
% end)
</body>
</html>
@@ thanks.html.ep
<!DOCTYPE html>
<html><body>Thank you <%= validation().param('user') %>.</body></html>
"""
from Pyjoyment.Lite import *
import re
@get('/', 'index')
def index(c):
# Check if parameters have been submitted
v = c.validation()
if not v.has_data:
return c.render('index')
# Validate parameters ("pass_again" depends on "pass")
v.required('user').size(1, 20).like(re.compile(r'^[a-z0-9]+$'))
if v.optional('pass').size(7, 500).is_valid():
v.required('pass_again').equal_to('pass')
# Check if validation failed
if v.has_error():
return c.render('index')
# Render confirmation
c.render('thanks')
app.start()
Form elements generated with tag helpers from
Pyjoyment.Plugin.TagHelpers will automatically remember their previous
values and add the class field-with-error for fields that failed
validation to make styling with CSS easier.
<label class="field-with-error" for="user">
Username (required, 1-20 characters, a-z/0-9)
</label>
<input class="field-with-error" type="text" name="user" value="SRI">
For a full list of available checks see also the checks of
Pyjoyment.Validator.
Adding form validation checks¶
Validation checks can be registered with
add_check() and return a
false value if they were successful. A true value may be used to pass along
additional information which can then be retrieved with
error().
"""
__DATA__
@@ form.html.ep
<!DOCTYPE html>
<html>
<body>
% if (number := flash('number')):
<p>Thanks, the number <%= number %> was valid.</p>
% end
%= form_for('test', begin
% if (err := validation().error('number')):
<p>
% if err[0] == 'required':
Value is required.
% end
% if err[0] == 'range':
Value needs to be between 3 and 23.
% end
</p>
% end
%= text_field('number')
%= submit_button()
% end)
</body>
</html>
"""
from Pyjoyment.Lite import *
# Add "range" check
@app.validator.add_check('range')
def range_check(v, name, value, min, max):
return float(value) < min or float(value) > max
get('/', 'form')
@post('/test')
def test(c):
# Validate parameters with custom check
v = c.validation()
v.required('number').range(3, 23)
# Render form again if validation failed
if v.has_error():
return c.render('form')
# Prevent double form submission with redirect
c.flash(number=v.param('number'))
c.redirect_to('form')
app.start()
Cross-site request forgery¶
CSRF is a very common attack on web applications that trick your logged in
users to submit forms they did not intend to send, with something as mundane
as a link. All you have to do, to protect your users from this, is to add an
additional hidden field to your forms with the helper csrf_field of
Pyjoyment.Plugin.TagHelpers, and validate it with
csrf_protect().
"""
__DATA__
@@ target.html.ep
<!DOCTYPE html>
<html>
<body>
%= form_for('target', begin
%= csrf_field()
%= label_for('city', 'Which city to point low orbit ion cannon at?')
%= text_field('city', id='city')
%= submit_button()
%= end)
</body>
</html>
"""
from Pyjoyment.Lite import *
get('/', {'template': 'target'})
@post('/', 'target')
def target(c):
# Check CSRF token
v = c.validation()
if v.csrf_protect().has_error('csrf_token'):
return c.render(text='Bad CSRF token!', status=403)
city = v.required('city').param('city')
if not v.has_error():
c.render(text=f'Low orbit ion cannon pointed at {city}!')
app.start()
For Ajax requests and the like, you can also generate a token directly with
the helper csrf_token of Pyjoyment.Plugin.DefaultHelpers, and then
pass it along with the X-CSRF-Token request header.
The token is masked with a fresh random value on every request, so its value changes each time even though it stays bound to the same session. This protects against BREACH attacks, which could otherwise recover a static token from compressed responses.
Advanced¶
Less commonly used and more powerful features.
Template inheritance¶
Inheritance takes the layout concept above one step further, the helpers
content and extends of Pyjoyment.Plugin.DefaultHelpers allow
you to build skeleton templates with named blocks that child templates can
override.
"""
__DATA__
@@ layouts/mylayout.html.ep
<!DOCTYPE html>
<html>
<head><title>Hello</title></head>
<body><%= content() %></body>
</html>
@@ first.html.ep
%= content('header', begin
Default header
% end)
<div>Hello World!</div>
%= content('footer', begin
Default footer
% end)
@@ second.html.ep
% extends('first')
% content('header', begin
New header
% end)
@@ third.html.ep
% extends('second')
% content('footer', begin
New footer
% end)
"""
from Pyjoyment.Lite import *
# first > mylayout
get('/first', {'template': 'first', 'layout': 'mylayout'})
# third > second > first > mylayout
get('/third', {'template': 'third', 'layout': 'mylayout'})
app.start()
This chain could go on and on to allow a very high level of template reuse.
Custom responses¶
Most response content, static as well as dynamic, gets served through
Pyjo.Asset.File and Pyjo.Asset.Memory objects. For somewhat
static content, like cached JSON data or temporary files, you can create your
own and use the helper reply.asset of
Pyjoyment.Plugin.DefaultHelpers to serve them while allowing content
negotiation to be performed with Range, If-Modified-Since and
If-None-Match headers.
import Pyjo.Asset.File
from Pyjoyment.Lite import *
@get('/leak')
def leak(c):
c.res.headers.content_type = 'text/plain'
c.reply.asset(Pyjo.Asset.File.new(path='/etc/passwd'))
app.start()
For even more control you can also just skip the helper and use
rendered() to tell the
renderer when you’re done generating a response.
import Pyjo.Asset.File
from Pyjoyment.Lite import *
@get('/leak')
def leak(c):
c.res.headers.content_type = 'text/plain'
c.res.content.asset = Pyjo.Asset.File.new(path='/etc/passwd')
c.rendered(200)
app.start()
Helper plugins¶
Some helpers might be useful enough for you to share them between multiple applications, plugins make that very simple.
import Pyjoyment.Plugin
class Pyjoyment_Plugin_DebugHelper(Pyjoyment.Plugin.object):
def register(self, app, conf):
@app.helper('debug')
def debug(c, string):
c.app.log.debug(string)
object = Pyjoyment_Plugin_DebugHelper
The register method will be called when you load the plugin. And to add
your helper to the application, you can use
helper().
from Pyjoyment.Lite import *
plugin('DebugHelper')
@get('/')
def index(c):
c.debug('It works!')
c.render(text='Hello!')
app.start()
A skeleton for a full distribution of a plugin can be automatically generated.
$ pyjo generate plugin DebugHelper
And if you have a PyPI account, you are only a few commands away from releasing it to PyPI.
$ cd Pyjoyment-Plugin-DebugHelper
$ uv run python -m Pyjo.Prove -l -r t
$ uv build
$ uv publish
Bundling assets with plugins¶
Assets such as templates and static files can be easily bundled with your plugins, even if you plan to release them to PyPI.
$ pyjo generate plugin AlertAssets
$ mkdir Pyjoyment-Plugin-AlertAssets/Pyjoyment/Plugin/AlertAssets
$ cd Pyjoyment-Plugin-AlertAssets/Pyjoyment/Plugin/AlertAssets
$ mkdir public
$ echo 'alert("Hello World!");' > public/alertassets.js
$ mkdir templates
$ echo '%= javascript("/alertassets.js")' > templates/alertassets.html.ep
Just give them reasonably unique names, ideally based on the name of your
plugin, and append their respective directories to the list of search paths
when register is called.
import Pyjoyment.Plugin
from Pyjo.File import curfile
class Pyjoyment_Plugin_AlertAssets(Pyjoyment.Plugin.object):
def register(self, app, conf):
# Append "templates" and "public" directories
base = curfile().sibling('AlertAssets')
app.renderer.paths.append(base.child('templates').to_str())
app.static.paths.append(base.child('public').to_str())
object = Pyjoyment_Plugin_AlertAssets
The wheel of the distribution has only the module of the plugin, so the
directory goes to only-include in pyproject.toml too.
[tool.hatch.build.targets.wheel]
only-include = ["Pyjoyment/Plugin/AlertAssets.py", "Pyjoyment/Plugin/AlertAssets"]
Both will work just like normal templates and public directories once
you’ve installed and loaded the plugin, with slightly lower precedence.
"""
__DATA__
@@ alert_me.html.ep
<!DOCTYPE html>
<html>
<head>
<title>Alert me!</title>
%= include('alertassets')
</head>
<body>You've been alerted.</body>
</html>
"""
from Pyjoyment.Lite import *
plugin('AlertAssets')
get('/alert_me')
app.start()
And it works just the same for assets bundled in the __DATA__ section of
your plugin.
"""
__DATA__
@@ alertassets.js
alert("Hello World!");
@@ alertassets.html.ep
%= javascript("/alertassets.js")
"""
import Pyjoyment.Plugin
class Pyjoyment_Plugin_AlertAssets(Pyjoyment.Plugin.object):
def register(self, app, conf):
# Append class
app.renderer.classes.append(__name__)
app.static.classes.append(__name__)
object = Pyjoyment_Plugin_AlertAssets
Post-processing dynamic content¶
While post-processing tasks are generally very easy with the hook
after_dispatch of hook(), for content
generated by the renderer it is a lot more efficient to use the hook
after_render, which gets the content as a file object of bytes.
"""
__DATA__
@@ hello.html.ep
<!DOCTYPE html>
<html>
<head><title><%= c.title() %></title></head>
<body>Compressed content.</body>
</html>
"""
from Pyjoyment.Lite import *
import gzip
import re
@hook('after_render')
def after_render(c, output, format):
# Check if "gzip=True" has been set in the stash
if not c.stash().get('gzip'):
return
# Check if user agent accepts gzip compression
if not re.search(r'gzip', c.req.headers.accept_encoding or '', re.I):
return
c.res.headers.append('Vary', 'Accept-Encoding')
# Compress content with gzip
c.res.headers.content_encoding = 'gzip'
compressed = gzip.compress(output.getvalue())
output.seek(0)
output.truncate()
output.write(compressed)
get('/', {'template': 'hello', 'title': 'Hello', 'gzip': True})
app.start()
If you want to compress all dynamically generated content you can also
activate compress.
Streaming¶
You don’t have to render all content at once, the method
write() can also be used to
stream a series of smaller chunks of bytes.
from Pyjoyment.Lite import *
@get('/')
def index(c):
# Prepare body
body = b'Hello World!'
c.res.headers.content_length = len(body)
# Start writing directly with a drain callback
def drain(c):
nonlocal body
chunk, body = body[:1], body[1:]
c.write(chunk, drain if body else None)
drain(c)
app.start()
The drain callback will be executed whenever the entire previous chunk of data has actually been written.
HTTP/1.1 200 OK
Date: Sat, 13 Sep 2014 16:48:29 GMT
Content-Length: 12
Server: Pyjoyment (Python)
Hello World!
Instead of providing a Content-Length header you can also call
finish() and close the
connection manually once you are done.
from Pyjoyment.Lite import *
@get('/')
def index(c):
# Prepare body
body = b'Hello World!'
# Start writing directly with a drain callback
def drain(c):
nonlocal body
chunk, body = body[:1], body[1:]
if chunk:
c.write(chunk, drain)
else:
c.finish()
drain(c)
app.start()
While this is rather inefficient, as it prevents keep-alive, it is sometimes necessary for EventSource and similar applications.
HTTP/1.1 200 OK
Date: Sat, 13 Sep 2014 16:48:29 GMT
Connection: close
Server: Pyjoyment (Python)
Hello World!
Chunked transfer encoding¶
For very dynamic content you might not know the response content length in
advance, that’s where the chunked transfer encoding and
write_chunk() come in
handy. A common use would be to send the head section of an HTML document
to the browser in advance and speed up preloading of referenced images and
stylesheets.
from Pyjoyment.Lite import *
@get('/')
def index(c):
c.write_chunk(b'<html><head><title>Example</title></head>', lambda c: c.finish(b'<body>Example</body></html>'))
app.start()
The optional drain callback ensures that all previous chunks have been
written before processing continues. To end the stream you can call
finish() or write an empty
chunk of data.
HTTP/1.1 200 OK
Date: Sat, 13 Sep 2014 16:48:29 GMT
Transfer-Encoding: chunked
Server: Pyjoyment (Python)
29
<html><head><title>Example</title></head>
1b
<body>Example</body></html>
0
Especially in combination with long inactivity timeouts this can be very useful for Comet (long polling). Due to limitations in some web servers this might not work perfectly in all deployment environments.
Encoding¶
Templates stored in files are expected to be UTF-8 by default, but that
can be easily changed with
encoding.
app.renderer.encoding = 'koi8-r'
All templates from the __DATA__ section are characters of the Python
script, which is UTF-8 unless it declares another encoding.
"""
__DATA__
@@ heart.html.ep
I ♥ Pyjoyment!
"""
from Pyjoyment.Lite import *
get('/heart')
app.start()
Base64 encoded DATA files¶
Base64 encoded static files such as images can be easily stored in the
__DATA__ section of your application, similar to templates.
"""
__DATA__
@@ favicon.ico (base64)
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAB5UlE
QVQ4jbWRvWtTYRTGf+e9uTdNSZo0Vo10af2g4sdQdRKxBSeL4KD/gIuDg4OL4N/gJPgv6OqgLhZX
0SiiBGrQIpT0kxJqm9ybe/Pe9ziYgrY0tIPPen7POQ/ngf8pVR3RZPWpbtXeqo3uqKrsZDJ9zHnX
nK2z9a2kzZohO3LFG783Cdz/mzN9Atwwa7NDxLFRF6Nhw08bL+7uTLFnAiBI41CERbw0RV0Rkez2
0XQ/CV5L+XLkVj8SLzSIm8uOw1dfiUjax/OvVPVi+OaBtt89STRceqyqwb7NvQXXN5/d1jTafLQX
k+mBl4Bru6bdXzclKGAGCsdU9eGO6ayIfBJV9XXt5Ub85flgWv2Jd2aCYPwkcRiyXq+hzXWOXpgm
IKSzvIWb+4o5dZ7c5EzIuVslA0wlKx98UxilO3YEm2ljowjfJRgp4A0O4EUrWOsjkmKLQ5jiMOnC
ex+YygCVVv2HZzsR4gzduc9sBotouIx/6CxIlvX5RaQ1D8EguvSdMFGyOd8rz1D5U6PLoGELkg4U
xxCX4pcnEPHBekg7RnMFdGMeLR/HJRGaZnX7idWh09Mdu7GQUQsmEMV2BJwQx2BKqu22OD+ntjIq
Xr6ihBHB8AkLVKXXgg/kD9QxtESke0DPbv0G4xbnimn9MWcAAAAASUVORK5CYII=
"""
from Pyjoyment.Lite import *
get('/', {'text': 'I ♥ Pyjoyment!'})
app.start()
Inflating DATA templates¶
Templates stored in files get preferred over files from the __DATA__
section, this allows you to include a default set of templates in your
application that the user can later customize. The command
Pyjoyment.Command.Author.inflate will write all templates and static
files from the __DATA__ section into actual files in the templates
and public directories.
$ ./myapp.py inflate
Customizing the template syntax¶
You can easily change the whole template syntax by loading
Pyjoyment.Plugin.EPRenderer with a custom configuration.
"""
__DATA__
@@ index.html.mustache
Hello {{= name }}.
"""
from Pyjoyment.Lite import *
plugin('EPRenderer', {'name': 'mustache', 'template': {'tag_start': '{{', 'tag_end': '}}'}})
get('/:name', {'name': 'Anonymous'}, 'index')
app.start()
Pyjo.Template contains the whole list of available options.
Adding your favorite template system¶
Maybe you would prefer a different template system than ep, which is
provided by Pyjoyment.Plugin.EPRenderer, and there is not already a
plugin on PyPI for your favorite one. All you have to do, is to add a new
handler with add_handler()
when register is called.
import Pyjoyment.Plugin
class Pyjoyment_Plugin_MyRenderer(Pyjoyment.Plugin.object):
def register(self, app, conf):
# Add "mine" handler
def mine(renderer, c, output, options):
# Check for one-time use inline template
inline_template = options.get('inline')
# Check for appropriate template in "templates" directories
template_path = renderer.template_path(options)
# Check for appropriate template in DATA sections
data_template = renderer.get_data_template(options)
# This part is up to you and your template system :)
template = inline_template or data_template
if template is None and template_path is not None:
with open(template_path, encoding='utf-8') as file:
template = file.read()
# Or just raise an exception if an error occurs
if template is None:
raise Exception('Something went wrong with the template')
# Pass the rendered result back to the renderer
output.write(template.replace('World', 'Pyjoyment'))
app.renderer.add_handler('mine', mine)
object = Pyjoyment_Plugin_MyRenderer
An inline template, if provided by the user, will be passed along with
the options. You can use
template_path() to search the
templates directories of the application, and
get_data_template() to search
the __DATA__ sections.
"""
__DATA__
@@ test.html.mine
Hello World from the DATA section!
"""
from Pyjoyment.Lite import *
plugin('MyRenderer')
# Render an inline template
get('/inline', {'inline': 'Hello World inline!', 'handler': 'mine'})
# Render a template from the DATA section
get('/data', {'template': 'test'})
app.start()
Adding a handler to generate binary data¶
By default the renderer assumes that every handler generates characters
that need to be automatically encoded, but this can be easily disabled if
you’re generating bytes instead.
from Pyjoyment.Lite import *
import pickle
# Add "pickle" handler
def pickle_handler(renderer, c, output, options):
# Disable automatic encoding
options.pop('encoding', None)
# Encode data from stash value
output.write(pickle.dumps(c.stash().pop('pickle')))
app.renderer.add_handler('pickle', pickle_handler)
# Set "handler" value automatically if "pickle" value is set already
@hook('before_render')
def before_render(c, args):
if 'pickle' in args or 'pickle' in c.stash():
args['handler'] = 'pickle'
get('/', {'pickle': {'i': '♥ pyjoyment'}})
app.start()
The hook before_render of hook() can be
used to make stash values like pickle special, so that they no longer
require a handler value to be set explicitly.
# Explicit "handler" value
c.render(pickle={'i': '♥ pyjoyment'}, handler='pickle')
# Implicit "handler" value (with "before_render" hook)
c.render(pickle={'i': '♥ pyjoyment'})
More¶
You can continue with Guides now or take a look at the Mojolicious wiki, which contains a lot more documentation and examples by many different authors.
Ported from Mojolicious::Guides::Rendering of Mojolicious 9.53.