summaryrefslogtreecommitdiff
path: root/api/routers
diff options
context:
space:
mode:
authorRasmus Luha <rasmus.luha@gmail.com>2022-03-17 18:04:24 +0200
committerRasmus Luha <rasmus.luha@gmail.com>2022-03-17 18:04:24 +0200
commit6d6a6a8ac331135aa26320c532f47f25872d4243 (patch)
treee83b099a025899bcfa41559a1a4c6588e00f8724 /api/routers
parent98e375682a50f3cdeddc8e6af022d1e8d12f608d (diff)
fix importing modules, add readme
Diffstat (limited to 'api/routers')
-rw-r--r--api/routers/get.py37
1 files changed, 28 insertions, 9 deletions
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"])):