0

I'm trying to call a function in python (using @app.route) from my webpage. However, I keep getting an Error 500 and I cannot figure out why. Here are some relevant code snippets:

server.py: python server. In particular, I am trying to call the "chart" function and this request is returning an error.

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 :("

Here is the relevant snippets from the 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");
        });
    });

Here's a screenshot of the error message: enter image description here

zainosaurus
  • 148
  • 8
  • 1
    500 error code represents an error in the server-side code. Check the logs, and post the relevant excerpts from it, which you cannot understand. – 31piy Jul 24 '18 at 04:28
  • The console log doesn't specify anything other than what I showed in the screenshot. Perhaps I am looking in the wrong place, could you please specify? – zainosaurus Jul 24 '18 at 04:41
  • 1
    I meant to observe the server logs, not your browser's logs. – 31piy Jul 24 '18 at 04:42
  • Thank you, I found the error by checking the server logs as you suggested. The problem was that python wasn't able to open the file as it was not located under the localhost root. – zainosaurus Jul 24 '18 at 05:53

0 Answers0