With Python web application frameworks such as Flask, webapp2 and Pyramid, how can each route-handling-function can have its own py file? I don’t want all these functions piled up together in a single file structure. What do I do to make this work in Flask, webapp2 and Pyramid?
Thank you.
In flask and pyramid (don’t know about webapp2, but probably the same), route-handling-function (let’s call them views) are nothing but function, that are register to an app registry.
In flask, you can put your view anywhere, as long as you register it :
app.py :
view.py :
main.py :
Same thing for pyramid. I won’t go into the details. The registering process is different, but the idea is the same. But it anywhere, as long as you register it. There are two way to register views :
add_view: the first argument is the dotted path to the function. Put it anywhere, and put the right path here.scan: the first argument is a package that is scanned to find the views. Just make sure all your views are in the package and its subpackages, and everything will work.