烧瓶& angular - 将Python输出发送到URL

时间:2017-02-23 17:04:23

标签: python angularjs flask

我正在编写一个小型Web应用程序,以测试我对集成Angular 1和Flask的理解。当我尝试将我的Python函数的输出发送到URL Localhost / synthesize_data时,我看到一个Flask错误:'NoneType Object不是可订阅的。'它试图再次运行我的Python脚本,但我只想让它显示输出该功能应该已经生成。

HTML with Angular:

<!doctype html>
<html>
    <head>

        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
        <script src="http://code.angularjs.org/1.5.3/angular-route.min.js"></script>

        <script>

            var myApp = angular.module('myApp', [
             'ngRoute',
            ]);

            myApp.config(['$routeProvider',
                 function($routeProvider) {
                     $routeProvider.
                         when('/', {
                             templateUrl: '/static/partials/index.html',
                         }).
                         otherwise({
                             redirectTo: '/'
                         });
                }]);

            myApp.controller('formController', ['$scope', '$http', function($scope, $http) {
                console.log('HA');
                $scope.formData = {};
            $scope.processForm=function() {
                console.log("righthere")
                $http({
                    method: 'POST',
                    url : '/synthesize_data',
                    data : $scope.formData,
                    headers: {'Content-Type': 'application/json'}
                })
                .success(function(data){
                    console.log(data);
                })
            };
        }])

        </script>

        <!--<link rel="stylesheet" href="/static/css/style.css" />-->
    </head>
    <body ng-app="myApp" ng-controller="formController">
                <h1>Data Synthesizer Startup Page</h1>
        <div>
            <form ng-submit = "processForm()">
                <div id = "name-group" class = "form-group">
                    <label>Number of Rows to Create </label>
                    <input type = "text" name = "name" class = "form-control" placeholder = "Enter valid number input" ng-model = "formData.name">
                    <span class = "help-block"></span>
                </div>

                <button type = "submit" class = "btn btn-success btn-lg btn-block">
                    <span class = "glyphicon glyphicon-flash"></span> Submit
                </button>
            </form>
    <!--<div ng-view></div>-->

    </body>
</html>

Python代码(使用hello()初始化程序):

from flask import Flask, send_file, request
from data_synthesis import *
app = Flask(__name__)

@app.route("/synthesize_data", methods=['GET', 'POST'])
def datasynthesize():
    print ("Calling synthesizer for ", request.json['name'], " rows.")
    main(int(request.json['name']))
    return "TEST"

@app.route("/")
def hello():
    print ("Python up and running.")
    return send_file("templates/start.html")

if __name__ == "__main__":
app.run(debug=True)

我想要的是在运行此程序并导航到Localhost / synthesize_data时在屏幕上看到“TEST”。相反,它会尝试在没有正确输入的情况下再次运行我的Python程序,这会引发错误。

1 个答案:

答案 0 :(得分:0)

在提交表单数据时执行程序,您需要在视图函数中添加条件。代码的方式取决于将name值作为请求的一部分。

如果mimetype不是 application / json ,则

Request.jsonNone。这就是抛出&#39; NoneType&#39;当您发出GET请求时(例如,当您导航到 localhost / synthesize_data 时),对象不可订阅错误。根据文档,更好的方法是使用Request.get_json()

@app.route("/synthesize_data", methods=['GET', 'POST'])
def datasynthesize():
    data = request.get_json()
    if data:
        print ("Calling synthesizer for ", data['name'], " rows.")
        main(int(data['name']))
    return "TEST"