Phil
Phil

Reputation: 14681

How to have different py files to handle different routes?

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:

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.

Upvotes: 2

Views: 3795

Answers (2)

Sean Vieira
Sean Vieira

Reputation: 160073

Webapp2 actually provides this out of the box - the WSGIApplication class instances have an instance of Router provided in their router attribute 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 LazyView class 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 LateLoader and Mapper. 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

from flask import Flask

app = Flask("yourapp")
# ... snip ...

urls.py

from .app import app
from hip_pocket import Mapper

mapper = Mapper(app)

mapper.add_url_rule("/", "index.index")
mapper.add_url_rule("/test", "index.test_endpoint", methods=["POST"])

mapper.add_url_rule("/say-hello/<name>",
                        "say_hello.greeter",
                        methods=["GET", "POST"])

index.py

def index():
    return "Hello from yourapp.index.index!"

def test_endpoint():
    return "Got a post request at yourapp.index.test_endpoint"

say_hello.py

def say_hello(name=None):
    name = name if name is not None else "World"
    return "Greetings {name}!".format(name=name)

run_app.py

from yourapp.app import app
from yourapp.urls import mapper
# We need to import the mapper to cause the URLs to be mapped.

if __name__ == "__main__":
    app.run()

Pull requests and issue reports are welcome!

Upvotes: 5

ntlarson
ntlarson

Reputation: 181

In my experience with flask, you cannot declare route configurations in a central file. Route handling is done via the use of route declarations. In my experience with python frameworks route handling is done at a more granular function level rather than a file level. Even in the frameworks that do have a more central route configuration setup, the routes are defined as being tied to a specific view/controller function not simply a python file.

As stated, each framework handles it differently. The three frameworks I have looked at in any detail, django, pyramid, and flask, all handle it differently. The closest to what you are looking for is django which has a urls.py file that you place all of your url configurations in, but again it points to function level items, not higher level .py files. Pyramid does a mix with part of a url declaration being put into the __init__.py file of the main module and the use of a decorator to correlate that named route to a function. And then you have flask, which you mentioned having looked at, which appears to use just decorators for "simplicity sake" as they are trying to reduce the number of overall files and configuration files that need to be used or edited to get an application from concept into served space.

Upvotes: 3

Related Questions