Flask 与 Python 交互

问题描述 投票:0回答:1

我创建了一个小型 Flask 应用程序,当我运行它时,无论我选择哪个页面,浏览器的输出都不会改变。 我做错了什么?

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")
python flask
1个回答
0
投票

了解装饰器(

@app.route
是一个装饰器)最重要的一点是它们会影响它们上面的函数。如果您希望浏览器路由应用于您定义的函数,则必须将
@app.route
装饰器放在该函数的正上方。

@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")
© www.soinside.com 2019 - 2024. All rights reserved.