即使我在html中调用CDN,我也需要安装吗?

时间:2019-05-13 06:49:54

标签: javascript python html flask

我正在使用flask来构建在线应用程序。我正在使用d3.js抓取用户输入,然后将其发送到app.py,它将使用此输入进行api调用并抓取适当的数据,然后它将jsonfied数据返回给javascript,以在正确的html标签中绘制图,但是它保持给我错误:错误:提供的DOM元素为空或未定义

app.py:

import os
from flask import Flask, render_template, jsonify, request, redirect, url_for
from alpha_vantage.timeseries import TimeSeries
import plotly
# import plotly.plotly as py
# import plotly.graph_objs as go


app = Flask(__name__)

api_key = "ssdqwjhwq"

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

@app.route("/stockchart/<label>")
def stock_data(label):
    ts = TimeSeries(key=api_key, output_format='pandas')
    df, meta_deta = ts.get_daily(label, outputsize="full")
    df.columns = ["open", "high", "low", "close", "volume"]
    data = [{"dates":list(df.index.values)},
            {"close": list(df.close)}]
    return jsonify(data)

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

我的html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Stock Screener</title>
    <!-- Importing all cdn -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.5.0/d3.min.js"></script>
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
    <div class="text-center">
        <h2>Stock Screener</h2>
        <h5>Interactive chart with predictions using machine learning</h5>
    </div>

    <div>
        <!-- Input for stock label -->
        <form>
            <label for="stockInput">Enter stock label</label>

            <!-- use js to store input into a variable. -->
            <input type="text" id="stockInput" name="stock_label" placeholder="Label">

            <!-- submit button-->
            <input type="submit" value="submit" id="stocklabelsubmit">
        </form>
    </div>
    <div class="chart">
    </div>

</body>
<script src="../static/js/index.js"></script>
</html>

还有我的JavaScript

// give reference to submit button
// Upon click, submitted function will run

d3.select("#stocklabelsubmit").on("click", submitted);

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

    // grab label inputted.
    var inputted_label = d3.select("#stockInput").node().value;
    // refresh input box
    d3.select("#stockInput").node().value = "";
    // pass inputted_label to plot function
    Plot(inputted_label);
};

function Plot(input){
    var url = `/stockchart/${input}`
    // when this function is called call /stock_data function!!
    // data is what is returned from python
    d3.json(url).then(function(data){
        console.log(data);
        console.log(data[0]);
        console.log(data[0].dates);
        var trace = {
            x:data[0].dates,
            y:data[1].close,
            type: "scatter"
        };

        var layout {
            title:"Stock chart",
            xaxis:{
                title:"dates"
            },
            yaxis:{
                title:"closing price"
            }
        };
        var data = [trace];
        // var loc_plot = document.getElementById("chart");

        Plotly.newPlot("chart", data, layout);
    });
};

错误告诉我,js行33中存在错误= Plotly.newPlot(“ chart”,data,layout);我不确定如何解决此问题,直到在进行控制台日志正确记录之前,一切似乎都工作正常。有什么帮助吗?谢谢!

1 个答案:

答案 0 :(得分:0)

根据the documentationgraphDiv的{​​{1}}参数是指DOM节点或DOM节点的字符串ID。

在上面的代码中,您似乎没有提供任何代码。您提供的是DOM节点的类,但没有提供实际的DOM节点或DOM节点的ID。这可能解释了关于空或未定义DOM节点的错误。

尝试将Plotly.newPlot()更改为<div class="chart">(或其他一些ID),或使用<div class="chart" id="chart">获取节点,然后将其传递给DOM节点,看看是否可行。