我正在尝试使用flask创建我的第一个脚本。
这是我的代码:
from flask import Flask
from flask import Blueprint, request
prediction_app = Blueprint('prediction_app', __name__)
@prediction_app.route('/health', methods=['GET'])
def health():
if request.method == 'GET':
return 'ok'
def create_app() -> Flask:
"""Create a flask app instance."""
flask_app = Flask('ml_api')
# import blueprints
flask_app.register_blueprint(prediction_app)
return flask_app
application = create_app()
if __name__ == '__main__':
application.run()
我以 python run.py 的身份运行此代码,并且得到 “在http://127.0.0.1:5000/上运行” 。 我转到此链接,而不是得到一个出现下一个错误的页面,而不是 “确定” :
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
命令promt给出以下输出:
127.0.0.1 - - [17/Jun/2020 16:59:25] "[33mGET / HTTP/1.1[0m" 404 -
问题出在哪里?
答案 0 :(得分:2)
我没有定义默认路由(import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) {
HBox controls = new HBox(5);
controls.getChildren().add(new TextField());
for (int i = 1 ; i <=5 ; i++) {
String text = "Button "+i ;
Button button = new Button(text);
button.setOnAction(e -> System.out.println(text));
controls.getChildren().add(button);
}
Scene scene = new Scene(controls, 600, 400);
scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
if (e.getCode() == KeyCode.ENTER) {
if (scene.getFocusOwner() instanceof Button) {
Button button = (Button) scene.getFocusOwner();
button.fire();
}
}
});
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
);您是否尝试将浏览器指向/
?这就是您定义的路线。
(顺便说一下,{http://localhost:5000/health
和localhost
通常是等效的...)