From 230f242af519b16fd69fcfb2c4c1a48e96749ecf Mon Sep 17 00:00:00 2001 From: crunk Date: Mon, 21 Dec 2020 18:58:46 +0100 Subject: [PATCH] cleaned up code with flake8 and black --- library/csvparser/csvparser.py | 22 ++++++++++++++++++---- library/page.py | 13 ++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/library/csvparser/csvparser.py b/library/csvparser/csvparser.py index c66e355..e4d8d8f 100644 --- a/library/csvparser/csvparser.py +++ b/library/csvparser/csvparser.py @@ -17,12 +17,15 @@ def getpublications(): csv_as_dict = csv.DictReader(libcsv) publications = {} for row in csv_as_dict: - pubinfo = {"Title" : row["Publication"], - "Author" : row["Author"], - "Type" : row["Type"]} + pubinfo = { + "Title": row["Publication"], + "Author": row["Author"], + "Type": row["Type"], + } publications[row["Id"]] = pubinfo return publications + def gettypes(): libcsv = open(os.path.join(script_dir, "varlib.csv"), "r") with libcsv: @@ -32,9 +35,20 @@ def gettypes(): lowertype = row["Type"].lower() if lowertype not in listoftypes: listoftypes.append(lowertype) - #listoftypes = [t.title() for t in listoftypes] return listoftypes + +def getyears(): + libcsv = open(os.path.join(script_dir, "varlib.csv"), "r") + with libcsv: + csv_as_dict = csv.DictReader(libcsv) + listofyears = [] + for row in csv_as_dict: + if row["Year"] not in listofyears: + listofyears.append(row["Year"]) + return listofyears + + # test = getpublications() # for ids, pubinfo in test.items(): # print(pubinfo["Title"]) diff --git a/library/page.py b/library/page.py index a0ace4f..642ee67 100644 --- a/library/page.py +++ b/library/page.py @@ -4,7 +4,7 @@ import flask from flask import render_template -from csvparser.csvparser import getpublications, gettypes +from csvparser.csvparser import getpublications, gettypes, getyears APP = flask.Flask(__name__, static_folder="static") @@ -14,15 +14,22 @@ def index(): """Main route""" # parse csv, render template with a few elements from the csv pubtypes = gettypes() + pubyears = getyears() publicatons = getpublications() - return render_template("index.html", publications=publicatons, pubtypes=pubtypes) + template = render_template( + "index.html", + publications=publicatons, + pubtypes=pubtypes, + pubyears=pubyears + ) + return template @APP.route("/") def show_book(publicationID): """route for a publication""" # parse csv, render template with full list. - return render_template("publication.html", publication=publication) + return render_template("publication.html") @APP.route("/")