====== Esplicitare le rotte con Bottle ====== Autore: **//Fabio Di Matteo//** \\ Ultima revisione: **// 21/10/2022 - 08:34 //** // // In certi casi puo' essere utile configurare le rotte in altri moduli Python. A tal fine è sicuramente meglio non usare i decoratori, ma il metodo **route**. Quello che ci interessa è nella funzione **setup_routing()** . Come mostrato nella guida ufficiale: [[http://bottlepy.org/docs/dev/routing.html#explicit-routing-configuration|Explicit routing configuration]] ... 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