summaryrefslogtreecommitdiff
path: root/api
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
parent98e375682a50f3cdeddc8e6af022d1e8d12f608d (diff)
fix importing modules, add readme
Diffstat (limited to 'api')
-rw-r--r--api/main.py2
-rw-r--r--api/routers/get.py37
-rw-r--r--api/utils.py4
3 files changed, 31 insertions, 12 deletions
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():
'''