Pyjoyment.Lite - Micro real-time web framework¶
from Pyjoyment.Lite import *
# Route with placeholder
@get('/:foo')
def foo(c):
foo = c.param('foo')
c.render(text=f'Hello from {foo}.')
# Start the Pyjoyment command system
app.start()
Pyjoyment.Lite is a tiny domain specific language built around
Pyjoyment, made up of only about a dozen Python functions.
The module that imports the functions gets an application of its own, as
from Pyjoyment.Lite import * makes one for it, named after its file, with
its home in the directory of the file and its DATA templates in its
docstring after a line __DATA__, which is there before app.start()
runs. A route is a decorator of its callback too, which returns the route.
from Pyjoyment.Lite import *
get('/', {'text': 'Hello World!'})
@get('/:foo')
def foo(c):
c.render(text=f'Hello from {c.param("foo")}.')
app.start()
Growing¶
Here we have collected a few snippets that illustrate very well just how
similar Pyjoyment.Lite and Pyjoyment applications are.
Routes¶
The functions get, post and friends all have equivalent methods.
# Pyjoyment.Lite
@get('/foo')
def foo(c):
c.render(text='Hello World!')
# Pyjoyment
class MyApp(Pyjoyment.object):
def startup(self):
routes = self.routes
routes.get('/foo', lambda c: c.render(text='Hello World!'))
Application¶
The application object you can access with the name app is the object
passed as self to the startup method.
# Pyjoyment.Lite
app.max_request_size = 16777216
# Pyjoyment
class MyApp(Pyjoyment.object):
def startup(self):
self.max_request_size = 16777216
Plugins¶
Instead of the plugin function you just use the method
Pyjoyment.Pyjoyment_App.plugin().
# Pyjoyment.Lite
plugin('Config')
# Pyjoyment
class MyApp(Pyjoyment.object):
def startup(self):
self.plugin('Config')
Helpers¶
Similar to plugins, instead of the helper function you just use the
method Pyjoyment.Pyjoyment_App.helper().
# Pyjoyment.Lite
@helper('two')
def two(c):
return 1 + 1
# Pyjoyment
class MyApp(Pyjoyment.object):
def startup(self):
self.helper('two', lambda c: 1 + 1)
Templates¶
Instead of the template function you just use the method
Pyjoyment.Pyjoyment_App.template().
# Pyjoyment.Lite
@template('hello.html')
def hello(c, name):
return t('<p>Hello {name}!</p>')
# Pyjoyment
class MyApp(Pyjoyment.object):
def startup(self):
self.template('hello.html', lambda c, name: t('<p>Hello {name}!</p>', name=name))
Under¶
Instead of sequential function calls, we can use methods to build a tree with nested routes, that much better illustrates how routes work internally.
# Pyjoyment.Lite
under('/foo')
get('/bar', lambda c: ...)
# Pyjoyment
class MyApp(Pyjoyment.object):
def startup(self):
routes = self.routes
foo = routes.under('/foo')
foo.get('/bar', lambda c: ...)
Functions¶
Pyjoyment.Lite implements the following functions, which are
automatically exported.
any
route = any('/:foo', lambda c: ...)
route = any('/:foo', lambda c: ..., 'name')
route = any('/:foo', {'foo': 'bar'}, lambda c: ...)
route = any('/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = any(['GET', 'POST'], '/:foo', lambda c: ...)
route = any(['GET', 'POST'], '/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = any(['GET', 'POST'], '/:foo', lambda c: ...).requires(agent=re.compile(r'Firefox'))
Generate route with Pyjoyment.Routes.Route.Pyjoyment_Routes_Route.any(),
matching any of the listed HTTP request methods or all.
app
app = app
The Pyjoyment.Lite application object, which is a subclass of
Pyjoyment.
# Use all the available attributes and methods
app.log.level = 'error'
app.defaults(foo='bar')
delete
route = delete('/:foo', lambda c: ...)
route = delete('/:foo', lambda c: ..., 'name')
route = delete('/:foo', {'foo': 'bar'}, lambda c: ...)
route = delete('/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = delete('/:foo', lambda c: ...).requires(agent=re.compile(r'Firefox'))
Generate route with
Pyjoyment.Routes.Route.Pyjoyment_Routes_Route.delete(), matching only
DELETE requests.
get
route = get('/:foo', lambda c: ...)
route = get('/:foo', lambda c: ..., 'name')
route = get('/:foo', {'foo': 'bar'}, lambda c: ...)
route = get('/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = get('/:foo', lambda c: ...).requires(agent=re.compile(r'Firefox'))
Generate route with Pyjoyment.Routes.Route.Pyjoyment_Routes_Route.get(),
matching only GET requests.
group
with group():
...
Start a new route group, which ends with the block. A function can be the
group too: group(lambda: ...).
helper
helper('foo', lambda c, *args: ...)
Add a new helper with Pyjoyment.Pyjoyment_App.helper().
hook
hook('after_dispatch', lambda c: ...)
Share code with Pyjoyment.Pyjoyment_App.hook().
options
route = options('/:foo', lambda c: ...)
route = options('/:foo', lambda c: ..., 'name')
route = options('/:foo', {'foo': 'bar'}, lambda c: ...)
route = options('/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = options('/:foo', lambda c: ...).requires(agent=re.compile(r'Firefox'))
Generate route with
Pyjoyment.Routes.Route.Pyjoyment_Routes_Route.options(), matching only
OPTIONS requests.
patch
route = patch('/:foo', lambda c: ...)
route = patch('/:foo', lambda c: ..., 'name')
route = patch('/:foo', {'foo': 'bar'}, lambda c: ...)
route = patch('/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = patch('/:foo', lambda c: ...).requires(agent=re.compile(r'Firefox'))
Generate route with
Pyjoyment.Routes.Route.Pyjoyment_Routes_Route.patch(), matching only
PATCH requests.
plugin
plugin('SomePlugin', foo=23)
Load a plugin with Pyjoyment.Pyjoyment_App.plugin().
post
route = post('/:foo', lambda c: ...)
route = post('/:foo', lambda c: ..., 'name')
route = post('/:foo', {'foo': 'bar'}, lambda c: ...)
route = post('/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = post('/:foo', lambda c: ...).requires(agent=re.compile(r'Firefox'))
Generate route with
Pyjoyment.Routes.Route.Pyjoyment_Routes_Route.post(), matching only
POST requests.
put
route = put('/:foo', lambda c: ...)
route = put('/:foo', lambda c: ..., 'name')
route = put('/:foo', {'foo': 'bar'}, lambda c: ...)
route = put('/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = put('/:foo', lambda c: ...).requires(agent=re.compile(r'Firefox'))
Generate route with Pyjoyment.Routes.Route.Pyjoyment_Routes_Route.put(),
matching only PUT requests.
query
route = query('/:foo', lambda c: ...)
route = query('/:foo', lambda c: ..., 'name')
route = query('/:foo', {'foo': 'bar'}, lambda c: ...)
route = query('/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = query('/:foo', lambda c: ...).requires(agent=re.compile(r'Firefox'))
Generate route with
Pyjoyment.Routes.Route.Pyjoyment_Routes_Route.query(), matching only
QUERY requests.
template
template('index.html', lambda c: t('<p>Hello!</p>'))
@template('index.html')
def index(c): ...
Add a template of a function with Pyjoyment.Pyjoyment_App.template(),
the counterpart of a template of the DATA section for template strings,
see Pyjoyment.Plugin.PyRenderer.
under
route = under(lambda c: ...)
route = under('/:foo', lambda c: ...)
route = under('/:foo', {'foo': 'bar'})
route = under('/:foo', foo=re.compile(r'\w+'))
route = under('/:foo').requires(agent=re.compile(r'Firefox'))
route = under(format=['json', 'yaml'])
Generate nested route with
Pyjoyment.Routes.Route.Pyjoyment_Routes_Route.under(), to which all
following routes are automatically appended.
websocket
route = websocket('/:foo', lambda c: ...)
route = websocket('/:foo', lambda c: ..., 'name')
route = websocket('/:foo', {'foo': 'bar'}, lambda c: ...)
route = websocket('/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = websocket('/:foo', lambda c: ...).requires(agent=re.compile(r'Firefox'))
Generate route with
Pyjoyment.Routes.Route.Pyjoyment_Routes_Route.websocket(), matching
only WebSocket handshakes.
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojolicious::Lite of Mojolicious 9.49.