简体   繁体   English

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

[英]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.

相关问题 如何从 Javascript 中读取 flask session 变量? - How can I read flask session variable from Javascript? 如何将变量从 Python Flask 转移到 JavaScript? - How can I transfer a variable from Python Flask to JavaScript? 如何将 JavaScript 变量发送回 flask? - How can i send a JavaScript variable back to flask? 我如何使用 AJAX 和 Flask 将诸如变量之类的信息从 javascript 传输到 python - How would I transfer information like a variable from javascript to python using AJAX and Flask 如何从 css 变量中获取值并使用 javascript 多次(循环)自动更改它 - How can i get value from css variable and change it automatically in several times (loop) using javascript 使用 JavaScript 提交表单时,如何从用户那里获取数量变量? - How can I get quantity variable from user when subnmitting a form using JavaScript? 如何使用JavaScript在PHP GET变量上运行if语句? - How can I run an if statement on a PHP GET variable using JavaScript? 如何从从 javascript 发送到 flask 的发布请求中获取返回值 - How can I get a return value from a post request sent from javascript to flask 如何从从 javascript 发送到 flask 的 post 请求中获取返回值 - How can I get a return value from a post request sent from javascript to flask 我如何从javascript获取变量到PHP或HTML - How i can get a variable from javascript to PHP or HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM