(collections.Mapping) The default configuration mapping.
The application base class. In order to make an application you have to inherit this class. As you know Python’s subclassing can be done like:
from plastic.app import BaseApp
class MyApp(BaseApp):
pass
However in most cases you don’t have to fill its body but simply clone():
from plastic.app import BaseApp
MyApp = BaseApp.clone()
Use route() class method for routing.
Adds a new rule and maps it to the function. It is a lower-level primitive method. In most cases you don’t need it and just use route() decorator or other higher-level methods.
Parameters: |
|
---|
Registers a function which serializes a value into a string. The funtion has to take two arguments and return its serialized result.
Parameters: |
|
---|---|
Returns: | a serialized result |
Return type: | basestring |
Parameters: |
|
---|
Registers a templating function to the given suffix. The function has to take three arguments and return its rendering result:
Parameters: |
|
---|---|
Returns: | the rendering result |
Return type: | basestring |
Parameters: |
|
---|
Associates mimetype to suffix. Associations made by this are used by render() function. For example:
from plastic.rendering import render
App.associate_mimetypes({
'text/plain': 'rst',
'text/x-rst': 'rst',
'text/html': 'html'
})
@App.route('/')
def home(request):
return render(request, None, 'main/home')
with the above setting when you make a request like:
GET / HTTP/1.0
Accept: text/html
will find a template file main/home.html.* (* is suffix for template engine).
Parameters: |
|
---|
Subclasses the application class. It is a just shorthand of normal subclassing. If your application becomes bigger stop using this and normaly subclass the application class using class keyword instead.
(collections.Mapping) The dict of endpoints to view functions.
(ImmutableDict) The immutable dictionary of mimetype to registered renderers.
Renders the response using registered template_engines.
You have to pass the path without specific suffix. The last suffix will be used for determining what template engine to use. For example, if there is user_profile.html.mako file in the template_path and 'mako' is associated to Mako template engine in template_engines mapping, the following code will find user_profile.html.mako (not user_profile.html) and render it using Mako:
app.render_template(request, 'user_profile.html')
In other words, you have to append a suffix to determine what template engine to use into filenames of templates.
Parameters: |
|
---|---|
Returns: | a rendered result |
Return type: | basestring |
Raises plastic.exceptions.RenderError: | |
when there are no matched template files |
The function decorator which maps the path to the decorated view function.
It takes the same arguments to the constructor of werkzeug.routing.Rule class. In most case simply give a path string to route:
@App.route('/people/<name>')
def person(request, name):
return 'Hi, ' + name
(collections.Sequence) The list of routing rules.
Starts serving the application.
Parameters: |
|
---|
The function decorator which associate the given serializer function with mimetypes.
import json
import plistlib
@App.serializer('application/json')
def serialize_json(request, value):
return json.dumps(value)
@App.serializer(['application/x-plist', 'text/xml'])
def serialize_plist(request, value):
return plistlib.writePlistToString(value)
Parameters: | mimetypes (basestring, collections.Iterable) – one or more mimetypes to assiciate the function with. it can be either a single string or multiple strings in an iterable e.g. 'application/json', ['application/xml', 'text/xml'] |
---|
(collections.Mapping) The mapping of cookie settings sessions will use. It has the following configuable keys:
See also
(werkzeug.contrib.sessions.SessionStore) The session store instance an application uses. It’s a proxy to 'session_store' value of config.
(ResourceDirectory) The mapping object of the template directory.
The function decorator which makes the given function the template engine of the suffix.
# The following example has several issues. Do not C&P
# it into your program to use Mako.
from mako.template import Template
@App.template_engine(suffix='mako')
def render_mako(request, path, values):
with request.app.template_directory[path] as f:
template = Template(f.read())
return template.render(**values)
Parameters: | suffix (basestring) – the filename suffix (without period character) to register the template engine e.g. 'mako' |
---|
(ImmutableDict) The immutable dictionary of suffix to registered templating functions.
(basestring) The path to the directory which contains template files. It has to be relative to the application module. It can be overridden. Default is templates/.
The actual WSGI function. Replace it when the application should be wrapped by middlewares.
app.wsgi_app = Middleware(app.wsgi_app)