Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid project structure

Tags:

python

pyramid

I am developing a rather big project in pyramid. I used django before. I really like the way it structures the project and encapsulate functionality into apps. I would like to achieve same structure with pyramid. I know pyramid is very flexible to get this, but I need some help to achieve same structure with loose coupling. The project structure should look something like:

  Project/
         app1/
             models.py
             routes.py
             views.py
         app2/
             models.py
             routes.py
             views.py

Any suggestions?

like image 621
udaycode Avatar asked May 16 '11 03:05

udaycode


2 Answers

Since Pyramid makes no assumptions about your package structure in the first place, any way you divide your app ends up being fairly similar in configuration. However, if you're breaking your app into some distinct packages, you can (optionally) take advantage of the config.include() directive to include each package into your main config.

For example:

# myapp/__init__.py (main config)
def main(global_config, **settings):
    config = Configurator(...)
    # basic setup of your app
    config.include('pyramid_tm')
    config.include('pyramid_jinja2')

    # add config for each of your subapps
    config.include('project.app1')
    config.include('project.app2')

    # make wsgi app
    return config.make_wsgi_app()

# myapp/app1/__init__.py (app1's config)
def includeme(config):
    config.add_route(...)
    config.scan()

# myapp/app2/__init__.py (app2's config)
def includeme(config):
    config.add_route(...)
    config.scan()

In each of your subapps, you can then define views/models/etc.

In general you might want to create your SQLAlchemy (or other DB) session in the common setup, as likely your different apps are all using the same engine.

like image 64
Michael Merickel Avatar answered Oct 09 '22 10:10

Michael Merickel


I have implemented a global appIncluder function which is imported as includeme with the init.py of the package to be included.

includeme (ApplicationIncluder) receives the config object, so it is then easy to use config.package and its variables/methods/classes (present in the same init.py and submodules.

Thanks a lot for this idea!

The code:

project: 'foo' apps to be deployed in foo.foo.apps directory

Structure:

foo
|-foo
  |- __init__.py
  |- appincluder.py
  |-apps
    |-test
      |- __init__.py
      |- views.py
      |- templates
         |- test.jinja2

foo/foo/init.py:

config.include('foo.apps.test')

foo/foo/appincluder.py

def appIncluder(config):
    app    = config.package
    prefix = app.prefix
    routes = app.routes

    for route,url in routes.items():
        config.add_route(route,prefix + url)

    config.scan(app)

    log.info('app: %s included' % app.__name__)

foo/foo/apps/test/init.py

from foo.appincluder import appIncluder as includeme

prefix = '/test'

routes = {
    'test': '/home'
}

foo/foo/apps/test/views.py

from pyramid.view import view_config


@view_config(route_name='test', renderer='templates/test.jinja2')
def test(request):
    return {}

I hope it helps someone.

like image 33
DevLounge Avatar answered Oct 09 '22 09:10

DevLounge