简体   繁体   English

在Flask中运行python脚本

[英]running a python script within Flask

So I have this simple python script running on Flask that I'd like to pass variables to with an ajax jQuery request. 因此,我在Flask上运行了这个简单的python脚本,希望通过ajax jQuery请求将变量传递给该脚本。 I might be missing something obvious but I can't get it to work properly. 我可能会遗漏一些明显的东西,但是我无法使其正常工作。

@app.route('/test', methods=['GET', 'POST'])
def test():
    my_int1 = request.args.get('a')
    my_int2 = request.args.get('b')
    my_list = [my_int1, my_int2]
    with open('my_csv.csv', 'wb') as myfile:
        wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
        wr.writerow(my_list)
    return '' #does one have to have an return even tho it is just a script?

So, above will work fine when just passing parameters to the URL field: http://127.0.0.1:5000/test?a=10&b=25 however, trying this in the chrome console will not yield any output at all: 因此,仅在将参数传递到URL字段时,上述方法就可以正常工作: http://127.0.0.1:5000/test?a=10&b=25 : http://127.0.0.1:5000/test?a=10&b=25 : http://127.0.0.1:5000/test?a=10&b=25但是,在chrome控制台中尝试此操作根本不会产生任何输出:

$.ajax({
  method: "POST",
  url: "127.0.0.1:5000/test",
  data: {a: 10, b: 25},
  dataType: "script"
});

What am I missing and why does the above jquery ajax request not work? 我缺少什么,为什么上面的jquery ajax请求不起作用? $.ajax is not a function(…)

Please refer to http://flask.pocoo.org/docs/0.11/api/#flask.Request 请参考http://flask.pocoo.org/docs/0.11/api/#flask.Request

request.args : If you want the parameters in the URL
request.form : If you want the information in the body (as sent by a html POST form)
request.values : If you want both

Try this 尝试这个

@app.route('/test', methods=['GET', 'POST'])
def test():
    my_int1 = request.values.get('a')
    my_int2 = request.values.get('b')

Please, try with the following code and let me know if that's what you are looking for: 请尝试以下代码,让我知道您是否正在寻找它:

from flask import Flask, request
import csv

app = Flask(__name__)


@app.route('/test', methods=['GET', 'POST'])
def test():
    if request.is_xhr:
        a = request.json['a']
        b = request.json['b']
    else:
        a = request.args.get('a')
        b = request.args.get('b')
    my_list = [a, b]
    with open('my_csv.csv', 'wb') as myfile:
        wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
        wr.writerow(my_list)
    return '''
<html>
  <head>
    <title>This is just a testing template!</title>
  </head>
  <body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  </body>
</html>
'''


if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)

To test from your Chrome console, you need first to go to http://127.0.0.1:5000/test (after that, jQuery will be available in your browser) then you run the following code: 要从您的Chrome控制台进行测试,您首先需要转到http://127.0.0.1:5000/test (之后,浏览器中将提供jQuery),然后运行以下代码:

$.ajax({
    url: "http://127.0.0.1:5000/test",
    method: "POST",
    headers: {'Content-Type':'application/json'},
    data: JSON.stringify({a: 10, b: 25}),
    success: function(data){console.log('result: ' + data);}
});

If you have a Cross-origin issue, add also the below code to your app.py file: 如果您遇到跨域问题,请将以下代码也添加到您的app.py文件中:

@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
    response.headers.add('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS')
    return response

似乎您在该HTML页面上的jQuery参考未加载或不正确,请在HTML页面的<head> here </head>部分中添加此参考。

<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM