mercredi 23 septembre 2020

Split routes into multiple files klein python

I've a strange requirement. I want to split my routes which were under a single main.py into multiple files. I have one solution where I return file_name.app.resource() under '/' route to call routes from file_name.py file. I can't return multiple app.resources() under the '/' route for obvious reasons(a method can't return multiple values). It can be possible, provided I add a sub route. Can I achieve this without any sub-routes. Currently this is what i have:

from klein import Klein  #in main.py
from twisted.web.static import File

from routes import blueprint, main    # this module holds the other Klein app

app = Klein()


@app.route('/branch', branch=True)
def branchOff2(request):
    return routes_file.app.resource()



from klein import Klein   # in routes_file.py
app = Klein()

@app.route('/hello')
def index(request):
    return 'Hello from the subroutes main example'

with app.subroute('/blue') as sub:
    @sub.route('/first')
    def first(request):
        return 'first'

    @sub.route('/second')
    def second(request):
        return 'second'

    @sub.route('/third')
    def third(request):
        return 'third'
#
# if __name__ == '__main__':
#     app.run(host='localhost',port=8083)

Any help will be appreciated. Thanks!




Aucun commentaire:

Enregistrer un commentaire