Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having a Working Pyramid Example #278

Closed
Miserlou opened this issue Aug 23, 2016 · 9 comments
Closed

Having a Working Pyramid Example #278

Miserlou opened this issue Aug 23, 2016 · 9 comments

Comments

@Miserlou
Copy link
Owner

Miserlou commented Aug 23, 2016

Somebody mentioned having some trouble with Pyramid and Zappa, but I haven't explored it yet. It should be somewhat straight forward, but I haven't confirmed it yet. My guess is that it'll mean having something like this:

def generate_wsgi_app():
    config = Configurator()
    config.add_route('hello', '/hello/{name}')
    config.add_view(hello_world, route_name='hello')
    app = config.make_wsgi_app()
    return app

and then

{
    "dev": {
       "s3_bucket": "lmbda",
       "app_function": "your_module.generate_wsgi_app"
    }
}

But, I haven't tried this yet. Does this make sense, @bbangert ? Is there a pattern/convention that'd be better?

@Miserlou
Copy link
Owner Author

It looks like this might work, if it weren't for

#272
#45

:[

@Miserlou
Copy link
Owner Author

Miserlou commented Aug 23, 2016

Okay, this seems to work:

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    return Response('Hello %(name)s!' % request.matchdict)

def generate_wsgi_app(app, environ):
    config = Configurator()
    config.add_route('hello', '/hello/{name}')
    config.add_view(hello_world, route_name='hello')
    wsgi_app = config.make_wsgi_app()
    return wsgi_app(app, environ)
{
    "dev": {
        "app_function": "app.generate_wsgi_app", 
        "s3_bucket": "lmbda"
    }
}

@bbangert
Copy link
Collaborator

Does this work?

from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    return Response('Hello %(name)s!' % request.matchdict)

wsgi_app = None

def make_wsgiapp():
    global wsgi_app
    config = Configurator()
    config.add_route('hello', '/hello/{name}')
    config.add_view(hello_world, route_name='hello')
    wsgi_app = config.make_wsgi_app()

def generate_wsgi_app(app, environ):
    if wsgi_app is None:
        make_wsgiapp()
    return wsgi_app(app, environ)

@Miserlou
Copy link
Owner Author

Yes, but I think we need to move that pattern into handler.py. Semi-related #153

@knowsuchagency
Copy link
Contributor

knowsuchagency commented Jun 8, 2017

In case others are in need of a recipe, the following is what I came up with.
If I misunderstood any terminology or explanations in the following,
please feel free to correct me.

from pyramid.config import Configurator
from configparser import ConfigParser # This will be different in Python 2.7
from functools import partial 
import os


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    config.include('pyramid_jinja2')
    config.include('.models')
    config.include('.routes')
    config.scan()
    return config.make_wsgi_app()


def zappa(config_uri, event, context, **vars):
    """
    Uses the settings in the configuration uri to bootstrap a wsgi application
    through pyramid. 

    Zappa then uses that wsgi application
    to create a handler function for use with aws lambda. 

    Event and context information are passed to the handler function which uses
    our wsgi application to return a response.

    :param config_uri: string pointing to paste deploy config file
    :param event: aws event
    :param context: aws context
    :param vars: parameters that will be passed to the configuration file
    :return: response
    """
    config = ConfigParser()
    config.read(config_uri)
    settings = dict(config.items('app:main', vars=vars))
    wsgi_app = main(None, **settings)

    return wsgi_app(event, context)


# the following functions will have a signature similar to: 
# function(event, context)
# which is what zappa seems to like
zappa_dev = partial(zappa,
                    'development.ini',
                    dbusername=os.environ.get('dbusername'),
                    dbpassword=os.environ.get('dbpassword')
                    )

zappa_prod = partial(zappa,
                     'production.ini',
                     dbusername=os.environ.get('dbusername'),
                     dbpassword=os.environ.get('dbpassword')
                     )

@Nico005
Copy link

Nico005 commented Jul 13, 2017

Hi Guys, how should the zappa_settings file look when using pyramid?

@knowsuchagency
Copy link
Contributor

knowsuchagency commented Jul 14, 2017

@Nico005 Same as with any other application. Your dev configuration, if using the above code in main_module.py at the root of your project, could start with something like this

{
    "dev": {
        "app_function": "main_module.zappa_dev",
        ...

@gutomaia
Copy link

gutomaia commented Nov 24, 2017

I'm trying something like:

import os
from wsgiref.simple_server import make_server
from paste.deploy import loadapp

here = os.path.abspath(os.path.dirname(__file__))


def get_app(ini_file):
    return loadapp('config:%s/production.ini' % here)

app = get_app()

if __name__ == '__main__':
    server = make_server('0.0.0.0', 8888, app)
    server.serve_forever()

But I'm receiving an strange Configuration Error

ConfigurationError: No source file for module 'pyramid_jinja2' (.py file must exist, refusing to use orphan .pyc or .pyo file).

(I will edit later with comments to make it work, as soon as I get it running, for the time, any clues?)
BTW, where is the best place to post this question?

@Miserlou
Copy link
Owner Author

Miserlou commented Nov 24, 2017 via email

brianrower added a commit to brianrower/pypicloud that referenced this issue Sep 17, 2018
…pa#278 (comment)

Try manually parsing the settings file


oops forgot filename


Add config parser


fix params
brianrower added a commit to brianrower/pypicloud that referenced this issue Sep 17, 2018
…pa#278 (comment)

Try manually parsing the settings file


oops forgot filename


Add config parser


fix params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants