To catch URL segments using application routes:
server.py
from flask import * app = Flask(__name__) @app.route("/") @app.route("/<segment1>") def main(segment1=None): if segment1 is not None: return("One segment: " + str(segment1)) else: return("Base URL, no segments.") app.run(debug=True)
To set a default value for a segment, which can be overwritten by the URL:
def main(segment1=None):With this server application running, try these URL paths:
server.py
from flask import * app = Flask(__name__) @app.route("/") @app.route("/<segment1>") @app.route("/<segment1>/<segment2>") def main(segment1=None, segment2=None): if segment1 is not None: if segment2 is not None: return("Two segments, 2nd is: " + str(segment2)) else: return("One segment: " + str(segment1)) else: return("Base URL, no segments.") app.run(debug=True)
With this server application running, try these URL paths: