Skip to content

Commit

Permalink
Create endpoints with data tables for each profile
Browse files Browse the repository at this point in the history
  • Loading branch information
timskovjacobsen committed Jul 24, 2021
1 parent d1ceaa5 commit fbdbf0a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# Project imports
from views import home
from api import steel_api
from views import tables


api = fastapi.FastAPI()
Expand All @@ -27,6 +28,7 @@ def configure_routing():
api.mount("/static", StaticFiles(directory="static"), name="static")
api.include_router(home.router)
api.include_router(steel_api.router)
api.include_router(tables.router)


if __name__ == "__main__":
Expand Down
18 changes: 0 additions & 18 deletions views/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,3 @@ def api_index(request: Request):
@router.get("/favicon.ico", include_in_schema=False)
def favicon():
return fastapi.responses.RedirectResponse(url="/static/img/favicon.ico")


@router.get("/HEA", include_in_schema=False)
def HEA(request: Request):

df = pd.read_csv("./assets/HEA.csv")
df_html = df.to_html()

return templates.TemplateResponse(
"home/profiletable.html",
{"request": request, "df_html": df_html, "profile_type": "HEA"},
)


@router.get("/hea", include_in_schema=False)
def hea(request: Request):
"""Alias for '/HEA' endpoint to make enable lower case usage in URL."""
return HEA(request)
48 changes: 48 additions & 0 deletions views/tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Standard Library imports
from typing import List

# Third party imports
import fastapi
from starlette.requests import Request
from starlette.templating import Jinja2Templates
import pandas as pd

# Project imports
from api import steel_api


# Template object for managing all templates
templates = Jinja2Templates("templates")
router = fastapi.APIRouter()


def _create_profile_table_endpoint(profile_type: str):
"""Generate functions representing endpoints upper and lower case url strings."""

@router.get(f"/{profile_type.upper()}", include_in_schema=False)
def uppercase_profile_endpoint_template(request: Request):

df = pd.read_csv(f"./assets/{profile_type}.csv")
df_html = df.to_html()

return templates.TemplateResponse(
"home/profiletable.html",
{"request": request, "df_html": df_html, "profile_type": profile_type},
)

@router.get(f"/{profile_type.lower()}")
def lowercase_profile_endpoint_template(request: Request):
return uppercase_profile_endpoint_template(request)

return uppercase_profile_endpoint_template, lowercase_profile_endpoint_template


def _create_all_profile_table_endpoints(all_profile_types: List[str]):
"""Generate endpoints for all supported profiles."""

for profile in all_profile_types:
_create_profile_table_endpoint(profile)


# Driver code for endpoint generation
_create_all_profile_table_endpoints(steel_api.PROFILE_TYPES)

0 comments on commit fbdbf0a

Please sign in to comment.