From 6d6a6a8ac331135aa26320c532f47f25872d4243 Mon Sep 17 00:00:00 2001 From: Rasmus Luha Date: Thu, 17 Mar 2022 18:04:24 +0200 Subject: fix importing modules, add readme --- api/main.py | 2 +- api/routers/get.py | 37 ++++++++++++++++++++++++++++--------- api/utils.py | 4 ++-- 3 files changed, 31 insertions(+), 12 deletions(-) (limited to 'api') diff --git a/api/main.py b/api/main.py index 8068243..e4d90f8 100644 --- a/api/main.py +++ b/api/main.py @@ -1,5 +1,5 @@ from fastapi import FastAPI -from routers import get +from .routers import get import pandas as pd app = FastAPI() diff --git a/api/routers/get.py b/api/routers/get.py index bbbd26d..7842cc3 100644 --- a/api/routers/get.py +++ b/api/routers/get.py @@ -1,18 +1,37 @@ from fastapi import FastAPI, APIRouter import numpy as np import pandas as pd -import sys - -#sys.path.append("/home/rasmus/Proge/STACC/api") -from .. import utils router = APIRouter(tags=["Get requests"]) + +# Making relative paths. On windows slashes would be backwards +# Though, it is probably not a good way. And the API has to be activated +# from the project dir, for the paths to match. +relPathTeams = "AllAboutData/Data/NBAteams.csv" +relPathPlayers = "AllAboutData/Data/Players/" + + + +def getTeamNames(): + ''' + Returns dictionary with fetched teams' names as keys + and full_names as values. + ''' + + teamsDf = pd.read_csv(relPathTeams).to_dict() + team_names = {} + for el in range(len(teamsDf["name"])): + team_names.update({teamsDf["name"][el] : teamsDf["full_name"][el]}) + return team_names + + + @router.get("/teams") def getTeams(): - # Getting the data from csv files and then converting into dict to be send out on get request - teamsDf = pd.read_csv(utils.relPathTeams).to_dict() + # Converting the data and converting into dict to send + teamsDf = pd.read_csv(relPathTeams).to_dict() teams_dict = {} for el in range(len(teamsDf["name"])): @@ -29,12 +48,12 @@ def getTeams(): @router.get("/players/{team_name}") def getPlayers(team_name: str): - teamNames = utils.getTeamNames() + teamNames = getTeamNames() team_name = team_name.capitalize() # Capitalizing in case url is given as lowercase if team_name in teamNames.keys(): - playersDf = pd.read_csv(utils.relPathPlayers+teamNames[team_name]+".csv") - playersDf = playersDf.replace({np.nan:None}) # For Json Nan must be replaced + playersDf = pd.read_csv(relPathPlayers+teamNames[team_name]+".csv") + playersDf = playersDf.replace({np.nan:None}) # For Json Nan must be replaced players_dict = {} for el in range(len(playersDf["first_name"])): diff --git a/api/utils.py b/api/utils.py index c04bdf1..069f072 100644 --- a/api/utils.py +++ b/api/utils.py @@ -2,8 +2,8 @@ import pandas as pd # Making relative paths. On windows slashes would be backwards # It is probably not the best way. -relPathTeams = "../AllAboutData/Data/NBAteams.csv" -relPathPlayers = "../AllAboutData/Data/Players/" +relPathTeams = "../../AllAboutData/Data/NBAteams.csv" +relPathPlayers = "../../AllAboutData/Data/Players/" def getTeamNames(): ''' -- cgit v1.2.3