how to organise files with python27 app engine webapp2 framework

2024/4/15 0:35:46

I've gone through the getting started tut for python27 and app engine: https://developers.google.com/appengine/docs/python/gettingstartedpython27/

By the end of the tut, all the the classes are in the same file (helloworld.py) and you and you configure the router to point a url path to a class at the bottom of the file:

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

What the tut did not cover is how do I orginise my classes / files as my app grows. For example, would I put MainPage in a separate file and then call 'import MainPage' in the helloworld.py file and add the route to the WSGIApplication? Is there anything more automated than this? What should I call the MainPage file and where should I store it?

Answer

Preferable to importing all of your handlers at app-startup is to take advantage of webapp2's lazy handler loading which loads modules/packages as needed.
So you have a couple of options:

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

/my_gae_appapp.yamlhelloworld.pyhandlers.py

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

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

Option 2, Handlers in a package; perhaps consider as your app gets larger
As your app gets larger you may wish to create a package in which to place your handlers:

/my_gae_app/handlers__init__.pyguestbook.pymain.pyapp.yamlhelloworld.py

Routes (in helloworld.py):

app = webapp2.WSGIApplication([('/', 'handlers.main.MainPage'),('/sign', 'handlers.guestbook.Guestbook')],debug=True)
https://en.xdnf.cn/q/73217.html

Related Q&A

Keras MSE definition

I stumbled across the definition of mse in Keras and I cant seem to find an explanation.def mean_squared_error(y_true, y_pred):return K.mean(K.square(y_pred - y_true), axis=-1)I was expecting the mean …

How do I adjust the size and aspect ratio of matplotlib radio buttons?

Ive been trying for hours to get the size and aspect ratio of a simple list of radio buttons correct with no success. Initially, import the modules:import matplotlib.pyplot as plt from matplotlib.widge…

App engine NDB: how to access verbose_name of a property

suppose I have this code:class A(ndb.Model):prop = ndb.StringProperty(verbose_name="Something")m = A() m.prop = "a string value"Now of course if I print m.prop, it will output "…

How do I load specific rows from a .txt file in Python?

Say I have a .txt file with many rows and columns of data and a list containing integer values. How would I load the row numbers in the text file which match the integers in the list?To illustrate, sa…

How to check in python if Im in certain range of times of the day?

I want to check in python if the current time is between two endpoints (say, 8:30 a.m. and 3:00 p.m.), irrespective of the actual date. As in, I dont care what the full date is; just the hour. When I c…

Python __class__()

In Pythons documentation __class__ is described as an attribute. In the object type (the metaclass), __class__ appears to be a method.If we do:>>> class Foo:pass>>> a = Foo() >>…

how to handle ever-changing password in sqlalchemy+psycopg2?

I inherited some code that uses sqlalchemy with psycopg2, which needs to run on AWS. RDS Postgres supports iam-based authentication, but the way it does it is rather kludgy: you request a temporary pas…

How to determine which compiler was requested

My project uses SCons to manage the build process. I want to support multiple compilers, so I decided to use AddOption so the user can specify which compiler to use on the command line (with the defaul…

Does pytest have anything like google tests non-fatal EXPECT_* behavior?

Im more familiar with the google test framework and know about the primary behavior pair they support about ASSERT_* vs EXPECT_* which are the fatal and non-fatal assert modes.From the documentation:Th…

Radon transformation in python

Here is a dummy code:def radon(img):theta = np.linspace(-90., 90., 180, endpoint=False)sinogram = skimage.transform.radon(img, theta=theta, circle=True)return sinogram # end defI need to get the sinogr…