With pyramid, you can create an application from one file as shown in Example of hello world in official document. However, in most cases, there are many patterns of creating a project (pcreate) and starting it from a configuration file (pserve development.ini).
On the other hand, when I find it awkward to create a project, I also want to do it if I can do it with a one-file application. However, there are few examples of one-file applications in the official documentation. It stops at the example of returning a Response object without using the template engine, and there is no description beyond that.
A note on how to use the template engine (e.g. mako, jinja2, chameleon) in a one-file application.
hello world
Quoted from the official documentation. Creating Your First Pyramid Application — The Pyramid Web Framework v1.5.1
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('Hello %(name)s!' % request.matchdict)
if __name__ == '__main__':
config = Configurator()
config.add_route('hello', '/hello/{name}')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
I want to change the following part.
--Returning a Response object instead of a template engine --For example, I want to use mako. --You need to restart the application to change the html part to the screen
Include modules for each template engine
For mako, you need to specify the location of the template's top-level directory.
If you have created a project, write the application settings in the configuration file (e.g. development.ini). For example, the template engine settings mentioned earlier are written as follows.
mako.directories = appname:templates
I want to set this "mako.directories" even in a single file application. In such a case, pass the dictionary as an argument to the constructor of pyramid.config.Configurator.
In fact, when you create a project, the main function has an argument called settings, which is passed to the constructor.
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
config.include('pyramid_chameleon')
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
config.scan()
return config.make_wsgi_app()
The .ini file becomes just a dictionary after it is passed, so you can give it a dictionary. The shape is as follows.
if __name__ == '__main__':
#Make the same hierarchy as the source code of the application the top level hierarchy of the template
here = os.path.abspath(os.path.dirname(__file__))
settings = {"mako.directories": [here]}
config = Configurator()
After that, you can create a one-file application using the template engine by doing the following in the same way as when creating from the project. (Since the template part is divided in the first place, it may not be called a one-file application)
--Register with add_view instead of view_config --treat files ending in ".html" with add_mako_renderer as mako templates
An example of combining these is as follows.
onefile.py
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
import os.path
def hello_world(request):
return {"name": request.matchdict["name"]}
if __name__ == '__main__':
here = os.path.dirname(os.path.abspath(__file__))
settings = {"mako.directories": here,
"pyramid.reload_all": True}
config = Configurator(settings=settings)
config.include("pyramid_mako")
config.add_mako_renderer(".html")
config.add_route('hello', '/hello/{name}')
config.add_view(hello_world, route_name='hello', renderer="hello.html")
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
hello.html
Hello ${name}!
For the following, set "pyramid.reload_templates" to True.
--You need to restart the application to change the html part to the screen
Recommended Posts