I have been studying python for quite sometime now and very recently I decided to get into learning web development side of things. I have experience with PHP and PHP frameworks, along with ruby, where:
- Routes are defined in a (single) file and then in that file, each route is assigned to a model (py file) which will uniquely handle incoming requests matching that route.
How can I achieve this with flask AND webapp2?
I read the documentation and tutorial in full but it got me very confused. I just want A file where all routes and how should they be handled are set, and then each route request to be handled by its own model (python file).
All the examples lead to single file apps.
Thank you VERY MUCH, really. Please teach, kindly, in a simple way.
Webapp2 actually provides this out of the box – the
WSGIApplicationclass instances have an instance ofRouterprovided in theirrouterattribute that can be used for centralized URL maping as shown in the documentation.Flask doesn’t, but this is actually documented in its most basic form in Patterns for Flask: Lazy Loading. Using the
LazyViewclass it defines you can build out a system to provide central URL maps – either to a pre-defined symbol in each of your modules or to particular functions or class instances in your modules.I actually recently published a package (HipPocket) that provides wrappers to simplify getting started with this pattern. It provides two classes for this purpose
LateLoaderandMapper. Using HipPocket your central route configuration file could look something like this (this assumes a package layout similar to what is discussed here):app.py
urls.py
index.py
say_hello.py
run_app.py
Pull requests and issue reports are welcome!