...
def setup_routing(app):
app.route('/', ['GET', 'POST'], home)
app.route('/get/', ['GET', 'POST'], get)
...
Come possiamo vedere si esplicita rispettivamente rotta, metodi http, e la funzione di callback associata alla rotta. Di seguito uno script che mette a confronto alcuni modalita' per la definizione delle rotte.
**myserver.py**
from bottle import Bottle,static_file, route, run,os
from routes import *
app = Bottle()
@app.route('/static/')
def send_js(filename):
return static_file(filename,
root=os.path.dirname(os.path.realpath(__file__))+'/static/')
@app.route('/static/sub/')
def send_js(filename):
return static_file(filename,
root=os.path.dirname(os.path.realpath(__file__))+'/static/sub')
@app.route('/hello')
def hello(route='/hello'):
return "Hello World!"
def setup_routing(app):
app.route('/', ['GET', 'POST'], home) #function home is in "routes.py"
app.route('/get/', ['GET', 'POST'], get)
setup_routing(app)
run(app, host='localhost', port=8080,reloader=True)
Qui invece il modulo routes.py che contiene le nostre rotte.
**routes.py**
def home():
r='''
Prova
Prova rotte esplicite
'''
return r
def get(name):
r=''+name+'
'
s='Paragrafe
'
yield r
yield s