3: Configuration Files and pserve

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.

Objectives

  • Create an application driven by a .ini file
  • Startup the application with Pyramid’s pserve command
  • Move code into the package’s __init__.py

Steps

  1. 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
    
  2. 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
    
  3. 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()
    
  4. 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.

  5. Run the WSGI application with:

    (env33)$ pserve development.ini
    
  6. Open http://127.0.0.1:6547/ in your browser.

Analysis

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:

  • pserve looks for [app:main] and finds use = egg:tutorial
  • The package’s setup.py has defined this (lines 9-10) for the distribution (egg) as tutorial:main
  • The tutorial distribution’s __init__ has a main function
  • This function is invoked, with the values from certain .ini sections passed in

The .ini file is also used for two other functions:

  • [server:main] wires up the choice of WSGI server for your WSGI application
  • Pyramid uses Python standard logging, which needs a number of configuration values. The .ini serves this function.

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.

Extra Credit

  1. Is it possible to develop your application separately from the configuration? Is that a good idea?
  2. Can you have more than one .ini file for an application?
  3. If you don’t like configuration and/or .ini files, could you do this yourself in Python code?

Table Of Contents