From 25e67cce2816871ceec3c4b4346b9d80cf7f439e Mon Sep 17 00:00:00 2001 From: Yaro Kasear Date: Mon, 16 Jun 2025 15:45:54 -0500 Subject: [PATCH] Add pandas dependency, enhance inventory summary visualization, and update table rendering --- requirements.txt | 1 + routes.py | 46 ++++++++++++++++++++++-- templates/fragments/_table_fragment.html | 1 + templates/index.html | 16 ++++++++- templates/layout.html | 26 ++++++++++++-- templates/table.html | 2 +- 6 files changed, 86 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index b8d2de2..d12b286 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ flask flask_sqlalchemy pyodbc +pandas diff --git a/routes.py b/routes.py index 9119d32..f8794e8 100644 --- a/routes.py +++ b/routes.py @@ -6,6 +6,7 @@ from typing import Callable, Any, List from . import db from .utils import eager_load_user_relationships, eager_load_inventory_relationships, eager_load_room_relationships, eager_load_worklog_relationships, chunk_list from datetime import datetime, timedelta +import pandas as pd main = Blueprint('main', __name__) @@ -141,7 +142,42 @@ def index(): stale_pagination = make_paginated_data(worklog_query, stale_worklog_page, 3) stale_count = len(worklog_query.all()) - stale_worklog_headers = {k: v for k, v in worklog_headers.items() if k not in ['End Time', 'Quick Analysis?', 'Complete?', 'Follow Up?']} + stale_worklog_headers = { + k: v for k, v in worklog_headers.items() + if k not in ['End Time', 'Quick Analysis?', 'Complete?', 'Follow Up?'] + } + + inventory_query = eager_load_inventory_relationships( + db.session.query(Inventory) + ) + + results = inventory_query.all() + + data = [{ + 'id': item.id, + 'condition': item.condition + } for item in results] + + df = pd.DataFrame(data) + + # Count items per condition + expected_conditions = [ + 'Deployed', 'Disposed', 'Inoperable', 'Partially Inoperable', + 'Removed', 'Unverified', 'Working' + ] + pivot = df['condition'].value_counts().reindex(expected_conditions, fill_value=0) + + # Convert pandas/numpy int64s to plain old Python ints + pivot = pivot.astype(int) + labels = list(pivot.index) + data = [int(x) for x in pivot.values] # <- This is what fixes the JSON error + + datasets = [{ + 'type': 'bar', + 'x': labels, + 'y': data, + 'name': 'Inventory Conditions' + }] return render_template( "index.html", @@ -149,9 +185,15 @@ def index(): stale_pagination=stale_pagination, stale_count=stale_count, stale_worklog_headers=stale_worklog_headers, - stale_worklog_rows=[{"id": log.id, "cells": [fn(log) for fn in stale_worklog_headers.values()]} for log in stale_pagination['items']], + stale_worklog_rows=[{ + "id": log.id, + "cells": [fn(log) for fn in stale_worklog_headers.values()] + } for log in stale_pagination['items']], + labels=labels, + datasets=datasets ) + def link(text, endpoint, **values): return {"text": text, "url": url_for(endpoint, **values)} diff --git a/templates/fragments/_table_fragment.html b/templates/fragments/_table_fragment.html index 1d63d6f..f6e3dd7 100644 --- a/templates/fragments/_table_fragment.html +++ b/templates/fragments/_table_fragment.html @@ -2,6 +2,7 @@ {% if rows %}
{% if title %} diff --git a/templates/index.html b/templates/index.html index 08eded5..0f08b08 100644 --- a/templates/index.html +++ b/templates/index.html @@ -32,6 +32,20 @@ {% endif %} +
+
+
+
Summary
+
+
+
+
-{% endblock %} \ No newline at end of file +{% endblock %} + +{% block script %} + const data = {{ datasets|tojson }}; + const layout = { title: 'Summary' }; + Plotly.newPlot('summary', data, layout) +{% endblock %} diff --git a/templates/layout.html b/templates/layout.html index 94fb02d..ded6e3e 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -13,8 +13,14 @@ {% block title %}Inventory{% endblock %} + @@ -75,6 +83,11 @@ + + diff --git a/templates/table.html b/templates/table.html index f545ea0..8000d66 100644 --- a/templates/table.html +++ b/templates/table.html @@ -11,5 +11,5 @@ ) }} {{ tables.render_table(header, rows, entry_route) }} -{{ tables.render_pagination(endpoint, page, has_prev, has_next, total_pages, extra_args=extra_args) }} +{# { tables.render_pagination(endpoint, page, has_prev, has_next, total_pages, extra_args=extra_args) } #} {% endblock %} \ No newline at end of file
{{ title }}