How to modularize the URL handling functions?
Question:
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.
Answers:
If Python expects functions in a particular place then you can import them yourself from other files.
For example, if the web framework imports functions from code.py
:
def a():
pass
def b():
pass
def c():
pass
then you can replace code.py
with the following by importing the functions from elsewhere:
from mycode.somewhere import a, b
from mycode.some.other.place import c
As far as the web framework is concerned, your code.py
still contains the function a
, b
and c
but your code can certainly be organized in a different way.
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 :
from flask import Flask
app = Flask(__name__)
view.py :
from app import app
@app.route("/")
def hello():
return "Hello World!"
main.py :
from app import app
if __name__ == "__main__":
app.run()
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 :
- using
add_view
: the first argument is the dotted path to the function. Put it anywhere, and put the right path here.
- Using
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.
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.
If Python expects functions in a particular place then you can import them yourself from other files.
For example, if the web framework imports functions from code.py
:
def a():
pass
def b():
pass
def c():
pass
then you can replace code.py
with the following by importing the functions from elsewhere:
from mycode.somewhere import a, b
from mycode.some.other.place import c
As far as the web framework is concerned, your code.py
still contains the function a
, b
and c
but your code can certainly be organized in a different way.
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 :
from flask import Flask
app = Flask(__name__)
view.py :
from app import app
@app.route("/")
def hello():
return "Hello World!"
main.py :
from app import app
if __name__ == "__main__":
app.run()
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 :
- using
add_view
: the first argument is the dotted path to the function. Put it anywhere, and put the right path here. - Using
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.