github-actions[bot] commited on
Commit ·
4c46ca1
1
Parent(s): 3a1dc7d
Add all files with LFS support
Browse files- admin.py +49 -0
- analytics.py +98 -0
- app.py +90 -248
- components.py +119 -0
admin.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def init_admin_state():
|
| 9 |
+
"""Initialize admin-related session state variables"""
|
| 10 |
+
if "admin_authenticated" not in st.session_state:
|
| 11 |
+
st.session_state.admin_authenticated = False
|
| 12 |
+
|
| 13 |
+
if "ENABLE_TIMING" not in st.session_state:
|
| 14 |
+
st.session_state.ENABLE_TIMING = False
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def check_admin_access() -> bool:
|
| 18 |
+
"""Check if admin access is granted"""
|
| 19 |
+
if not ADMIN_PASSWORD:
|
| 20 |
+
return False
|
| 21 |
+
return st.session_state.get("admin_authenticated", False)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def render_admin_panel():
|
| 25 |
+
"""Render admin UI elements in the sidebar"""
|
| 26 |
+
with st.sidebar:
|
| 27 |
+
st.markdown("---")
|
| 28 |
+
with st.expander("🔒 Admin", expanded=False):
|
| 29 |
+
if st.session_state.admin_authenticated:
|
| 30 |
+
if st.button("Logout"):
|
| 31 |
+
st.session_state.admin_authenticated = False
|
| 32 |
+
st.rerun()
|
| 33 |
+
else:
|
| 34 |
+
password_input = st.text_input("Password", type="password")
|
| 35 |
+
if st.button("Login"):
|
| 36 |
+
if password_input == ADMIN_PASSWORD:
|
| 37 |
+
st.session_state.admin_authenticated = True
|
| 38 |
+
st.rerun()
|
| 39 |
+
else:
|
| 40 |
+
st.error("Incorrect password")
|
| 41 |
+
|
| 42 |
+
if st.session_state.admin_authenticated:
|
| 43 |
+
enable_timing = st.toggle(
|
| 44 |
+
"Enable Timing",
|
| 45 |
+
value=st.session_state.ENABLE_TIMING,
|
| 46 |
+
key="timing_toggle",
|
| 47 |
+
)
|
| 48 |
+
if enable_timing != st.session_state.ENABLE_TIMING:
|
| 49 |
+
st.session_state.ENABLE_TIMING = enable_timing
|
analytics.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import uuid
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from typing import Any, Dict, Optional
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def init_analytics_state() -> None:
|
| 11 |
+
"""Initialize analytics-related session state variables"""
|
| 12 |
+
if "logged_visit" not in st.session_state:
|
| 13 |
+
st.session_state.logged_visit = False
|
| 14 |
+
|
| 15 |
+
if "visitor_id" not in st.session_state:
|
| 16 |
+
st.session_state.visitor_id = str(uuid.uuid4())
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def log_visit(current_section: Optional[str] = None) -> None:
|
| 20 |
+
"""Log visitor analytics including timestamp, user agent, and page info"""
|
| 21 |
+
if st.session_state.get("admin_authenticated", False):
|
| 22 |
+
return
|
| 23 |
+
|
| 24 |
+
log_file = Path("analytics.json")
|
| 25 |
+
now = datetime.now()
|
| 26 |
+
today = now.strftime("%Y-%m-%d")
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
user_agent = st.context.headers.get("User-Agent", "Unknown")
|
| 30 |
+
except Exception:
|
| 31 |
+
user_agent = "Unknown"
|
| 32 |
+
|
| 33 |
+
visit_type = (
|
| 34 |
+
"initial" if not st.session_state.get("logged_visit") else "section_change"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
visit_data = {
|
| 38 |
+
"timestamp": now.isoformat(),
|
| 39 |
+
"date": today,
|
| 40 |
+
"user_agent": user_agent,
|
| 41 |
+
"visitor_id": st.session_state.visitor_id,
|
| 42 |
+
"page_section": current_section
|
| 43 |
+
or st.session_state.get("current_section", "Overall Summary"),
|
| 44 |
+
"visit_type": visit_type,
|
| 45 |
+
"query_params": dict(st.query_params),
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
# Initialize default data structure
|
| 49 |
+
data = {
|
| 50 |
+
"visits": [],
|
| 51 |
+
"daily_counts": {},
|
| 52 |
+
"section_counts": {},
|
| 53 |
+
"daily_visitors": {},
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# Try to load existing data, fallback to default if corrupted
|
| 57 |
+
if log_file.exists():
|
| 58 |
+
try:
|
| 59 |
+
with open(log_file, "r") as f:
|
| 60 |
+
data = json.load(f)
|
| 61 |
+
if "visits" not in data:
|
| 62 |
+
data["visits"] = []
|
| 63 |
+
if "daily_counts" not in data:
|
| 64 |
+
data["daily_counts"] = {}
|
| 65 |
+
if "section_counts" not in data:
|
| 66 |
+
data["section_counts"] = {}
|
| 67 |
+
if "daily_visitors" not in data:
|
| 68 |
+
data["daily_visitors"] = {}
|
| 69 |
+
except json.JSONDecodeError:
|
| 70 |
+
# If file is corrupted, backup the old file and start fresh
|
| 71 |
+
if log_file.exists():
|
| 72 |
+
backup_file = log_file.with_suffix(".json.bak")
|
| 73 |
+
log_file.rename(backup_file)
|
| 74 |
+
|
| 75 |
+
if today not in data["daily_visitors"]:
|
| 76 |
+
data["daily_visitors"][today] = []
|
| 77 |
+
if st.session_state.visitor_id not in data["daily_visitors"][today]:
|
| 78 |
+
data["daily_visitors"][today].append(st.session_state.visitor_id)
|
| 79 |
+
data["daily_counts"][today] = len(data["daily_visitors"][today])
|
| 80 |
+
|
| 81 |
+
data["visits"].append(visit_data)
|
| 82 |
+
current_section = visit_data["page_section"]
|
| 83 |
+
data["section_counts"][current_section] = (
|
| 84 |
+
data["section_counts"].get(current_section, 0) + 1
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
with open(log_file, "w") as f:
|
| 88 |
+
json.dump(data, f, indent=2)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def get_analytics_data() -> Dict[str, Any]:
|
| 92 |
+
"""Load and return analytics data from file"""
|
| 93 |
+
log_file = Path("analytics.json")
|
| 94 |
+
if not log_file.exists():
|
| 95 |
+
return {}
|
| 96 |
+
|
| 97 |
+
with open(log_file, "r") as f:
|
| 98 |
+
return json.load(f)
|
app.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
import calendar
|
| 2 |
import io
|
| 3 |
-
import json
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
import textwrap
|
| 7 |
import time
|
| 8 |
-
import uuid
|
| 9 |
from datetime import date, datetime
|
| 10 |
from functools import wraps
|
| 11 |
from pathlib import Path
|
|
@@ -19,6 +17,7 @@ from great_tables import GT, html
|
|
| 19 |
from matplotlib import pyplot as plt
|
| 20 |
from osgeo import gdal
|
| 21 |
|
|
|
|
| 22 |
from analysis import (
|
| 23 |
altair_plot_do_temp_relationship,
|
| 24 |
altair_plot_np_ratios,
|
|
@@ -31,6 +30,15 @@ from analysis import (
|
|
| 31 |
plot_seasonal_salinity_for_bays,
|
| 32 |
plot_sector_trends,
|
| 33 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
from main import (
|
| 35 |
create_multiindex_columns,
|
| 36 |
create_overall_summary,
|
|
@@ -47,6 +55,8 @@ if os.getenv("DEBUG", "false").lower() == "true":
|
|
| 47 |
else:
|
| 48 |
st.session_state.DEBUG = False
|
| 49 |
|
|
|
|
|
|
|
| 50 |
if "ENABLE_TIMING" not in st.session_state:
|
| 51 |
st.session_state.ENABLE_TIMING = False
|
| 52 |
|
|
@@ -71,139 +81,12 @@ if "logged_visit" not in st.session_state:
|
|
| 71 |
if "current_section" not in st.session_state:
|
| 72 |
st.session_state.current_section = "Overall Summary"
|
| 73 |
|
| 74 |
-
|
| 75 |
-
def log_visit():
|
| 76 |
-
"""Log visitor analytics including timestamp, user agent, and page info"""
|
| 77 |
-
if st.session_state.get("admin_authenticated", False):
|
| 78 |
-
return
|
| 79 |
-
log_file = Path("analytics.json")
|
| 80 |
-
now = datetime.now()
|
| 81 |
-
today = now.strftime("%Y-%m-%d")
|
| 82 |
-
|
| 83 |
-
if "visitor_id" not in st.session_state:
|
| 84 |
-
st.session_state.visitor_id = str(uuid.uuid4())
|
| 85 |
-
|
| 86 |
-
try:
|
| 87 |
-
user_agent = st.context.headers.get("User-Agent", "Unknown")
|
| 88 |
-
except Exception:
|
| 89 |
-
user_agent = "Unknown"
|
| 90 |
-
|
| 91 |
-
visit_type = (
|
| 92 |
-
"initial" if not st.session_state.get("logged_visit") else "section_change"
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
visit_data = {
|
| 96 |
-
"timestamp": now.isoformat(),
|
| 97 |
-
"date": today,
|
| 98 |
-
"user_agent": user_agent,
|
| 99 |
-
"visitor_id": st.session_state.visitor_id,
|
| 100 |
-
"page_section": st.session_state.get("current_section", "Overall Summary"),
|
| 101 |
-
"visit_type": visit_type,
|
| 102 |
-
"query_params": dict(st.query_params),
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
# Initialize default data structure
|
| 106 |
-
data = {
|
| 107 |
-
"visits": [],
|
| 108 |
-
"daily_counts": {},
|
| 109 |
-
"section_counts": {},
|
| 110 |
-
"daily_visitors": {},
|
| 111 |
-
}
|
| 112 |
-
|
| 113 |
-
# Try to load existing data, fallback to default if corrupted
|
| 114 |
-
if log_file.exists():
|
| 115 |
-
try:
|
| 116 |
-
with open(log_file, "r") as f:
|
| 117 |
-
data = json.load(f)
|
| 118 |
-
if "visits" not in data:
|
| 119 |
-
data["visits"] = []
|
| 120 |
-
if "daily_counts" not in data:
|
| 121 |
-
data["daily_counts"] = {}
|
| 122 |
-
if "section_counts" not in data:
|
| 123 |
-
data["section_counts"] = {}
|
| 124 |
-
if "daily_visitors" not in data:
|
| 125 |
-
data["daily_visitors"] = {}
|
| 126 |
-
except json.JSONDecodeError:
|
| 127 |
-
# If file is corrupted, backup the old file and start fresh
|
| 128 |
-
if log_file.exists():
|
| 129 |
-
backup_file = log_file.with_suffix(".json.bak")
|
| 130 |
-
log_file.rename(backup_file)
|
| 131 |
-
# Continue with default data structure
|
| 132 |
-
|
| 133 |
-
if today not in data["daily_visitors"]:
|
| 134 |
-
data["daily_visitors"][today] = []
|
| 135 |
-
if st.session_state.visitor_id not in data["daily_visitors"][today]:
|
| 136 |
-
data["daily_visitors"][today].append(st.session_state.visitor_id)
|
| 137 |
-
data["daily_counts"][today] = len(data["daily_visitors"][today])
|
| 138 |
-
|
| 139 |
-
data["visits"].append(visit_data)
|
| 140 |
-
current_section = visit_data["page_section"]
|
| 141 |
-
data["section_counts"][current_section] = (
|
| 142 |
-
data["section_counts"].get(current_section, 0) + 1
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
with open(log_file, "w") as f:
|
| 146 |
-
json.dump(data, f, indent=2)
|
| 147 |
-
|
| 148 |
|
| 149 |
if not st.session_state.get("logged_visit"):
|
| 150 |
log_visit()
|
| 151 |
st.session_state["logged_visit"] = True
|
| 152 |
|
| 153 |
-
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "")
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
def check_admin_access():
|
| 157 |
-
"""Handle admin authentication logic only"""
|
| 158 |
-
if not ADMIN_PASSWORD:
|
| 159 |
-
return False
|
| 160 |
-
|
| 161 |
-
if "admin_authenticated" not in st.session_state:
|
| 162 |
-
st.session_state.admin_authenticated = False
|
| 163 |
-
|
| 164 |
-
return st.session_state.admin_authenticated
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
def render_admin_panel():
|
| 168 |
-
"""Handle admin UI elements only"""
|
| 169 |
-
with st.sidebar:
|
| 170 |
-
st.markdown("---")
|
| 171 |
-
with st.expander("🔒 Admin", expanded=False):
|
| 172 |
-
if st.session_state.admin_authenticated:
|
| 173 |
-
if st.button("Logout"):
|
| 174 |
-
st.session_state.admin_authenticated = False
|
| 175 |
-
st.rerun()
|
| 176 |
-
else:
|
| 177 |
-
password_input = st.text_input("Password", type="password")
|
| 178 |
-
if st.button("Login"):
|
| 179 |
-
if password_input == ADMIN_PASSWORD:
|
| 180 |
-
st.session_state.admin_authenticated = True
|
| 181 |
-
st.rerun()
|
| 182 |
-
else:
|
| 183 |
-
st.error("Incorrect password")
|
| 184 |
-
|
| 185 |
-
if st.session_state.admin_authenticated:
|
| 186 |
-
enable_timing = st.toggle(
|
| 187 |
-
"Enable Timing",
|
| 188 |
-
value=st.session_state.ENABLE_TIMING,
|
| 189 |
-
key="timing_toggle",
|
| 190 |
-
)
|
| 191 |
-
if enable_timing != st.session_state.ENABLE_TIMING:
|
| 192 |
-
st.session_state.ENABLE_TIMING = enable_timing
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
def get_reporting_year_info_message() -> str:
|
| 196 |
-
"""Generate standardized info message about reporting year grouping."""
|
| 197 |
-
example_year = st.session_state.dataset_end_date.year
|
| 198 |
-
return f"""
|
| 199 |
-
📅 **Data is grouped by reporting years**:
|
| 200 |
-
- Each reporting year ends in **{calendar.month_name[st.session_state.reporting_month]}**.
|
| 201 |
-
- Example: Reporting year **{example_year}** covers
|
| 202 |
-
**{calendar.month_abbr[st.session_state.reporting_month + 1 if st.session_state.reporting_month < 12 else 1]}. {example_year - 1 if st.session_state.reporting_month < 12 else example_year}**
|
| 203 |
-
through **{calendar.month_abbr[st.session_state.reporting_month]}. {example_year}**.
|
| 204 |
-
- **Note**: You can change the reporting year end month in the **Dataset Settings** in the sidebar.
|
| 205 |
-
"""
|
| 206 |
-
|
| 207 |
|
| 208 |
st.set_page_config(
|
| 209 |
page_title="Water Quality Summary",
|
|
@@ -600,39 +483,9 @@ with st.sidebar:
|
|
| 600 |
day=calendar.monthrange(raw_end.year, raw_end.month)[1]
|
| 601 |
)
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
end = st.session_state.dataset_end_date
|
| 607 |
-
|
| 608 |
-
if start > end:
|
| 609 |
-
st.error("Start date must be before end date")
|
| 610 |
-
return
|
| 611 |
-
|
| 612 |
-
st.session_state.start_date = start
|
| 613 |
-
st.session_state.end_date = end
|
| 614 |
-
|
| 615 |
-
col1, col2 = st.columns(2)
|
| 616 |
-
with col1:
|
| 617 |
-
start_date = st.date_input(
|
| 618 |
-
"Start Date",
|
| 619 |
-
value=min_date,
|
| 620 |
-
min_value=min_date,
|
| 621 |
-
max_value=max_date,
|
| 622 |
-
format="MM/DD/YYYY",
|
| 623 |
-
key="dataset_start_date",
|
| 624 |
-
on_change=on_date_change,
|
| 625 |
-
)
|
| 626 |
-
with col2:
|
| 627 |
-
end_date = st.date_input(
|
| 628 |
-
"End Date",
|
| 629 |
-
value=max_date,
|
| 630 |
-
min_value=min_date,
|
| 631 |
-
max_value=max_date,
|
| 632 |
-
format="MM/DD/YYYY",
|
| 633 |
-
key="dataset_end_date",
|
| 634 |
-
on_change=on_date_change,
|
| 635 |
-
)
|
| 636 |
|
| 637 |
reporting_month = st.selectbox(
|
| 638 |
"Last Month of Reporting Year",
|
|
@@ -779,8 +632,12 @@ elif section == "Summary by Station":
|
|
| 779 |
|
| 780 |
elif section == "Trends by Station":
|
| 781 |
st.title("Trends by Station")
|
| 782 |
-
st.info(
|
| 783 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 784 |
"Dissolved Oxygen",
|
| 785 |
"Salinity",
|
| 786 |
"pH",
|
|
@@ -790,6 +647,11 @@ elif section == "Trends by Station":
|
|
| 790 |
"Total Nitrogen",
|
| 791 |
"Total Phosphorus",
|
| 792 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 793 |
st.sidebar.markdown("### Filter Options")
|
| 794 |
|
| 795 |
selected_station = st.sidebar.selectbox(
|
|
@@ -804,6 +666,9 @@ elif section == "Trends by Station":
|
|
| 804 |
selection_mode="single",
|
| 805 |
)
|
| 806 |
selected_position = selected_position or "All"
|
|
|
|
|
|
|
|
|
|
| 807 |
filtered_df = data["raw_df"].query("Station_Number == @selected_station")
|
| 808 |
if selected_position != "All":
|
| 809 |
filtered_df = filtered_df.query("Sample_Position == @selected_position")
|
|
@@ -818,7 +683,6 @@ elif section == "Trends by Station":
|
|
| 818 |
)
|
| 819 |
|
| 820 |
with st.sidebar.expander("Preview Filtered Data"):
|
| 821 |
-
st.markdown(f"**{len(filtered_df):,}** records")
|
| 822 |
display_columns = [
|
| 823 |
"Activity_Start_Date_Time",
|
| 824 |
"Sample_Position",
|
|
@@ -827,16 +691,10 @@ elif section == "Trends by Station":
|
|
| 827 |
"Org_Result_Unit",
|
| 828 |
"Reporting_Year",
|
| 829 |
]
|
| 830 |
-
|
| 831 |
-
preview_df.set_index("Station_Number", inplace=True)
|
| 832 |
-
st.dataframe(
|
| 833 |
-
preview_df.style.format(precision=2),
|
| 834 |
-
use_container_width=True,
|
| 835 |
-
height=300,
|
| 836 |
-
)
|
| 837 |
|
| 838 |
if not filtered_df.empty:
|
| 839 |
-
fig = plot_analyte_trends(filtered_df,
|
| 840 |
st.pyplot(fig)
|
| 841 |
else:
|
| 842 |
st.warning(
|
|
@@ -848,7 +706,11 @@ elif section == "Sector Trends":
|
|
| 848 |
st.session_state.ENABLE_ALTAIR = st.sidebar.toggle(
|
| 849 |
"Interactive Plots", value=st.session_state.ENABLE_ALTAIR
|
| 850 |
)
|
| 851 |
-
st.info(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 852 |
default_analytes = [
|
| 853 |
"Dissolved Oxygen",
|
| 854 |
"Salinity",
|
|
@@ -862,12 +724,8 @@ elif section == "Sector Trends":
|
|
| 862 |
if x not in default_analytes
|
| 863 |
]
|
| 864 |
|
| 865 |
-
selected_analytes =
|
| 866 |
-
|
| 867 |
-
options=all_analytes,
|
| 868 |
-
default=default_analytes,
|
| 869 |
-
key="sector_analyte_select",
|
| 870 |
-
help="Choose one or more analytes to plot.",
|
| 871 |
)
|
| 872 |
if selected_analytes and not data["raw_df"].empty:
|
| 873 |
if st.session_state.ENABLE_ALTAIR:
|
|
@@ -885,8 +743,8 @@ elif section == "Parameter Correlations":
|
|
| 885 |
st.title("Parameter Correlations")
|
| 886 |
subset_by = "Sector"
|
| 887 |
st.sidebar.markdown("### Filter Options")
|
| 888 |
-
position_filter =
|
| 889 |
-
"
|
| 890 |
)
|
| 891 |
with st.spinner("Loading data for correlation plots..."):
|
| 892 |
analyte_names = [
|
|
@@ -988,7 +846,6 @@ elif section == "Parameter Correlations":
|
|
| 988 |
cols[idx % 2].pyplot(fig)
|
| 989 |
plt.close()
|
| 990 |
with cols[idx % 2].expander(f"View {subset} Data"):
|
| 991 |
-
st.markdown(f"**{len(subset_df):,}** records")
|
| 992 |
display_columns = [
|
| 993 |
"Activity_Start_Date_Time",
|
| 994 |
"Station_Number",
|
|
@@ -997,11 +854,7 @@ elif section == "Parameter Correlations":
|
|
| 997 |
"Org_Result_Value",
|
| 998 |
"Org_Result_Unit",
|
| 999 |
]
|
| 1000 |
-
|
| 1001 |
-
subset_df[display_columns].style.format(precision=2),
|
| 1002 |
-
use_container_width=True,
|
| 1003 |
-
height=300,
|
| 1004 |
-
)
|
| 1005 |
csv_buffer = io.StringIO()
|
| 1006 |
subset_df.to_csv(csv_buffer, index=False)
|
| 1007 |
st.download_button(
|
|
@@ -1053,12 +906,11 @@ elif section == "Calendar Heatmaps":
|
|
| 1053 |
for x in sorted(raw_df["Org_Analyte_Name"].unique())
|
| 1054 |
if x not in default_analytes
|
| 1055 |
]
|
| 1056 |
-
selected_analytes =
|
| 1057 |
-
|
| 1058 |
-
|
| 1059 |
-
|
| 1060 |
-
|
| 1061 |
-
help="Choose one or more analytes to display in the heatmap.",
|
| 1062 |
)
|
| 1063 |
|
| 1064 |
# Filter Options
|
|
@@ -1069,12 +921,7 @@ elif section == "Calendar Heatmaps":
|
|
| 1069 |
index=0,
|
| 1070 |
key="calendar_sector_select",
|
| 1071 |
)
|
| 1072 |
-
position_filter =
|
| 1073 |
-
"Position:",
|
| 1074 |
-
["All", "Surface", "Bottom"],
|
| 1075 |
-
index=0,
|
| 1076 |
-
key="calendar_position_select",
|
| 1077 |
-
)
|
| 1078 |
|
| 1079 |
def format_colormap_option(option):
|
| 1080 |
append = ""
|
|
@@ -1237,63 +1084,58 @@ elif section == "Raw Data":
|
|
| 1237 |
|
| 1238 |
elif section == "Analytics":
|
| 1239 |
st.title("Analytics")
|
|
|
|
| 1240 |
|
| 1241 |
-
|
| 1242 |
-
if log_file.exists():
|
| 1243 |
-
with open(log_file, "r") as f:
|
| 1244 |
-
analytics_data = json.load(f)
|
| 1245 |
|
| 1246 |
-
|
|
|
|
|
|
|
| 1247 |
|
| 1248 |
-
|
| 1249 |
-
visits_df
|
| 1250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1251 |
|
| 1252 |
-
|
| 1253 |
-
|
| 1254 |
-
|
| 1255 |
-
.reset_index()
|
| 1256 |
-
.rename(columns={"nunique": "Unique Visitors", "count": "Total Views"})
|
| 1257 |
-
)
|
| 1258 |
-
daily_visits_df["date"] = pd.to_datetime(daily_visits_df["date"])
|
| 1259 |
-
daily_visits_df = daily_visits_df.sort_values("date")
|
| 1260 |
|
| 1261 |
-
|
| 1262 |
-
|
| 1263 |
-
|
|
|
|
|
|
|
| 1264 |
|
| 1265 |
-
|
| 1266 |
-
|
| 1267 |
-
|
| 1268 |
-
|
| 1269 |
-
|
|
|
|
|
|
|
| 1270 |
|
| 1271 |
-
|
| 1272 |
-
|
| 1273 |
-
|
| 1274 |
-
|
| 1275 |
-
),
|
| 1276 |
-
|
| 1277 |
-
|
|
|
|
| 1278 |
|
| 1279 |
-
|
| 1280 |
-
|
| 1281 |
-
{
|
| 1282 |
-
"Section": analytics_data["section_counts"].keys(),
|
| 1283 |
-
"Views": analytics_data["section_counts"].values(),
|
| 1284 |
-
}
|
| 1285 |
-
)
|
| 1286 |
-
section_visits_df = section_visits_df.sort_values("Views", ascending=True)
|
| 1287 |
|
| 1288 |
-
|
| 1289 |
-
|
|
|
|
|
|
|
| 1290 |
|
| 1291 |
-
with st.expander("Raw Visit Data"):
|
| 1292 |
-
visits_df = pd.DataFrame(analytics_data["visits"])
|
| 1293 |
-
visits_df["timestamp"] = pd.to_datetime(visits_df["timestamp"])
|
| 1294 |
-
st.dataframe(visits_df)
|
| 1295 |
-
else:
|
| 1296 |
-
st.warning("No analytics data available.")
|
| 1297 |
|
| 1298 |
if st.session_state.ENABLE_TIMING:
|
| 1299 |
st.markdown("---")
|
|
|
|
| 1 |
import calendar
|
| 2 |
import io
|
|
|
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import textwrap
|
| 6 |
import time
|
|
|
|
| 7 |
from datetime import date, datetime
|
| 8 |
from functools import wraps
|
| 9 |
from pathlib import Path
|
|
|
|
| 17 |
from matplotlib import pyplot as plt
|
| 18 |
from osgeo import gdal
|
| 19 |
|
| 20 |
+
from admin import check_admin_access, init_admin_state, render_admin_panel
|
| 21 |
from analysis import (
|
| 22 |
altair_plot_do_temp_relationship,
|
| 23 |
altair_plot_np_ratios,
|
|
|
|
| 30 |
plot_seasonal_salinity_for_bays,
|
| 31 |
plot_sector_trends,
|
| 32 |
)
|
| 33 |
+
from analytics import get_analytics_data, init_analytics_state, log_visit
|
| 34 |
+
from components import (
|
| 35 |
+
get_reporting_year_info_message,
|
| 36 |
+
on_date_change,
|
| 37 |
+
render_date_filters,
|
| 38 |
+
render_filtered_data_preview,
|
| 39 |
+
render_sidebar_analyte_multiselect,
|
| 40 |
+
render_sidebar_position_filter_selectbox,
|
| 41 |
+
)
|
| 42 |
from main import (
|
| 43 |
create_multiindex_columns,
|
| 44 |
create_overall_summary,
|
|
|
|
| 55 |
else:
|
| 56 |
st.session_state.DEBUG = False
|
| 57 |
|
| 58 |
+
init_admin_state()
|
| 59 |
+
|
| 60 |
if "ENABLE_TIMING" not in st.session_state:
|
| 61 |
st.session_state.ENABLE_TIMING = False
|
| 62 |
|
|
|
|
| 81 |
if "current_section" not in st.session_state:
|
| 82 |
st.session_state.current_section = "Overall Summary"
|
| 83 |
|
| 84 |
+
init_analytics_state()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
if not st.session_state.get("logged_visit"):
|
| 87 |
log_visit()
|
| 88 |
st.session_state["logged_visit"] = True
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
st.set_page_config(
|
| 92 |
page_title="Water Quality Summary",
|
|
|
|
| 483 |
day=calendar.monthrange(raw_end.year, raw_end.month)[1]
|
| 484 |
)
|
| 485 |
|
| 486 |
+
start_date, end_date = render_date_filters(
|
| 487 |
+
min_date, max_date, key_prefix="dataset", on_change=on_date_change
|
| 488 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
|
| 490 |
reporting_month = st.selectbox(
|
| 491 |
"Last Month of Reporting Year",
|
|
|
|
| 632 |
|
| 633 |
elif section == "Trends by Station":
|
| 634 |
st.title("Trends by Station")
|
| 635 |
+
st.info(
|
| 636 |
+
get_reporting_year_info_message(
|
| 637 |
+
st.session_state.reporting_month, st.session_state.dataset_end_date.year
|
| 638 |
+
)
|
| 639 |
+
)
|
| 640 |
+
default_analytes = [
|
| 641 |
"Dissolved Oxygen",
|
| 642 |
"Salinity",
|
| 643 |
"pH",
|
|
|
|
| 647 |
"Total Nitrogen",
|
| 648 |
"Total Phosphorus",
|
| 649 |
]
|
| 650 |
+
all_analytes = default_analytes + [
|
| 651 |
+
x
|
| 652 |
+
for x in sorted(data["raw_df"]["Org_Analyte_Name"].unique())
|
| 653 |
+
if x not in default_analytes
|
| 654 |
+
]
|
| 655 |
st.sidebar.markdown("### Filter Options")
|
| 656 |
|
| 657 |
selected_station = st.sidebar.selectbox(
|
|
|
|
| 666 |
selection_mode="single",
|
| 667 |
)
|
| 668 |
selected_position = selected_position or "All"
|
| 669 |
+
selected_analytes = render_sidebar_analyte_multiselect(
|
| 670 |
+
all_analytes, default_analytes, key_prefix="station_trends"
|
| 671 |
+
)
|
| 672 |
filtered_df = data["raw_df"].query("Station_Number == @selected_station")
|
| 673 |
if selected_position != "All":
|
| 674 |
filtered_df = filtered_df.query("Sample_Position == @selected_position")
|
|
|
|
| 683 |
)
|
| 684 |
|
| 685 |
with st.sidebar.expander("Preview Filtered Data"):
|
|
|
|
| 686 |
display_columns = [
|
| 687 |
"Activity_Start_Date_Time",
|
| 688 |
"Sample_Position",
|
|
|
|
| 691 |
"Org_Result_Unit",
|
| 692 |
"Reporting_Year",
|
| 693 |
]
|
| 694 |
+
render_filtered_data_preview(filtered_df, display_columns)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
|
| 696 |
if not filtered_df.empty:
|
| 697 |
+
fig = plot_analyte_trends(filtered_df, selected_analytes, selected_position)
|
| 698 |
st.pyplot(fig)
|
| 699 |
else:
|
| 700 |
st.warning(
|
|
|
|
| 706 |
st.session_state.ENABLE_ALTAIR = st.sidebar.toggle(
|
| 707 |
"Interactive Plots", value=st.session_state.ENABLE_ALTAIR
|
| 708 |
)
|
| 709 |
+
st.info(
|
| 710 |
+
get_reporting_year_info_message(
|
| 711 |
+
st.session_state.reporting_month, st.session_state.dataset_end_date.year
|
| 712 |
+
)
|
| 713 |
+
)
|
| 714 |
default_analytes = [
|
| 715 |
"Dissolved Oxygen",
|
| 716 |
"Salinity",
|
|
|
|
| 724 |
if x not in default_analytes
|
| 725 |
]
|
| 726 |
|
| 727 |
+
selected_analytes = render_sidebar_analyte_multiselect(
|
| 728 |
+
all_analytes, default_analytes, key_prefix="sector_trends"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
)
|
| 730 |
if selected_analytes and not data["raw_df"].empty:
|
| 731 |
if st.session_state.ENABLE_ALTAIR:
|
|
|
|
| 743 |
st.title("Parameter Correlations")
|
| 744 |
subset_by = "Sector"
|
| 745 |
st.sidebar.markdown("### Filter Options")
|
| 746 |
+
position_filter = render_sidebar_position_filter_selectbox(
|
| 747 |
+
key_prefix="parameter_correlation"
|
| 748 |
)
|
| 749 |
with st.spinner("Loading data for correlation plots..."):
|
| 750 |
analyte_names = [
|
|
|
|
| 846 |
cols[idx % 2].pyplot(fig)
|
| 847 |
plt.close()
|
| 848 |
with cols[idx % 2].expander(f"View {subset} Data"):
|
|
|
|
| 849 |
display_columns = [
|
| 850 |
"Activity_Start_Date_Time",
|
| 851 |
"Station_Number",
|
|
|
|
| 854 |
"Org_Result_Value",
|
| 855 |
"Org_Result_Unit",
|
| 856 |
]
|
| 857 |
+
render_filtered_data_preview(subset_df, display_columns)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
csv_buffer = io.StringIO()
|
| 859 |
subset_df.to_csv(csv_buffer, index=False)
|
| 860 |
st.download_button(
|
|
|
|
| 906 |
for x in sorted(raw_df["Org_Analyte_Name"].unique())
|
| 907 |
if x not in default_analytes
|
| 908 |
]
|
| 909 |
+
selected_analytes = render_sidebar_analyte_multiselect(
|
| 910 |
+
all_analytes=all_analytes,
|
| 911 |
+
default_analytes=default_analytes,
|
| 912 |
+
key_prefix="calendar",
|
| 913 |
+
help_text="Choose one or more analytes to display in the heatmap.",
|
|
|
|
| 914 |
)
|
| 915 |
|
| 916 |
# Filter Options
|
|
|
|
| 921 |
index=0,
|
| 922 |
key="calendar_sector_select",
|
| 923 |
)
|
| 924 |
+
position_filter = render_sidebar_position_filter_selectbox(key_prefix="calendar")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 925 |
|
| 926 |
def format_colormap_option(option):
|
| 927 |
append = ""
|
|
|
|
| 1084 |
|
| 1085 |
elif section == "Analytics":
|
| 1086 |
st.title("Analytics")
|
| 1087 |
+
analytics_data = get_analytics_data()
|
| 1088 |
|
| 1089 |
+
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
|
|
|
| 1090 |
|
| 1091 |
+
with col1:
|
| 1092 |
+
visits_df = pd.DataFrame(analytics_data["visits"])
|
| 1093 |
+
visits_df["timestamp"] = pd.to_datetime(visits_df["timestamp"])
|
| 1094 |
|
| 1095 |
+
daily_visits_df = (
|
| 1096 |
+
visits_df.groupby("date")["visitor_id"]
|
| 1097 |
+
.agg(["nunique", "count"])
|
| 1098 |
+
.reset_index()
|
| 1099 |
+
.rename(columns={"nunique": "Unique Visitors", "count": "Total Views"})
|
| 1100 |
+
)
|
| 1101 |
+
daily_visits_df["date"] = pd.to_datetime(daily_visits_df["date"])
|
| 1102 |
+
daily_visits_df = daily_visits_df.sort_values("date")
|
| 1103 |
|
| 1104 |
+
total_unique_visitors = visits_df["visitor_id"].nunique()
|
| 1105 |
+
total_views = len(visits_df)
|
| 1106 |
+
avg_views_per_visitor = total_views / total_unique_visitors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1107 |
|
| 1108 |
+
st.subheader("Visitor Metrics")
|
| 1109 |
+
metrics_col1, metrics_col2, metrics_col3 = st.columns(3)
|
| 1110 |
+
metrics_col1.metric("Total Unique Visitors", total_unique_visitors)
|
| 1111 |
+
metrics_col2.metric("Total Page Views", total_views)
|
| 1112 |
+
metrics_col3.metric("Avg Views per Visitor", f"{avg_views_per_visitor:.1f}")
|
| 1113 |
|
| 1114 |
+
st.subheader("Daily Statistics")
|
| 1115 |
+
st.dataframe(
|
| 1116 |
+
daily_visits_df.style.format(
|
| 1117 |
+
{"Unique Visitors": "{:,.0f}", "Total Views": "{:,.0f}"}
|
| 1118 |
+
),
|
| 1119 |
+
hide_index=True,
|
| 1120 |
+
)
|
| 1121 |
|
| 1122 |
+
with col2:
|
| 1123 |
+
section_visits_df = pd.DataFrame(
|
| 1124 |
+
{
|
| 1125 |
+
"Section": analytics_data["section_counts"].keys(),
|
| 1126 |
+
"Views": analytics_data["section_counts"].values(),
|
| 1127 |
+
}
|
| 1128 |
+
)
|
| 1129 |
+
section_visits_df = section_visits_df.sort_values("Views", ascending=True)
|
| 1130 |
|
| 1131 |
+
st.subheader("Total Section Views")
|
| 1132 |
+
st.bar_chart(section_visits_df.set_index("Section"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1133 |
|
| 1134 |
+
with st.expander("Raw Visit Data"):
|
| 1135 |
+
visits_df = pd.DataFrame(analytics_data["visits"])
|
| 1136 |
+
visits_df["timestamp"] = pd.to_datetime(visits_df["timestamp"])
|
| 1137 |
+
st.dataframe(visits_df)
|
| 1138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1139 |
|
| 1140 |
if st.session_state.ENABLE_TIMING:
|
| 1141 |
st.markdown("---")
|
components.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import calendar
|
| 2 |
+
import datetime
|
| 3 |
+
from typing import List, Optional, Tuple
|
| 4 |
+
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import streamlit as st
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def on_date_change():
|
| 10 |
+
"""Callback for date input changes"""
|
| 11 |
+
start = st.session_state.dataset_start_date
|
| 12 |
+
end = st.session_state.dataset_end_date
|
| 13 |
+
|
| 14 |
+
if start > end:
|
| 15 |
+
st.error("Start date must be before end date")
|
| 16 |
+
return
|
| 17 |
+
|
| 18 |
+
st.session_state.start_date = start
|
| 19 |
+
st.session_state.end_date = end
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def get_reporting_year_info_message(reporting_month: int, example_year: int) -> str:
|
| 23 |
+
"""Generate standardized info message about reporting year grouping."""
|
| 24 |
+
return f"""
|
| 25 |
+
📅 **Data is grouped by reporting years**:
|
| 26 |
+
- Each reporting year ends in **{calendar.month_name[reporting_month]}**.
|
| 27 |
+
- Example: Reporting year **{example_year}** covers
|
| 28 |
+
**{calendar.month_abbr[reporting_month + 1 if reporting_month < 12 else 1]}. {example_year - 1 if reporting_month < 12 else example_year}**
|
| 29 |
+
through **{calendar.month_abbr[reporting_month]}. {example_year}**.
|
| 30 |
+
- **Note**: You can change the reporting year end month in the **Dataset Settings** in the sidebar.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def render_date_filters(
|
| 35 |
+
min_date: datetime.date,
|
| 36 |
+
max_date: datetime.date,
|
| 37 |
+
key_prefix: str = "dataset",
|
| 38 |
+
on_change=None,
|
| 39 |
+
) -> Tuple[datetime.date, datetime.date]:
|
| 40 |
+
"""Render date range filter controls"""
|
| 41 |
+
col1, col2 = st.columns(2)
|
| 42 |
+
|
| 43 |
+
with col1:
|
| 44 |
+
start_date = st.date_input(
|
| 45 |
+
"Start Date",
|
| 46 |
+
value=min_date,
|
| 47 |
+
min_value=min_date,
|
| 48 |
+
max_value=max_date,
|
| 49 |
+
format="MM/DD/YYYY",
|
| 50 |
+
key=f"{key_prefix}_start_date",
|
| 51 |
+
on_change=on_change,
|
| 52 |
+
)
|
| 53 |
+
if not isinstance(start_date, datetime.date):
|
| 54 |
+
raise TypeError("Expected start_date to be a datetime.date")
|
| 55 |
+
with col2:
|
| 56 |
+
end_date = st.date_input(
|
| 57 |
+
"End Date",
|
| 58 |
+
value=max_date,
|
| 59 |
+
min_value=min_date,
|
| 60 |
+
max_value=max_date,
|
| 61 |
+
format="MM/DD/YYYY",
|
| 62 |
+
key=f"{key_prefix}_end_date",
|
| 63 |
+
on_change=on_change,
|
| 64 |
+
)
|
| 65 |
+
if not isinstance(end_date, datetime.date):
|
| 66 |
+
raise TypeError("Expected end_date to be a datetime.date")
|
| 67 |
+
|
| 68 |
+
return start_date, end_date
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def render_sidebar_position_filter_selectbox(
|
| 72 |
+
key_prefix: str = "", default: str = "All"
|
| 73 |
+
) -> str:
|
| 74 |
+
"""Render sample position filter"""
|
| 75 |
+
return st.sidebar.selectbox(
|
| 76 |
+
"Sample Position:",
|
| 77 |
+
["All", "Surface", "Bottom"],
|
| 78 |
+
index=["All", "Surface", "Bottom"].index(default),
|
| 79 |
+
key=f"{key_prefix}_position_filter",
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def render_sidebar_analyte_multiselect(
|
| 84 |
+
all_analytes: List[str],
|
| 85 |
+
default_analytes: Optional[List[str]] = None,
|
| 86 |
+
key_prefix: str = "",
|
| 87 |
+
help_text: str = "Choose one or more analytes to display.",
|
| 88 |
+
) -> List[str]:
|
| 89 |
+
"""Render analyte multi-select"""
|
| 90 |
+
if default_analytes is None:
|
| 91 |
+
default_analytes = []
|
| 92 |
+
|
| 93 |
+
return st.sidebar.multiselect(
|
| 94 |
+
"Select Analytes:",
|
| 95 |
+
options=all_analytes,
|
| 96 |
+
default=default_analytes,
|
| 97 |
+
key=f"{key_prefix}_analyte_select",
|
| 98 |
+
help=help_text,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def render_filtered_data_preview(
|
| 103 |
+
df: pd.DataFrame,
|
| 104 |
+
display_columns: List[str],
|
| 105 |
+
set_index_col: str | None = None,
|
| 106 |
+
height: int = 300,
|
| 107 |
+
) -> None:
|
| 108 |
+
"""Render preview of filtered dataset"""
|
| 109 |
+
if set_index_col:
|
| 110 |
+
df = df.set_index(set_index_col)
|
| 111 |
+
else:
|
| 112 |
+
df = df.reset_index()
|
| 113 |
+
|
| 114 |
+
st.markdown(f"**{len(df):,}** records")
|
| 115 |
+
st.dataframe(
|
| 116 |
+
df[display_columns].style.format(precision=2),
|
| 117 |
+
use_container_width=True,
|
| 118 |
+
height=height,
|
| 119 |
+
)
|