adds data analysis

This commit is contained in:
Michael Beck
2025-02-08 19:12:38 +01:00
parent afd9ec1c5c
commit 2615972566
5 changed files with 178 additions and 3 deletions

View File

@@ -1,12 +1,14 @@
import os
import glob
from flask import render_template
from flask import render_template, Blueprint, current_app, request
from app.forms import ScrapingForm
from app.util import get_size
from app.config import load_config
from app.api import scraper as scraper# Import the scraper instance
from app.logging_config import get_logger
from app.analysis import load_data, generate_statistics, plot_activity_distribution
from app.state import log_file_name
@@ -15,6 +17,8 @@ print(f"A imported log_file_name: {log_file_name}")
config = load_config()
logger = get_logger()
views_bp = Blueprint("views", __name__)
def register_views(app):
@app.route('/')
def index():
@@ -79,4 +83,46 @@ def register_views(app):
files = {"data": data_files_info, "log": log_files_info}
return render_template('download_results.html', files=files)
return render_template('download_results.html', files=files)
views_bp = Blueprint("views", __name__)
@views_bp.route("/data-visualization", methods=["GET", "POST"])
def data_visualization():
"""Route to display activity statistics with a visualization."""
data_dir = current_app.config["DATA"]["DATA_DIR"]
# Find all available CSV files
data_files = sorted(
glob.glob(os.path.join(data_dir, "*.csv")),
key=os.path.getmtime,
reverse=True
)
if not data_files:
return render_template("data_visualization.html", error="No data files found.", data_files=[])
# Get the selected file from the dropdown (default to the latest file)
selected_file = request.form.get("data_file", data_files[0] if data_files else None)
if selected_file and os.path.exists(selected_file):
df = load_data(selected_file)
statistics = generate_statistics(df)
# ✅ Generate the plot and get the correct URL path
# remove app/ from the base URL
plot_url = plot_activity_distribution(df).replace("app/", "")
else:
return render_template("data_visualization.html", error="Invalid file selection.", data_files=data_files)
return render_template(
"data_visualization.html",
plot_url=plot_url,
statistics=statistics.to_dict(),
data_files=data_files,
selected_file=selected_file
)
app.register_blueprint(views_bp)