[英]how can I get variable from javascript using flask
So I have set up app.py, index.js, index.html in appropriate folder as flask suggests. 因此,我按照flask的建议在相应的文件夹中设置了app.py,index.js,index.html。 Index.html gets rendered as when app.py runs then index.html runs index.js which grabs input data from user.
Index.html会在app.py运行时呈现,然后index.html运行index.js,后者会从用户那里获取输入数据。 I am trying to send this input and send it to python where I can call an API, grab data, and work with it however I cannot think of a way to do this.
我试图发送此输入并将其发送到python,在这里我可以调用API,获取数据并使用它,但是我想不出一种方法。
my app.py: 我的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()
and here is my javascript code: 这是我的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){
})
}
Everything works fine, when I console log inputted_label at the end of function submitted it works. 一切正常,当我在提交的函数末尾控制台日志inputted_label时,它正常工作。 Now I want to send inputted_label variable to /stock_data.
现在,我想将inputted_label变量发送到/ stock_data。 Thanks in advance!
提前致谢!
var url = "/stock_data"
This needs to be a valid URL, not just the path to the Flask endpoint. 这需要是一个有效的URL,而不仅仅是Flask端点的路径。 That means it must start with "http://" or "https://" and include a domain.
这意味着它必须以“ http://”或“ https://”开头,并包含一个域。 For development purposes,
"http://localhost/stock_data"
will work. 出于开发目的,
"http://localhost/stock_data"
将起作用。 If you ever deploy this to a server, you will want to create a configuration file so that the host name can be configured depending on what environment you are running in. 如果曾经将其部署到服务器,则将需要创建一个配置文件,以便可以根据所运行的环境来配置主机名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.