In [1]:
# Code attribution: Yiyin Shen, Tyler Caraza-Harter
# Imports
from flask import Flask, request, Response
from io import BytesIO
import matplotlib.pyplot as plt
import pandas
import json
In [ ]:
# Build a webpage that displays the grades as histogram as json
app = Flask("app")
@app.route("/")
def home():
return """
<html>
<body>
<h1>Quiz PNG</h1>
<img src="grades.png?col=Quiz" width="250"><br>
<h1>Project SVG</h1>
<img src="grades.svg?col=Project" width="250"><br>
</body>
</html>
"""
@app.route("/grades.png")
def grades_png():
if request.args and request.args["col"]:
table = pandas.read_csv("Grades.csv")
fig, ax = plt.subplots()
col = request.args["col"]
if col in ["Quiz", "Project", "Exam"]:
ax.hist(table[col])
ax.set_title(col + " Grades")
fake_file = BytesIO()
fig.savefig(fake_file, format = "png")
return Response(fake_file.getvalue(), headers = {"Content-Type": "image/png"})
else:
return "Column not found."
else:
return "Column not specified."
@app.route("/grades.svg")
def grades_svg():
if request.args and request.args["col"]:
table = pandas.read_csv("Grades.csv")
fig, ax = plt.subplots()
col = request.args["col"]
if col in ["Quiz", "Project", "Exam"]:
ax.hist(table[col])
ax.set_title(col + " Grades")
fake_file = BytesIO()
fig.savefig(fake_file, format = "svg")
return Response(fake_file.getvalue(), headers = {"Content-Type": "image/svg+xml"})
else:
return "Column not found."
else:
return "Column not specified."
@app.route("/grades.json")
def grades_json():
if request.args and request.args["col"]:
table = pandas.read_csv("Grades.csv")
col = request.args["col"]
if col in ["Quiz", "Project", "Exam"]:
table = pandas.read_csv("Grades.csv")
table = table.sample(frac = 1).reset_index(drop = True)
return Response(table[col].to_json(), headers = {"Content-Type": "application/json"})
else:
return json.dumps({"1":"0", "2":"0", "3":"0"})
else:
return "Column not specified."
app.run(host = "0.0.0.0", debug = False, threaded = False)
* Serving Flask app 'app' * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:5000 * Running on http://192.168.1.87:5000 Press CTRL+C to quit
In [ ]:
In [ ]: