Files
quality_app/view_dashboard.py
2025-11-25 00:12:15 +02:00

186 lines
7.1 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Excel Dashboard Viewer
Reads and displays the Dashboard sheet from Open .Orders WIZ New.xlsb
"""
import pandas as pd
import sys
import os
def view_dashboard(file_path):
"""View the Dashboard sheet from the Excel file"""
if not os.path.exists(file_path):
print(f"❌ File not found: {file_path}")
return
try:
print("=" * 80)
print(f"📊 Loading Excel file: {os.path.basename(file_path)}")
print("=" * 80)
# First, list all sheets in the file
print("\n📋 Available sheets:")
xls = pd.ExcelFile(file_path, engine='pyxlsb')
for i, sheet in enumerate(xls.sheet_names, 1):
print(f" {i}. {sheet}")
# Try to find and read Daily Mirror sheet
dashboard_sheet = None
for sheet in xls.sheet_names:
if 'daily mirror' in sheet.lower() or 'dashboard' in sheet.lower() or 'dash' in sheet.lower():
dashboard_sheet = sheet
break
if not dashboard_sheet:
print("\n⚠️ No 'Dashboard' sheet found. Available sheets listed above.")
print("\nPlease select a sheet to view (enter number or name):")
choice = input("> ").strip()
if choice.isdigit():
idx = int(choice) - 1
if 0 <= idx < len(xls.sheet_names):
dashboard_sheet = xls.sheet_names[idx]
else:
if choice in xls.sheet_names:
dashboard_sheet = choice
if not dashboard_sheet:
print("❌ Invalid selection")
return
print(f"\n📊 Reading sheet: {dashboard_sheet}")
print("=" * 80)
# Read the sheet
df = pd.read_excel(file_path, sheet_name=dashboard_sheet, engine='pyxlsb')
# Display basic info
print(f"\n📏 Sheet dimensions: {df.shape[0]} rows × {df.shape[1]} columns")
print(f"\n📋 Column names:")
for i, col in enumerate(df.columns, 1):
print(f" {i}. {col}")
# Display first few rows
print(f"\n📊 First 10 rows:")
print("-" * 80)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', 50)
print(df.head(10).to_string())
# Display data types
print(f"\n📝 Data types:")
print(df.dtypes)
# Display summary statistics for numeric columns
numeric_cols = df.select_dtypes(include=['number']).columns
if len(numeric_cols) > 0:
print(f"\n📈 Summary statistics (numeric columns):")
print(df[numeric_cols].describe())
# Check for date columns
date_cols = []
for col in df.columns:
if 'date' in col.lower():
date_cols.append(col)
if date_cols:
print(f"\n📅 Date columns found: {', '.join(date_cols)}")
for col in date_cols:
try:
df[col] = pd.to_datetime(df[col], errors='coerce')
print(f" {col}: {df[col].min()} to {df[col].max()}")
except:
pass
# Interactive menu
while True:
print("\n" + "=" * 80)
print("📋 Options:")
print(" 1. View more rows")
print(" 2. Filter data")
print(" 3. View specific columns")
print(" 4. Export to CSV")
print(" 5. View another sheet")
print(" 6. Exit")
print("=" * 80)
choice = input("Select option (1-6): ").strip()
if choice == '1':
n = input("How many rows to display? (default 20): ").strip()
n = int(n) if n.isdigit() else 20
print(df.head(n).to_string())
elif choice == '2':
print(f"Available columns: {', '.join(df.columns)}")
col = input("Enter column name to filter: ").strip()
if col in df.columns:
print(f"Unique values in {col}:")
print(df[col].value_counts().head(20))
val = input(f"Enter value to filter {col}: ").strip()
filtered = df[df[col].astype(str).str.contains(val, case=False, na=False)]
print(f"\nFiltered results ({len(filtered)} rows):")
print(filtered.to_string())
else:
print("❌ Column not found")
elif choice == '3':
print(f"Available columns: {', '.join(df.columns)}")
cols = input("Enter column names (comma-separated): ").strip()
col_list = [c.strip() for c in cols.split(',')]
valid_cols = [c for c in col_list if c in df.columns]
if valid_cols:
print(df[valid_cols].head(20).to_string())
else:
print("❌ No valid columns found")
elif choice == '4':
output_file = input("Enter output filename (default: dashboard_export.csv): ").strip()
output_file = output_file if output_file else "dashboard_export.csv"
df.to_csv(output_file, index=False)
print(f"✅ Exported to {output_file}")
elif choice == '5':
print("\n📋 Available sheets:")
for i, sheet in enumerate(xls.sheet_names, 1):
print(f" {i}. {sheet}")
sheet_choice = input("Select sheet (number or name): ").strip()
if sheet_choice.isdigit():
idx = int(sheet_choice) - 1
if 0 <= idx < len(xls.sheet_names):
dashboard_sheet = xls.sheet_names[idx]
df = pd.read_excel(file_path, sheet_name=dashboard_sheet, engine='pyxlsb')
print(f"\n📊 Loaded sheet: {dashboard_sheet}")
print(f"Dimensions: {df.shape[0]} rows × {df.shape[1]} columns")
print(df.head(10).to_string())
else:
if sheet_choice in xls.sheet_names:
df = pd.read_excel(file_path, sheet_name=sheet_choice, engine='pyxlsb')
print(f"\n📊 Loaded sheet: {sheet_choice}")
print(f"Dimensions: {df.shape[0]} rows × {df.shape[1]} columns")
print(df.head(10).to_string())
elif choice == '6':
print("👋 Goodbye!")
break
else:
print("❌ Invalid option")
except Exception as e:
print(f"❌ Error reading file: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
file_path = "/srv/quality_app/Open .Orders WIZ New.xlsb"
if len(sys.argv) > 1:
file_path = sys.argv[1]
view_dashboard(file_path)