Cleaning up code

This commit is contained in:
Eljakim Herrewijnen 2022-09-22 17:15:32 +02:00
parent f8259a6bfd
commit 3f0c355578
5 changed files with 58 additions and 56 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,17 +1,16 @@
import io
from flask import Flask, render_template from flask import Flask, render_template
app = Flask("Project Numeri") app = Flask("Project Numeri")
from plots import Cijferlijst #from plots import Cijferlijst
import base64
cf = Cijferlijst() #cf = Cijferlijst()
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure from matplotlib.figure import Figure
from plots import plot
@app.route('/') @app.route('/')
def index(): def index():
return render_template('index.html', title="joe", imgdata=None) return render_template('index.html', title="joe", imgdata=None)
def RunWeb(): def RunWeb():
app.run(debug=True, port=5002) app.run(debug=True, port=5002)

31
main.py
View File

@ -1,39 +1,18 @@
# based on https://gist.github.com/rduplain/1641344 # based on https://gist.github.com/rduplain/1641344
import random import random
import io from io import BytesIO
import base64
from flask import Flask, make_response, request from flask import Flask, make_response, request
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure from matplotlib.figure import Figure
import plots
app = Flask(__name__) app = Flask(__name__)
@app.route('/') @app.route('/')
def root() : def plot_figure():
return "<form action='/plot.png' method='post'><input type='text' name='data'><input type='submit' value='submit'>" return plots.plot()
@app.route('/plot.png', methods = ['GET', 'POST'])
def plot() :
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
if request.method == 'GET' :
xs = range(100)
ys = [random.randint(1, 50) for x in xs]
if request.method == 'POST' :
ys = map( float, request.form['data'].strip().split(',') )
xs = range(len(ys))
axis.plot(xs, ys)
canvas = FigureCanvas(fig)
output = io.BytesIO()
canvas.print_png(output)
response = make_response(output.getvalue())
response.mimetype = 'image/png'
return response
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, port=5002) app.run(debug=True, port=5002)

View File

@ -1,32 +1,56 @@
import pandas as pd
import numpy as np
GRADE_LIST = "Cijfers-HerrewijnenJonathan.csv" from io import BytesIO
from flask import Flask, render_template
# app = Flask("Project Numeri")
from flask import Flask, make_response, request
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import base64
class Cijferlijst: app = Flask(__name__)
def __init__(self, grade_list=GRADE_LIST):
self.grade_list = grade_list
self.ps = pd.read_csv(GRADE_LIST, skiprows=2, sep=';')
''' def plot():
Get all data from a subject and a (yearly) period: # Generate the figure **without using pyplot**.
GetSubjectByYear("godsdienst", "2010/2011") fig = Figure()
''' ax = fig.subplots()
def GetSubjectByYear(self, subject, year): ax.plot([1, 2])
if(type(subject) == str and type(year) == str): # Save it to a temporary buffer.
select_year = self.ps[self.ps['Schooljaar(Voortgangsdossier)'] == year] buf = BytesIO()
select_subject = select_year[select_year["Vak(Voortgangsdossier)"] == subject] fig.savefig(buf, format="png")
# Embed the result in the html output.
#filter period and rapport data = base64.b64encode(buf.getbuffer()).decode("ascii")
rapports = select_subject[select_subject['Cijfertype(Voortgangsdossier)'] != "Periodegemiddelde"] return f"<img src='data:image/png;base64,{data}'/>"
return rapports[rapports ['Cijfertype(Voortgangsdossier)'] != "Rapportcijfer"]
else:
return False
def iets(self): # import pandas as pd
print(self.grade_list) # import numpy as np
def CalculateMedian(self, subject, year): # GRADE_LIST = "Cijfers-HerrewijnenJonathan.csv"
# Calculates average for all scores without weight!
Grades = self.ps['Cijfer(Voortgangsdossier)'].loc[:] # class Cijferlijst:
# def __init__(self, grade_list=GRADE_LIST):
# self.grade_list = grade_list
# self.ps = pd.read_csv(GRADE_LIST, skiprows=2, sep=';')
# '''
# Get all data from a subject and a (yearly) period:
# GetSubjectByYear("godsdienst", "2010/2011")
# '''
# def GetSubjectByYear(self, subject, year):
# if(type(subject) == str and type(year) == str):
# select_year = self.ps[self.ps['Schooljaar(Voortgangsdossier)'] == year]
# select_subject = select_year[select_year["Vak(Voortgangsdossier)"] == subject]
# #filter period and rapport
# rapports = select_subject[select_subject['Cijfertype(Voortgangsdossier)'] != "Periodegemiddelde"]
# return rapports[rapports ['Cijfertype(Voortgangsdossier)'] != "Rapportcijfer"]
# else:
# return False
# def iets(self):
# print(self.grade_list)
# def CalculateMedian(self, subject, year):
# # Calculates average for all scores without weight!
# Grades = self.ps['Cijfer(Voortgangsdossier)'].loc[:]