如何使用python27 app engine webapp2框架来组织文件 [英] how to organise files with python27 app engine webapp2 framework
问题描述
我已经通过了python27和应用程序引擎的入门啧啧了:的https: //developers.google.com/appengine/docs/python/gettingstartedpython27/
I've gone through the getting started tut for python27 and app engine: https://developers.google.com/appengine/docs/python/gettingstartedpython27/
到tut结束时,所有的类都在相同的文件(helloworld.py),然后你和你配置路由器将URL路径指向文件底部的一个类:
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)
tut并未涵盖的是随着我的应用程序的增长,我该如何规划我的类/文件。例如,我会将MainPage放在一个单独的文件中,然后在helloworld.py文件中调用'import MainPage'并将路由添加到WSGIApplication?有没有比这更自动化的东西?我应该怎样调用MainPage文件并将它存储在哪里?
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?
推荐答案
最好在应用程序启动时导入所有处理程序将利用 webapp2的懒处理程序加载,该程序将加载模块/根据需要安装包。
所以你有几个选择:
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:
选项1,模块中的处理程序 >
在 helloworld.py
文件的同一级别的另一个文件(模块)中放置 MainPage
:
Option 1, Handlers in a module
Place MainPage
in another file (module) at the same level as your helloworld.py
file:
/my_gae_app
app.yaml
helloworld.py
handlers.py
在你的路由中(在 helloworld.py
中),你应该这样做:
And in your routing (in helloworld.py
) you would do:
app = webapp2.WSGIApplication([('/', 'handlers.MainPage'),
('/sign', 'handlers.Guestbook')],
debug=True)
选项2,包装中的处理程序;或许考虑你的应用程序变得更大
随着你的应用程序变大,你可能希望创建一个放置你的处理程序的包:
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__.py
guestbook.py
main.py
app.yaml
helloworld.py
路径( helloworld.py
):
app = webapp2.WSGIApplication([('/', 'handlers.main.MainPage'),
('/sign', 'handlers.guestbook.Guestbook')],
debug=True)
这篇关于如何使用python27 app engine webapp2框架来组织文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!