Pyramid has a first-class concept of configuration distinct from code. This approach is optional, but its presence makes it distinct from other Python web frameworks.
Let’s begin using the previous package as a starting point for a new distribution, then making it active:
(env33)$ cd ..; cp -r step02 step03; cd step03
(env33)$ python3.3 setup.py develop
Pyramid can use a .ini-formatted configuration file as its launching point. Type the following into development.ini inside step03:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | [app:main]
use = egg:tutorial
pyramid.reload_templates = true
[server:main]
use = egg:pyramid#wsgiref
host = 0.0.0.0
port = 6547
[loggers]
keys = root
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = INFO
handlers = console
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
|
Many Pyramid apps put the logic for setting up the WSGI app into the distribution’s __init__.py, so enter the following into tutorial/__init__.py:
1 2 3 4 5 6 7 8 | from pyramid.config import Configurator
def main(global_config, **settings):
config = Configurator(settings=settings)
config.add_route('hello', '/')
config.scan()
return config.make_wsgi_app()
|
Let’s eliminate our wikiapp.py, as its functionality is now merged into __init__.py:
(env33)$ rm tutorial/wikiapp.py*
We do .py* to ensure we get the byte-compiled .pyc file.
Run the WSGI application with:
(env33)$ pserve development.ini
Open http://127.0.0.1:6547/ in your browser.
Our development.ini file is read by pserve and serves to bootstrap our application. Processing then proceeds as described in the Pyramid chapter on application startup:
The .ini file is also used for two other functions:
In this case, pserve finds the server defined by [server:main] and the application defined by [app:main]. The latter’s use = egg:tutorial governs
The code from wikiapp.py that launched our application not only was moved to __init__.py...it also got shorter, as it didn’t need to wire up a WSGI server.