因此,我按照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。提前致谢!
javascript大神给出的解决方案
var url = "/stock_data"
这必须是有效的URL,而不仅仅是Flask端点的路径。这意味着它必须以“ http://”或“ https://”开头,并包含一个域。出于开发目的,"http://localhost/stock_data"
将起作用。如果曾经将其部署到服务器,则将需要创建一个配置文件,以便可以根据所运行的环境来配置主机名。
将变量从Flask模板传递到Javascript文件的最佳方法是什么?这是我的代码我的webapp中有一个简单的视图:@webapp.route('/bars') def plot_d3_bars(): return render_template("bars.html", calendarMap = calendar…
从Chrome WebDriver访问getEventListeners(Python) - javascript目标:从Selenium WebDriver获取附加到节点的所有事件我正在使用selenium-python,我想执行一个javascript脚本(通过driver.execute_script('my js script')。该脚本使用getEventListeners,仅在Chrome上可用。我成功使用了driver = webdri…
Flask to Dygraph-如何传递数据? - javascript如果我有一个简单的Python时间数据系列,例如:graphdata = [] graphdata.append( [(datetime.date(2008, 5, 7)),75]) graphdata.append([(datetime.date(2008, 5, 8)), 85]) graphdata.append([(datetime.date(200…
读取客户端上的提要时出现NotAllowedException - javascript嗨,我是GetStream的新手,仍然在学习。这是我正在使用的压缩版本。我有一个python后端,可在其中创建用户令牌:client = stream.connect(...) token = client.create_user_token(id) return token 然后,我将令牌传递给js前端,在该前端中该令牌用于检索该用户的feed活动。我正在…
Ajax功能后没有数据传递到URL - javascript这是我使用Ajax将参数传递到另一个PHP页面的脚本。 $(document).on( "click",".btndriver", function() { var id = $(this).attr("id"); var nombre = $(this).attr("nombre…