繁体   English   中英

我如何使用flask从javascript获取变量

[英]how can I get variable from javascript using flask

因此,我按照flask的建议在相应的文件夹中设置了app.py,index.js,index.html。 Index.html会在app.py运行时呈现,然后index.html运行index.js,后者会从用户那里获取输入数据。 我试图发送此输入并将其发送到python,在这里我可以调用API,获取数据并使用它,但是我想不出一种方法。

我的app.py:

import os
from flask import Flask, render_template, jsonify, request, redirect
app = Flask(__name__)

# This will run upon entrance
@app.route("/")
def home():
    return render_template("index.html")

@app.route("/stock_data")
def get_stock_data():
    # called from index.js Plot function

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

这是我的JavaScript代码:

console.log("everythin works fine.")
d3.select("#stocklabelsubmit").on("click", submitted)

function submitted(){
    d3.event.preventDefault();

    // grab label inputted.
    var inputted_label = d3.select("#stockInput").node().value;

    d3.select("#stockInput").node().value = "";

    Plot(inputted_label);
};


function Plot(input){
    var url = "full url"
    // when this function is called call /stock_data function!!
    // data is what is returned from python
    d3.json(url).then(function(data){

    })
}

一切正常,当我在提交的函数末尾控制台日志inputted_label时,它正常工作。 现在,我想将inputted_label变量发送到/ stock_data。 提前致谢!

var url = "/stock_data"

这需要是一个有效的URL,而不仅仅是Flask端点的路径。 这意味着它必须以“ http://”或“ https://”开头,并包含一个域。 出于开发目的, "http://localhost/stock_data"将起作用。 如果曾经将其部署到服务器,则将需要创建一个配置文件,以便可以根据所运行的环境来配置主机名。

暂无
暂无

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

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