首页 > 解决方案 > 文件/函数引用在 Flask 应用程序中不起作用(错误 500)

问题描述

我正在尝试从我的网页调用 python 中的函数(使用@app.route)。但是,我不断收到错误 500,我不知道为什么。以下是一些相关的代码片段:

server.py:python服务器。特别是,我试图调用“图表”函数,这个请求返回一个错误。

from flask import Flask, flash, redirect, render_template, request, session, abort, jsonify


app = Flask(__name__)

# Load Main Page
@app.route("/")
def load_index():
    return render_template('index.html')

# Return Chart Data
@app.route("/chart")
def chart():
    file = open("~/1-1938.csv")
    file_contents = file.readlines()
    info = file_contents[121]
    info = info.split(",")
    for i in range(len(info)):
        info[i] = info[i].strip('"')

    # creating json object
    return jsonify (
        name = info[0],
        mean_temp = int(info[4]),
        high_temp = int(info[6]),
        low_temp = int(info[8])
    )


if __name__ == "__main__":
    try:
        app.run(host='0.0.0.0', port=80)
    except:
        print "Server Crashed :("

以下是 index.html 中的相关片段:

 // Button Click (Populating Chart Data)
    $("#populate").click(function() {
        $.getJSON($SCRIPT_ROOT + '/chart', function(received_data) {
            config.data.labels=["Jan 1938"];
            config.data.datasets[0].data = [received_data.high_temp];
            config.data.datasets[1].data = [received_data.low_temp];
            config.data.datasets[2].data = [received_data.mean_temp];
            window.myLine.update();
            console.log("Done");
        });
    });

这是错误消息的屏幕截图: 在此处输入图像描述

标签: pythonjqueryjsonajaxflask

解决方案


推荐阅读