Add template for rendering trainings
This commit is contained in:
parent
3cadfb1d09
commit
d2cfa5ae17
5 changed files with 90 additions and 4 deletions
|
|
@ -2,14 +2,27 @@ from fastapi import APIRouter, Request
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
|
|
||||||
|
from app.core.parsers.obsidian import parse_training_data
|
||||||
|
from app.core.templates.templates import BaseTemplateResponse
|
||||||
|
|
||||||
web_router = APIRouter()
|
web_router = APIRouter()
|
||||||
templates = Jinja2Templates(directory="app/templates")
|
templates = Jinja2Templates(directory="app/templates")
|
||||||
|
template_renderer = BaseTemplateResponse(templates)
|
||||||
|
|
||||||
|
|
||||||
@web_router.get("/", response_class=HTMLResponse)
|
@web_router.get("/", response_class=HTMLResponse)
|
||||||
async def home(request: Request):
|
async def home(request: Request):
|
||||||
"""Home page"""
|
"""Home page"""
|
||||||
return templates.TemplateResponse(
|
|
||||||
|
return template_renderer.render(
|
||||||
|
request,
|
||||||
"index.html",
|
"index.html",
|
||||||
{"request": request, "title": "Fitness Parser"}
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@web_router.get("/obsidian/list/", response_class=HTMLResponse)
|
||||||
|
async def obsidian_list(request: Request):
|
||||||
|
data = parse_training_data()
|
||||||
|
return template_renderer.render(
|
||||||
|
request=request, template_name="trainings.html", context={"trainings": data}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
0
app/core/templates/__init__.py
Normal file
0
app/core/templates/__init__.py
Normal file
30
app/core/templates/templates.py
Normal file
30
app/core/templates/templates.py
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
from typing import Optional
|
||||||
|
from fastapi import Request
|
||||||
|
from fastapi.templating import Jinja2Templates
|
||||||
|
|
||||||
|
|
||||||
|
class BaseTemplateResponse:
|
||||||
|
"""Базовый метод для рендеринга Jinja2 темплейтов.
|
||||||
|
|
||||||
|
Нужен в первую очередь для того, чтобы подставлять какие-то дефолтные значения (которые есть
|
||||||
|
на всех страницах) в переменные темплейтов
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
templates: Формат темплейтов, используемых в проекте
|
||||||
|
"""
|
||||||
|
def __init__(self, templates: Jinja2Templates):
|
||||||
|
self.templates = templates
|
||||||
|
|
||||||
|
def render(
|
||||||
|
self, request: Request, template_name: str, context: Optional[dict] = None
|
||||||
|
):
|
||||||
|
base_context = {
|
||||||
|
"request": request,
|
||||||
|
"title": "Fitness stats",
|
||||||
|
"app_name": "Fitness parser"
|
||||||
|
}
|
||||||
|
|
||||||
|
if context:
|
||||||
|
base_context.update(context)
|
||||||
|
|
||||||
|
return self.templates.TemplateResponse(template_name, base_context)
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
<nav class="navbar navbar-dark bg-primary">
|
<nav class="navbar navbar-dark bg-primary">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<a class="navbar-brand" href="/">
|
<a class="navbar-brand" href="/">
|
||||||
<strong>Fitness Parser</strong>
|
<strong>{{ app_name }}</strong>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
|
||||||
43
app/templates/trainings.html
Normal file
43
app/templates/trainings.html
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<h1>Training Sessions</h1>
|
||||||
|
|
||||||
|
{% if trainings %}
|
||||||
|
<p>Total trainings: {{ trainings|length }}</p>
|
||||||
|
|
||||||
|
{% for training in trainings %}
|
||||||
|
<div class="training">
|
||||||
|
<h2>Date: {{ training.date }}</h2>
|
||||||
|
|
||||||
|
{% if training.exercises %}
|
||||||
|
<p>Exercises: {{ training.exercises|length }}</p>
|
||||||
|
|
||||||
|
{% for exercise in training.exercises %}
|
||||||
|
<div class="exercise">
|
||||||
|
<h3>{{ exercise.name }}</h3>
|
||||||
|
{% if exercise.splitted_weight %}
|
||||||
|
<span>(Split Weight)</span>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<ul class="approaches">
|
||||||
|
{% for approach in exercise.approaches %}
|
||||||
|
<li>{{ approach.weight }}kg x {{ approach.reps }} reps</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
{% else %}
|
||||||
|
<p>No exercises recorded</p>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
{% else %}
|
||||||
|
<p>No training data available</p>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
Loading…
Reference in a new issue