I have created a small Flask application and when I go to run it, no matter what page I select, the output to the browser never changes. What have I been doing wrong?
from flask import Flask
from perf_api_linux import PerfAPILinux
app = Flask(__name__)
@app.route('/')
@app.route('/index')
@app.route('/health')
@app.route('/RunTest')
def index():
return "<p>Ready To go on Home Page<p>"
# return "<p>Hello, World!</p>"
def health():
print ("Health Matters")
return "<p>Hello from HealthCheck<p>"
def RunTest():
print("Test Execution Beginning")
PerfAPILinux.execute()
return("Test Complete")
2
What’s important to understand about decorators (@app.route
is a decorator) is that they affect the function that they are immediately above. If you want a browser route to apply to a function that you’ve defined, you must put the @app.route
decorator right above that function.
@app.route('/')
@app.route('/index')
def index():
# This page can be visited by navigating to / or /index
return "<p>Ready To go on Home Page<p>"
# return "<p>Hello, World!</p>"
@app.route('/health')
def health():
# This page can be visited by navigating to /health
print ("Health Matters")
return "<p>Hello from HealthCheck<p>"
@app.route('/RunTest')
def RunTest():
# This function is triggered by navigating to /RunTest
print("Test Execution Beginning")
PerfAPILinux.execute()
return("Test Complete")