How to organize files using the python27 app engine webapp2 framework

I already started getting tut for python27 and the application engine: https://developers.google.com/appengine/docs/python/gettingstartedpython27/

Towards the end of tut, all classes are in a single file (helloworld.py), and you and you configure the router to specify the URL path to the class at the bottom of the file:

 app = webapp2.WSGIApplication([('/', MainPage),
                           ('/sign', Guestbook)],
                          debug=True)

What has not been touched is how I configure my classes / files as my application grows. For example, can I put MainPage in a separate file and then call "import MainPage" in the helloworld.py file and add a route to WSGIApplication? Is there anything more automated than that? What should I call the MainPage file and where to store it?

+5
source share
1 answer

It is advisable to import all of your handlers at application startup, in order to take advantage of the lapp bootloader of the webapp2 bootloader , which loads modules / packages as needed.
Thus, you have several options:

Option 1, Handlers in the module
Place MainPagein another file (module) at the same level as your file helloworld.py:

/ my_gae_app
    app.yaml
    helloworld.py
    handlers.py

And in your routing (c helloworld.py) you would do:

app = webapp2.WSGIApplication([('/', 'handlers.MainPage'),
                               ('/sign', 'handlers.Guestbook')],
                              debug=True)

Option 2, Handlers in a package; perhaps they think your application is getting bigger
As your application grows in size, you can create a package to host your handlers:

/ my_gae_app
    / handlers
        __init__.py
        guestbook.py
        main.py
    app.yaml
    helloworld.py

( helloworld.py):

app = webapp2.WSGIApplication([('/', 'handlers.main.MainPage'),
                               ('/sign', 'handlers.guestbook.Guestbook')],
                              debug=True)
+7

All Articles