Add week and month view

This commit is contained in:
Peter Stockings
2024-12-26 17:09:52 +11:00
parent 9614567e22
commit 0fa3535c96
3 changed files with 237 additions and 52 deletions

View File

@@ -11,7 +11,7 @@ from app.models import Profile, Reading, db, User
from app.forms import DeleteForm, LoginForm, ProfileForm, ReadingForm, SignupForm
from flask_login import login_user, login_required, current_user, logout_user
import base64
from datetime import datetime, timedelta
from datetime import date, datetime, timedelta
from PIL import Image
main = Blueprint('main', __name__)
@@ -193,6 +193,14 @@ def dashboard():
diastolic = [reading.diastolic for reading in readings]
heart_rate = [reading.heart_rate for reading in readings]
# Group readings by date
readings_by_date = {}
for reading in readings:
date_key = reading.timestamp.date()
if date_key not in readings_by_date:
readings_by_date[date_key] = []
readings_by_date[date_key].append(reading)
# Render template
return render_template(
'dashboard.html',
@@ -208,7 +216,10 @@ def dashboard():
diastolic=diastolic,
heart_rate=heart_rate,
start_date=start_date,
end_date=end_date
end_date=end_date,
readings_by_date=readings_by_date,
date=date,
timedelta=timedelta
)
@main.route('/add-reading', methods=['GET', 'POST'])

View File

@@ -850,6 +850,10 @@ video {
grid-template-columns: repeat(1, minmax(0, 1fr));
}
.grid-cols-7 {
grid-template-columns: repeat(7, minmax(0, 1fr));
}
.flex-col {
flex-direction: column;
}
@@ -890,6 +894,11 @@ video {
gap: 1.5rem;
}
.gap-x-4 {
-moz-column-gap: 1rem;
column-gap: 1rem;
}
.space-x-2 > :not([hidden]) ~ :not([hidden]) {
--tw-space-x-reverse: 0;
margin-right: calc(0.5rem * var(--tw-space-x-reverse));
@@ -928,6 +937,12 @@ video {
overflow-x: auto;
}
.truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.whitespace-nowrap {
white-space: nowrap;
}
@@ -960,6 +975,10 @@ video {
border-bottom-width: 1px;
}
.border-b-2 {
border-bottom-width: 2px;
}
.border-gray-300 {
--tw-border-opacity: 1;
border-color: rgb(209 213 219 / var(--tw-border-opacity, 1));
@@ -970,6 +989,16 @@ video {
border-color: rgb(255 255 255 / var(--tw-border-opacity, 1));
}
.border-blue-600 {
--tw-border-opacity: 1;
border-color: rgb(37 99 235 / var(--tw-border-opacity, 1));
}
.border-green-200 {
--tw-border-opacity: 1;
border-color: rgb(187 247 208 / var(--tw-border-opacity, 1));
}
.bg-blue-600 {
--tw-bg-opacity: 1;
background-color: rgb(37 99 235 / var(--tw-bg-opacity, 1));
@@ -1025,6 +1054,16 @@ video {
background-color: rgb(255 255 255 / var(--tw-bg-opacity, 1));
}
.bg-blue-100 {
--tw-bg-opacity: 1;
background-color: rgb(219 234 254 / var(--tw-bg-opacity, 1));
}
.bg-green-50 {
--tw-bg-opacity: 1;
background-color: rgb(240 253 244 / var(--tw-bg-opacity, 1));
}
.bg-gradient-to-r {
background-image: linear-gradient(to right, var(--tw-gradient-stops));
}
@@ -1068,6 +1107,10 @@ video {
padding: 2rem;
}
.p-1 {
padding: 0.25rem;
}
.px-2 {
padding-left: 0.5rem;
padding-right: 0.5rem;
@@ -1250,6 +1293,16 @@ video {
color: rgb(239 68 68 / var(--tw-text-opacity, 1));
}
.text-blue-800 {
--tw-text-opacity: 1;
color: rgb(30 64 175 / var(--tw-text-opacity, 1));
}
.text-green-700 {
--tw-text-opacity: 1;
color: rgb(21 128 61 / var(--tw-text-opacity, 1));
}
.no-underline {
text-decoration-line: none;
}
@@ -1333,6 +1386,16 @@ video {
background-color: rgb(185 28 28 / var(--tw-bg-opacity, 1));
}
.hover\:bg-green-200:hover {
--tw-bg-opacity: 1;
background-color: rgb(187 247 208 / var(--tw-bg-opacity, 1));
}
.hover\:bg-green-100:hover {
--tw-bg-opacity: 1;
background-color: rgb(220 252 231 / var(--tw-bg-opacity, 1));
}
.hover\:text-blue-800:hover {
--tw-text-opacity: 1;
color: rgb(30 64 175 / var(--tw-text-opacity, 1));

View File

@@ -59,7 +59,7 @@
</div>
<!-- Collapsible Graph Section -->
<div x-show="open" x-transition.duration.300ms class="mt-4 space-y-6">
<div x-show="open" x-transition.duration.50ms class="mt-4 space-y-6">
<!-- Blood Pressure Graph -->
<div>
<h3 class="text-sm font-semibold text-gray-600 mb-2">Blood Pressure (mmHg)</h3>
@@ -142,7 +142,7 @@
</div>
<!-- Collapsible Content -->
<form method="POST" action="{{ url_for('main.dashboard') }}" x-show="open" x-transition.duration.300ms
<form method="POST" action="{{ url_for('main.dashboard') }}" x-show="open" x-transition.duration.50ms
class="mt-4">
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
@@ -165,55 +165,166 @@
</form>
</div>
<!-- Readings Table -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{% for reading in readings %}
<a href="{{ url_for('main.edit_reading', reading_id=reading.id) }}"
class="bg-white shadow-md rounded-lg p-4 flex flex-col justify-between relative hover:shadow-lg transition-shadow">
<!-- Timestamp -->
<div class="absolute top-2 right-2 flex items-center text-gray-400 text-xs">
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4 mr-1" fill="none" viewBox="0 0 24 24"
stroke="currentColor" stroke-width="1.5">
<path stroke-linecap="round" stroke-linejoin="round"
d="M12 8v4l3 3m9-3a9 9 0 1 1-18 0 9 9 0 0 1 18 0z" />
</svg>
<span title="{{ reading.timestamp.strftime('%d %b %Y, %I:%M %p') }}">
{{ reading.relative_timestamp }}
</span>
</div>
<!-- Blood Pressure -->
<div class="text-sm text-gray-600 mb-2">
<span class="block text-lg font-semibold text-gray-800">Blood Pressure</span>
<span class="text-2xl font-bold text-blue-600">{{ reading.systolic }}</span>
<span class="text-lg text-gray-500">/</span>
<span class="text-xl font-bold text-red-600">{{ reading.diastolic }}</span>
<span class="text-sm text-gray-500">mmHg</span>
</div>
<!-- Heart Rate and Arrow -->
<div class="flex justify-between items-center mt-4">
<div class="text-sm text-gray-600">
<span class="block text-lg font-semibold text-gray-800">Heart Rate</span>
<span class="text-2xl font-bold text-green-600">{{ reading.heart_rate }}</span>
<span class="text-sm text-gray-500">bpm</span>
</div>
<!-- Arrow Icon -->
<div class="text-gray-400 hover:text-gray-600">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="h-5 w-5">
<path stroke-linecap="round" stroke-linejoin="round"
d="M6.75 12a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0ZM12.75 12a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0ZM18.75 12a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0Z" />
</svg>
</div>
</div>
</a>
{% else %}
<div class="col-span-full text-center text-sm text-gray-500">
No readings found.
<div class="max-w-5xl mx-auto" x-data="{ activeView: 'weekly' }">
<!-- Tabs -->
<div class="flex border-b mb-4">
<button @click="activeView = 'list'" :class="{'border-blue-600 text-blue-600': activeView === 'list'}"
class="px-4 py-2 text-sm font-medium border-b-2">List View</button>
<button @click="activeView = 'weekly'" :class="{'border-blue-600 text-blue-600': activeView === 'weekly'}"
class="px-4 py-2 text-sm font-medium border-b-2">Weekly View</button>
<button @click="activeView = 'monthly'" :class="{'border-blue-600 text-blue-600': activeView === 'monthly'}"
class="px-4 py-2 text-sm font-medium border-b-2">Monthly View</button>
</div>
{% endfor %}
<!-- List -->
<div x-show="activeView === 'list'" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{% for reading in readings %}
<a href="{{ url_for('main.edit_reading', reading_id=reading.id) }}"
class="bg-white shadow-md rounded-lg p-4 flex flex-col justify-between relative hover:shadow-lg transition-shadow">
<!-- Timestamp -->
<div class="absolute top-2 right-2 flex items-center text-gray-400 text-xs">
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4 mr-1" fill="none" viewBox="0 0 24 24"
stroke="currentColor" stroke-width="1.5">
<path stroke-linecap="round" stroke-linejoin="round"
d="M12 8v4l3 3m9-3a9 9 0 1 1-18 0 9 9 0 0 1 18 0z" />
</svg>
<span title="{{ reading.timestamp.strftime('%d %b %Y, %I:%M %p') }}">
{{ reading.relative_timestamp }}
</span>
</div>
<!-- Blood Pressure -->
<div class="text-sm text-gray-600 mb-2">
<span class="block text-lg font-semibold text-gray-800">Blood Pressure</span>
<span class="text-2xl font-bold text-blue-600">{{ reading.systolic }}</span>
<span class="text-lg text-gray-500">/</span>
<span class="text-xl font-bold text-red-600">{{ reading.diastolic }}</span>
<span class="text-sm text-gray-500">mmHg</span>
</div>
<!-- Heart Rate and Arrow -->
<div class="flex justify-between items-center mt-4">
<div class="text-sm text-gray-600">
<span class="block text-lg font-semibold text-gray-800">Heart Rate</span>
<span class="text-2xl font-bold text-green-600">{{ reading.heart_rate }}</span>
<span class="text-sm text-gray-500">bpm</span>
</div>
<!-- Arrow Icon -->
<div class="text-gray-400 hover:text-gray-600">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="h-5 w-5">
<path stroke-linecap="round" stroke-linejoin="round"
d="M6.75 12a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0ZM12.75 12a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0ZM18.75 12a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0Z" />
</svg>
</div>
</div>
</a>
{% else %}
<div class="col-span-full text-center text-sm text-gray-500">
No readings found.
</div>
{% endfor %}
</div>
<!-- Weekly View -->
<div x-show="activeView === 'weekly'" class="grid grid-cols-7 gap-4 text-center">
{% set today = date.today() %}
{% for i in range(7) %}
{% set day = today - timedelta(days=today.weekday() - i) %}
<div class="border p-4 rounded-lg bg-gray-50 gap-x-4">
<div class="text-sm font-bold text-gray-800">{{ day.strftime('%A, %b %d') }}</div>
{% if day in readings_by_date %}
{% for reading in readings_by_date[day]|sort(attribute="timestamp", reverse = True) %}
<a href="{{ url_for('main.edit_reading', reading_id=reading.id) }}"
class="block mt-2 p-2 bg-green-100 rounded-lg shadow hover:bg-green-200 transition">
<p class="text-xs font-medium text-green-800">
{{ reading.systolic }}/{{ reading.diastolic }} mmHg
</p>
<p class="text-xs text-gray-600">{{ reading.heart_rate }} bpm</p>
<!-- Timestamp -->
<div class="text-xs text-gray-500 mt-2">
{{ reading.timestamp.strftime('%I:%M %p') }}
</div>
</a>
{% endfor %}
{% else %}
<div class="h-8"></div> <!-- Placeholder for spacing -->
{% endif %}
</div>
{% endfor %}
</div>
<!-- Monthly View -->
<div x-show="activeView === 'monthly'" class="space-y-4">
{% set current_date = date.today().replace(day=1) %}
<!-- Month Name -->
<div class="text-center">
<h2 class="text-xl font-bold text-gray-800">{{ current_date.strftime('%B %Y') }}</h2>
</div>
<!-- Day Headers -->
<div class="grid grid-cols-7 gap-4 text-center">
<div class="font-semibold text-gray-700">Sun</div>
<div class="font-semibold text-gray-700">Mon</div>
<div class="font-semibold text-gray-700">Tue</div>
<div class="font-semibold text-gray-700">Wed</div>
<div class="font-semibold text-gray-700">Thu</div>
<div class="font-semibold text-gray-700">Fri</div>
<div class="font-semibold text-gray-700">Sat</div>
</div>
<!-- Calendar Grid -->
<div class="grid grid-cols-7 gap-4 text-center">
{% set days_in_month = (current_date.replace(month=current_date.month % 12 + 1, day=1) -
timedelta(days=1)).day %}
{% set first_weekday = current_date.weekday() %}
<!-- Empty slots for days before the 1st -->
{% for _ in range((first_weekday + 1) % 7) %}
<div></div>
{% endfor %}
<!-- Days of the Month -->
{% for day in range(1, days_in_month + 1) %}
{% set current_day = current_date.replace(day=day) %}
<div class="border p-4 rounded-lg bg-gray-50 relative">
<!-- Day Label -->
<div class="text-sm font-bold text-gray-800 text-left">{{ current_day.day }}</div>
<!-- Red Heart for Days with Readings -->
{% if current_day in readings_by_date %}
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24"
class="w-5 h-5 text-red-500 absolute top-2 right-2">
<path
d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z" />
</svg>
{% endif %}
<!-- Readings -->
{% if current_day in readings_by_date %}
{% for reading in readings_by_date[current_day]|sort(attribute="timestamp", reverse = True) %}
<a href="{{ url_for('main.edit_reading', reading_id=reading.id) }}"
class="block mt-2 p-2 bg-green-100 rounded-lg shadow hover:bg-green-200 transition">
<p class="text-xs font-medium text-green-800">
{{ reading.systolic }}/{{ reading.diastolic }} mmHg
</p>
<p class="text-xs text-gray-600">{{ reading.heart_rate }} bpm</p>
<!-- Timestamp -->
<div class="text-xs text-gray-500 mt-2">
{{ reading.timestamp.strftime('%I:%M %p') }}
</div>
</a>
{% endfor %}
{% else %}
<div class="h-8"></div> <!-- Placeholder for spacing -->
{% endif %}
</div>
{% endfor %}
</div>
</div>
</div>
</div>