1

With Python Bottle microframework, I'm using:

@route('/')
@view('index.tpl')
def index():
    context = {'request': request, 'custom': ''}
    return (context)

@route('/test')
@view('test.tpl')
def test():
    context = {'request': request, 'custom': ''}
    return (context)

@route('/test2')
@view('test2.tpl')
def test2():
    context = {'request': request, 'custom': ''}
    return (context)

but there is some redundancy in the code. Is there a way to have a simpler routing in this case?

Note: I'm thinking about moving to Flask soon, so I would be interested for a solution working for both.

davidism
  • 121,510
  • 29
  • 395
  • 339
Basj
  • 41,386
  • 99
  • 383
  • 673
  • Note that `@thing` on `def f(...):` is just syntactic sugar for writing the function then doing `f = thing(f)`, so you could define the function separately then do e.g. `index = route('/')(view('index.tpl')(index))`. Or feasibly you can apply multiple `@route` decorators to the same function; looks like [you can in Flask](https://stackoverflow.com/q/14023664/3001761). – jonrsharpe Feb 23 '18 at 23:52
  • @jonrsharpe I'm understanding 50%, but not fully, can you post an answer about this? Thank you in advance! – Basj Feb 24 '18 at 00:06
  • Maybe start by reading up on decorators, then: https://docs.python.org/3/glossary.html#term-decorator – jonrsharpe Feb 24 '18 at 00:08
  • @jonrsharpe This one helped me a lot in the past: https://stackoverflow.com/a/739665/1422096 – Basj Feb 24 '18 at 00:10

0 Answers0