python - 文件/函数引用在 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");
});
});
解决方案
推荐阅读
- python - 在 Python 中跳过不需要的日期时绘制时间与日期
- android - 如何使用 TypeScript 在 React Native 中编写正确的 `Keyboard.addListener`?
- c++ - 如何从 Terralang 扩展 C++ 类?
- database - 如何使用经典 ASP 将图像插入 Access 数据库
- c# - 更改文本文件时需要吐司/模式
- javascript - 如何多次调用javascript函数?
- java - NoSuchElementException:使用 hasNextLine 找不到行
- vue.js - 根据循环内的条件呈现不同的组件
- python - 是否有另一种方法来打印多个列表元素,其中列表索引每次打印都会增加给定数量?
- c++ - 如何检测第一次 Windows 启动?