Jesvin Jose
Jesvin Jose

Reputation: 23098

Code auto-reload in pyramid

I got the templates to reload dynamically but the controller code does not; I have to kill and restart the process. Listed below is the code I run; I run it as python app.py.

What have I misconfigured and how do I get the code to reload dynamically?

if __name__ == '__main__':
    # configuration settings
    settings = {}
    settings['reload_all'] = True
    settings['debug_all'] = True
    settings['mako.directories'] = os.path.join(here, 'templates')
    #settings['db'] = os.path.join(here, 'tasks.db')
    # session factory
    session_factory = UnencryptedCookieSessionFactoryConfig('itsaseekreet')
    # configuration setup
    config = Configurator(settings=settings, session_factory=session_factory)
    # routes setup
    #config.add_route('list', '/')
    #config.add_route('new', '/new')
    #config.add_route('close', '/close/{id}')

    #config.add_route('hello', '/hello')
    config.add_route('meta', '/m/{mid}')
    # static view setup
    config.add_static_view('static', os.path.join(here, 'static'))
    # scan for @view_config and @subscriber decorators

    connection = pymongo.Connection()
    def add_db(request):
        #db = config.registry.db[db_url.path[1:]]
        #if db_url.username and db_url.password:
            #db.authenticate(db_url.username, db_url.password)
        db = connection["bricwall_pyramid"]["bricwall"]
        return db

    #def add_fs(request):
       #return GridFS(request.db)

    config.add_request_method(add_db, 'db', reify=True)
    #config.add_request_method(add_fs, 'fs', reify=True)

    config.scan()
    # serve app
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    server.serve_forever()

Upvotes: 1

Views: 3440

Answers (1)

Nemeth
Nemeth

Reputation: 1090

If you want to reload your Pyramid application, I would recommend using paster, instead of calling server.serve_forever()

One easy way to do that, is using pyramid's scaffolds:

You could use the Starter project scaffold:

pcreate -s starter MyProject

Or, with Sqlalchemy (but I've seen you're using pymongo, so I think the first one would the best choice):

pcreate -s alchemy MyProject

That will create a default MyProject application, then you must install it:

python setup.py develop

and then run it with:

pserve development.ini --reload

That's well explained in here: http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/narr/project.html

But your application would have to change to something like this:

def main(global_config, **settings)

    config = Configurator(settings=settings, session_factory=session_factory)
    # routes setup
    #config.add_route('list', '/')
    #config.add_route('new', '/new')
    #config.add_route('close', '/close/{id}')

    #config.add_route('hello', '/hello')
    config.add_route('meta', '/m/{mid}')
    # static view setup
    config.add_static_view('static', os.path.join(here, 'static'))
    # scan for @view_config and @subscriber decorators

    connection = pymongo.Connection()
    def add_db(request):
        #db = config.registry.db[db_url.path[1:]]
        #if db_url.username and db_url.password:
            #db.authenticate(db_url.username, db_url.password)
        db = connection["bricwall_pyramid"]["bricwall"]
        return db

    #def add_fs(request):
       #return GridFS(request.db)

    config.add_request_method(add_db, 'db', reify=True)
    #config.add_request_method(add_fs, 'fs', reify=True)

    config.scan()
    # serve app
    app = config.make_wsgi_app()
    return app

Where you have to wrap your init code inside a main function, and your settings go to the development.ini file.

Upvotes: 2

Related Questions