Custom Search

Thursday, January 3, 2013

Introduction pyramid __init__.py and views.py

Introduction to the pyramid __init__.py and views.py

a)   

An __init__.py file signifies that this is a Python package. It also contains code that helps users run the application, including a main function which is used as a entry point for commands such as pserve, pshell, pviews, and others.

b)   
A templates directory, which contains Chameleon (or other types of) templates.

c)   
A tests.py module, which contains unit test code for the application.

d)   
A views.py module, which contains view code for the application.

a)
__init__.py
========

We need a small Python module that configures our application and which advertises an entry point for use by our PasteDeploy .ini file. This is the file named __init__.py. The presence of an __init__.py also informs Python that the directory which contains it is a package.

from pyramid.config import Configurator
from sqlalchemy import engine_from_config

from .models import (
    DBSession,
    Base,
    )


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    print "===__init_.py===main===global_config===", global_config
    print "===__init_.py===main===settings===", settings
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    config = Configurator(settings=settings)
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.scan()
    return config.make_wsgi_app()

-----------------------

===__init_.py===main===global_config===
{'__file__': '/home/saju/pyra_env/test/alchemy_proj/development.ini',
'here': '/home/saju/pyra_env/test/alchemy_proj'}

===__init_.py===main===settings===
{'pyramid.includes': '\npyramid_debugtoolbar\npyramid_tm',
'sqlalchemy.url': 'mysql://root:xxxx@localhost:3306/mydb1?charset=utf8',
'pyramid.debug_authorization': 'false',
'pyramid.default_locale_name': 'en',
'pyramid.reload_templates': 'true',
'pyramid.debug_notfound': 'false',
'pyramid.debug_routematch': 'false'}

b)
views.py
=======

Much of the heavy lifting in a Pyramid application is done by view callables. A view callable is the main tool of a Pyramid web application developer; it is a bit of code which accepts a request and which returns a response.

from pyramid.view import view_config

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    return {'project':'MyProject'}

Above code define and register a view callable named my_view. The function named my_view is decorated with a view_config decorator (which is processed by the config.scan() line in our __init__.py). The view_config decorator asserts that this view be found when a route named home is matched. In our case, because our __init__.py maps the route named home to the URL pattern /, this route will match when a visitor visits the root URL. The view_config decorator also names a renderer, which in this case is a template that will be used to render the result of the view callable.

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/project.html

No comments:

Post a Comment