-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_html.py
More file actions
1474 lines (1258 loc) · 51.5 KB
/
export_html.py
File metadata and controls
1474 lines (1258 loc) · 51.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Static HTML export for the Autobiographer dashboard.
Generates a fully self-contained HTML report from a Last.fm listening
history CSV. Optionally enriches the report with a Foursquare/Swarm check-in
directory to add a Places tab with a world map and city/country breakdowns.
All JavaScript (plotly.js) is inlined — no external network calls are made
during rendering, preserving full data sovereignty.
Usage::
# Listening data only
python export_html.py data/tracks.csv
# With Swarm check-ins (adds Places map tab)
python export_html.py data/tracks.csv --swarm-dir data/swarm/
# Read both paths from local_settings.json
python export_html.py --from-settings
# Specify an output path (default: autobiographer_report.html)
python export_html.py data/tracks.csv --output reports/my_report.html
"""
from __future__ import annotations
import argparse
import os
import sys
from datetime import date as date_type
from datetime import datetime
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
from analysis_utils import (
get_artist_monthly_ranks,
get_cumulative_plays,
get_day_hour_heatmap,
get_forgotten_favorites,
get_genre_weekly,
get_hourly_distribution,
get_listening_intensity,
get_listening_streaks,
get_milestones,
get_top_entities,
load_listening_data,
load_swarm_data,
)
from components.theme import COLORWAY, SEQUENTIAL_SCALE, apply_dark_theme
from core.local_settings import LocalSettings
# ── Palette constants (mirrors components/theme.py) ────────────────────────
_TEAL = "#00C8C8"
_AMBER = "#FFA014"
_BG = "#0e1117"
_PANEL_BG = "#1a1e26"
_BORDER = "#2d3142"
_TEXT = "#fafafa"
_MUTED = "#9aa0ae"
# ── HTML helpers ────────────────────────────────────────────────────────────
def _chart_div(fig: go.Figure, *, include_js: bool = False) -> str:
"""Serialise a Plotly figure to an HTML div string.
Args:
fig: Any Plotly Figure to serialise.
include_js: When True, embeds the full plotly.js bundle inline.
Set this to True for the very first chart in the document so
the bundle is available for all subsequent ``include_js=False``
charts.
Returns:
HTML string containing the chart div (and optionally the plotly.js
script tag).
"""
return str(
pio.to_html(
fig,
full_html=False,
include_plotlyjs="inline" if include_js else False,
config={"responsive": True},
)
)
def _table_html(df: pd.DataFrame) -> str:
"""Render a DataFrame as a styled dark HTML table.
Args:
df: DataFrame to render. Empty DataFrames produce a placeholder message.
Returns:
HTML string for the table or an empty-state paragraph.
"""
if df.empty:
return "<p class='empty-msg'>No data available.</p>"
headers = "".join(f"<th>{col}</th>" for col in df.columns)
rows = ""
for _, row in df.iterrows():
cells = "".join(f"<td>{val}</td>" for val in row)
rows += f"<tr>{cells}</tr>\n"
return (
f"<div class='table-wrap'>"
f"<table><thead><tr>{headers}</tr></thead>"
f"<tbody>{rows}</tbody></table>"
f"</div>"
)
# ── Shared HTML scaffold ────────────────────────────────────────────────────
def _html_styles() -> str:
"""Return the CSS block shared by all self-contained HTML exports."""
return f"""
*, *::before, *::after {{ box-sizing: border-box; margin: 0; padding: 0; }}
body {{
background: {_BG};
color: {_TEXT};
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 15px;
line-height: 1.5;
}}
.container {{ max-width: 1200px; margin: 0 auto; padding: 2rem 1.5rem; }}
.app-header {{
display: flex;
align-items: baseline;
gap: 0.75rem;
margin-bottom: 2rem;
border-bottom: 1px solid {_BORDER};
padding-bottom: 1rem;
}}
.app-header h1 {{ font-size: 1.75rem; font-weight: 700; color: {_TEAL}; }}
.app-header .subtitle {{ color: {_MUTED}; font-size: 0.9rem; }}
.metrics-row {{
display: flex;
gap: 1rem;
flex-wrap: wrap;
margin-bottom: 2rem;
}}
.metric-card {{
background: {_PANEL_BG};
border: 1px solid {_BORDER};
border-radius: 8px;
padding: 1rem 1.5rem;
min-width: 160px;
flex: 1;
}}
.metric-card .label {{
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.06em;
color: {_MUTED};
margin-bottom: 0.25rem;
}}
.metric-card .value {{
font-size: 1.6rem;
font-weight: 700;
color: {_TEAL};
}}
.tab-nav {{
display: flex;
gap: 0.5rem;
margin-bottom: 1.5rem;
border-bottom: 1px solid {_BORDER};
padding-bottom: 0;
}}
.tab-btn {{
background: transparent;
border: none;
border-bottom: 2px solid transparent;
color: {_MUTED};
cursor: pointer;
font-size: 0.95rem;
padding: 0.5rem 1rem;
margin-bottom: -1px;
transition: color 0.15s, border-color 0.15s;
}}
.tab-btn:hover {{ color: {_TEXT}; }}
.tab-btn.active {{ color: {_TEAL}; border-bottom-color: {_TEAL}; font-weight: 600; }}
.tab-content {{ display: none; }}
.tab-content.active {{ display: block; }}
.section-title {{
font-size: 1.1rem;
font-weight: 600;
color: {_TEXT};
margin: 1.5rem 0 0.75rem 0;
}}
.section-desc {{
font-size: 0.85rem;
color: {_MUTED};
margin-bottom: 1rem;
}}
.grid-2 {{ display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }}
.grid-3 {{ display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 1rem; }}
@media (max-width: 800px) {{ .grid-2, .grid-3 {{ grid-template-columns: 1fr; }} }}
.card {{
background: {_PANEL_BG};
border: 1px solid {_BORDER};
border-radius: 8px;
padding: 1rem;
overflow: hidden;
}}
.table-wrap {{ overflow-x: auto; }}
table {{
width: 100%;
border-collapse: collapse;
font-size: 0.875rem;
}}
th {{
text-align: left;
padding: 0.4rem 0.75rem;
background: rgba(0,200,200,0.08);
color: {_TEAL};
font-weight: 600;
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.04em;
}}
td {{
padding: 0.4rem 0.75rem;
border-bottom: 1px solid {_BORDER};
color: {_TEXT};
}}
tr:last-child td {{ border-bottom: none; }}
tr:hover td {{ background: rgba(255,255,255,0.03); }}
.streak-row {{ display: flex; gap: 1rem; flex-wrap: wrap; margin-top: 0.5rem; }}
.streak-card {{
background: {_PANEL_BG};
border: 1px solid {_BORDER};
border-radius: 8px;
padding: 0.75rem 1.25rem;
}}
.streak-card .s-label {{ font-size: 0.75rem; color: {_MUTED}; }}
.streak-card .s-value {{ font-size: 1.4rem; font-weight: 700; color: {_AMBER}; }}
.empty-msg {{ color: {_MUTED}; font-style: italic; padding: 0.5rem 0; }}
.app-footer {{
margin-top: 3rem;
padding-top: 1rem;
border-top: 1px solid {_BORDER};
color: {_MUTED};
font-size: 0.8rem;
text-align: center;
}}
"""
# ── Places tab builder ──────────────────────────────────────────────────────
def _build_places_html(swarm_df: pd.DataFrame) -> str:
"""Generate the HTML content for the Places tab from Swarm check-in data.
Produces a world scatter-geo map (zero external tiles), a top-cities bar
chart, and a top-countries bar chart — all inlined as Plotly divs.
Args:
swarm_df: Swarm check-in DataFrame with at minimum ``lat``, ``lng``,
``city``, and ``country`` columns.
Returns:
HTML string for the inner content of the Places tab (no wrapper div).
"""
geo_data = (
swarm_df.dropna(subset=["lat", "lng"])
.groupby(["lat", "lng", "city", "country"], as_index=False)
.size()
.rename(columns={"size": "Check-ins"})
)
fig_map = px.scatter_geo(
geo_data,
lat="lat",
lon="lng",
size="Check-ins",
color="Check-ins",
hover_name="city",
hover_data={"country": True, "Check-ins": True, "lat": False, "lng": False},
title="Where You've Been",
color_continuous_scale=SEQUENTIAL_SCALE,
size_max=25,
projection="natural earth",
)
fig_map.update_layout(
height=500,
geo=dict(
bgcolor="rgba(0,0,0,0)",
landcolor=_PANEL_BG,
oceancolor=_BG,
countrycolor=_BORDER,
subunitcolor=_BORDER,
showland=True,
showocean=True,
showcountries=True,
showlakes=False,
),
)
apply_dark_theme(fig_map)
top_cities = (
swarm_df.groupby("city")
.size()
.reset_index(name="Check-ins")
.sort_values("Check-ins", ascending=False)
.head(20)
)
fig_cities = px.bar(top_cities, x="Check-ins", y="city", orientation="h", title="Top 20 Cities")
fig_cities.update_layout(yaxis={"categoryorder": "total ascending"}, height=500)
apply_dark_theme(fig_cities)
top_countries = (
swarm_df.groupby("country")
.size()
.reset_index(name="Check-ins")
.sort_values("Check-ins", ascending=False)
)
fig_countries = px.bar(
top_countries, x="Check-ins", y="country", orientation="h", title="Countries Visited"
)
fig_countries.update_layout(yaxis={"categoryorder": "total ascending"})
apply_dark_theme(fig_countries)
total_checkins = len(swarm_df)
unique_cities = swarm_df["city"].nunique()
unique_countries = swarm_df["country"].nunique()
div_map = _chart_div(fig_map)
div_cities = _chart_div(fig_cities)
div_countries = _chart_div(fig_countries)
return f"""
<div class="metrics-row" style="margin-bottom:1.5rem">
<div class="metric-card">
<div class="label">Total Check-ins</div>
<div class="value">{total_checkins:,}</div>
</div>
<div class="metric-card">
<div class="label">Cities Visited</div>
<div class="value">{unique_cities:,}</div>
</div>
<div class="metric-card">
<div class="label">Countries Visited</div>
<div class="value">{unique_countries:,}</div>
</div>
</div>
<div class="card">{div_map}</div>
<div class="grid-2" style="margin-top:1rem">
<div class="card">{div_cities}</div>
<div class="card">{div_countries}</div>
</div>"""
# ── Music page export ───────────────────────────────────────────────────────
def build_music_page_html(
filtered_df: pd.DataFrame,
start: date_type,
end: date_type,
generated_at: str,
) -> str:
"""Build a self-contained HTML export of the Music listening page.
Mirrors the charts rendered by ``pages/music.py`` for the given date range:
quick-facts metrics, daily scrobbles, listening clock, plays growth, top
artists/tracks/albums, streamgraph, bump chart, and monthly activity.
Args:
filtered_df: Listening history already filtered to ``[start, end]``.
start: Start date of the selected range (used in the title and footer).
end: End date of the selected range.
generated_at: Human-readable UTC timestamp shown in the footer.
Returns:
A complete ``<!DOCTYPE html>`` document as a string.
"""
if filtered_df.empty:
return f"""<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">
<title>Listening Report</title><style>{_html_styles()}</style></head><body>
<div class="container"><header class="app-header"><h1>Listening Report</h1>
<span class="subtitle">{start} – {end}</span></header>
<p class="empty-msg">No plays found in the selected date range.</p>
<footer class="app-footer">Generated by Autobiographer — {generated_at}</footer>
</div></body></html>"""
n = len(filtered_df)
days = max(1, (end - start).days + 1)
hours = n * 3.5 / 60
daily_counts = filtered_df.groupby(filtered_df["date_text"].dt.date).size()
peak_date = daily_counts.idxmax()
most_active_label = f"{int(daily_counts[peak_date])} on {peak_date.strftime('%d %b')}"
# ── Entity columns (top-5 per entity) ────────────────────────────────────
def _entity_col_html(entity: str, label: str) -> str:
top5 = get_top_entities(filtered_df, entity, limit=5)
unique_n = int(filtered_df[entity].nunique()) if entity in filtered_df.columns else 0
items = "".join(
f'<li style="padding:0.2rem 0;border-bottom:1px solid {_BORDER}">'
f'<span style="color:{_TEXT}">{row[entity]}</span>'
f'<span style="color:{_MUTED};float:right">{int(row["Plays"])}</span></li>'
for _, row in top5.iterrows()
)
return (
f'<div class="card"><div class="section-title">{label}</div>'
f'<div style="color:{_MUTED};font-size:0.8rem;margin-bottom:0.5rem">'
f"{unique_n:,} unique</div>"
f'<ol style="padding-left:1.2rem;margin:0;list-style:decimal">{items}</ol></div>'
)
entity_cols_html = (
f'<div class="grid-3" style="margin-bottom:1.5rem">'
f"{_entity_col_html('artist', 'Artists')}"
f"{_entity_col_html('album', 'Albums')}"
f"{_entity_col_html('track', 'Tracks')}"
f"</div>"
)
# Closure tracks which chart gets the inlined plotly.js bundle.
first_chart: list[bool] = [True]
def _chart(fig: go.Figure) -> str:
include = first_chart[0]
first_chart[0] = False
return _chart_div(fig, include_js=include)
# ── Daily scrobbles ──────────────────────────────────────────────────────
intensity_d = get_listening_intensity(filtered_df, "D")
div_daily = ""
if not intensity_d.empty:
fig = px.bar(intensity_d, x="date", y="Plays", title="Daily Scrobbles")
fig.update_traces(marker_color=_TEAL)
apply_dark_theme(fig)
div_daily = f'<div class="card">{_chart(fig)}</div>'
# ── Listening clock ──────────────────────────────────────────────────────
hourly = get_hourly_distribution(filtered_df)
div_clock = ""
if not hourly.empty:
all_hours = pd.DataFrame({"hour": range(24)})
hourly = all_hours.merge(hourly, on="hour", how="left").fillna(0)
hourly["Plays"] = hourly["Plays"].astype(int)
peak = hourly.loc[hourly["Plays"].idxmax()]
peak_label = f"{int(peak['hour']):02d}:00 ({int(peak['Plays'])} plays)"
fig = px.bar(hourly, x="hour", y="Plays", title=f"Listening Clock · Peak: {peak_label}")
fig.update_traces(marker_color=_AMBER)
fig.update_xaxes(tickmode="linear", tick0=0, dtick=2)
apply_dark_theme(fig)
div_clock = f'<div class="card">{_chart(fig)}</div>'
# ── Plays growth ─────────────────────────────────────────────────────────
cumulative = get_cumulative_plays(filtered_df)
div_growth = ""
if not cumulative.empty:
fig = px.area(cumulative, x="date", y="CumulativePlays", title="Plays Growth")
fig.update_traces(line_color=_TEAL, fillcolor="rgba(0,200,200,0.15)")
apply_dark_theme(fig)
div_growth = f'<div class="card">{_chart(fig)}</div>'
# ── Top artists / tracks / albums ────────────────────────────────────────
def _top_bar(entity: str, limit: int = 20) -> str:
top = get_top_entities(filtered_df, entity, limit=limit)
if top.empty:
return ""
fig = px.bar(
top,
x="Plays",
y=entity,
orientation="h",
title=f"Top {limit} {entity.capitalize()}s",
)
fig.update_layout(yaxis={"categoryorder": "total ascending"}, height=600)
apply_dark_theme(fig)
return f'<div class="card" style="margin-top:1rem">{_chart(fig)}</div>'
div_artists = _top_bar("artist")
div_tracks = _top_bar("track")
div_albums = _top_bar("album")
# ── Streamgraph ──────────────────────────────────────────────────────────
weekly = get_genre_weekly(filtered_df)
div_stream = ""
if not weekly.empty:
fig = px.area(
weekly,
x="date",
y="scrobbles",
color="genre",
title="Artist Listening Timeline",
color_discrete_sequence=COLORWAY,
labels={"genre": "Artist", "scrobbles": "Scrobbles", "date": ""},
)
fig.update_layout(legend_title_text="Artist")
apply_dark_theme(fig)
div_stream = f'<div class="card" style="margin-top:1rem">{_chart(fig)}</div>'
# ── Bump chart ───────────────────────────────────────────────────────────
ranks = get_artist_monthly_ranks(filtered_df)
div_bump = ""
if not ranks.empty and ranks["month"].nunique() >= 2:
fig = px.line(
ranks,
x="month",
y="rank",
color="artist",
title="Artist Rank Over Time",
color_discrete_sequence=COLORWAY,
labels={"month": "", "rank": "Rank", "artist": "Artist"},
markers=True,
)
fig.update_yaxes(autorange="reversed", dtick=1, title="Rank")
apply_dark_theme(fig)
div_bump = f'<div class="card" style="margin-top:1rem">{_chart(fig)}</div>'
# ── Monthly intensity + cumulative ───────────────────────────────────────
intensity_m = get_listening_intensity(filtered_df, "ME")
div_monthly = ""
if not intensity_m.empty:
fig = px.line(intensity_m, x="date", y="Plays", title="Monthly Listening Activity")
apply_dark_theme(fig)
div_monthly = f'<div class="card" style="margin-top:1rem">{_chart(fig)}</div>'
div_cumulative = ""
if not cumulative.empty:
fig = px.area(cumulative, x="date", y="CumulativePlays", title="Total Plays (Cumulative)")
fig.update_traces(line_color=_TEAL, fillcolor="rgba(0,200,200,0.15)")
apply_dark_theme(fig)
div_cumulative = f'<div class="card" style="margin-top:1rem">{_chart(fig)}</div>'
return f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Listening Report · {start} – {end}</title>
<style>{_html_styles()}</style>
</head>
<body>
<div class="container">
<header class="app-header">
<h1>Listening Report</h1>
<span class="subtitle">{start} – {end}</span>
</header>
<div class="metrics-row">
<div class="metric-card">
<div class="label">Scrobbles</div>
<div class="value">{n:,}</div>
</div>
<div class="metric-card">
<div class="label">Listening Time</div>
<div class="value">{hours:.0f}h</div>
</div>
<div class="metric-card">
<div class="label">Avg / Day</div>
<div class="value">{n / days:.0f}</div>
</div>
<div class="metric-card">
<div class="label">Most Active Day</div>
<div class="value" style="font-size:0.95rem;padding-top:0.3rem">{most_active_label}</div>
</div>
</div>
<div class="section-title">Top Artists · Albums · Tracks</div>
{entity_cols_html}
<div class="section-title">Daily Activity</div>
{div_daily}
<div class="grid-2" style="margin-top:1rem">
{div_clock}
{div_growth}
</div>
<div class="section-title">Top Charts</div>
{div_artists}
{div_tracks}
{div_albums}
<div class="section-title">Timeline</div>
{div_stream}
{div_bump}
{div_monthly}
{div_cumulative}
<footer class="app-footer">
Generated by Autobiographer — {generated_at}
— all data processed locally, no external network calls.
</footer>
</div>
</body>
</html>"""
# ── Overview page export ────────────────────────────────────────────────────
def build_overview_page_html(
df: pd.DataFrame,
swarm_df: pd.DataFrame | None,
generated_at: str,
) -> str:
"""Build a self-contained HTML export of the Overview page.
Args:
df: Listening history DataFrame.
swarm_df: Optional Swarm check-in DataFrame.
generated_at: Human-readable UTC timestamp shown in the footer.
Returns:
A complete ``<!DOCTYPE html>`` document as a string.
"""
total_scrobbles = len(df)
unique_artists = df["artist"].nunique() if "artist" in df.columns else 0
unique_albums = df["album"].nunique() if "album" in df.columns else 0
unique_tracks = df["track"].nunique() if "track" in df.columns else 0
active_days = df["date_text"].dt.date.nunique()
date_min = df["date_text"].min()
date_max = df["date_text"].max()
date_range_str = (
f"{date_min.strftime('%b %Y')} – {date_max.strftime('%b %Y')}"
if pd.notna(date_min) and pd.notna(date_max)
else "N/A"
)
swarm_html = ""
if swarm_df is not None and not swarm_df.empty:
total_checkins = len(swarm_df)
unique_venues = swarm_df["venue"].nunique() if "venue" in swarm_df.columns else 0
unique_cities = swarm_df["city"].nunique() if "city" in swarm_df.columns else 0
unique_countries = swarm_df["country"].nunique() if "country" in swarm_df.columns else 0
swarm_html = f"""
<div class="metric-card">
<div class="label">Check-ins</div><div class="value">{total_checkins:,}</div>
</div>
<div class="metric-card">
<div class="label">Venues</div><div class="value">{unique_venues:,}</div>
</div>
<div class="metric-card">
<div class="label">Cities</div><div class="value">{unique_cities:,}</div>
</div>
<div class="metric-card">
<div class="label">Countries</div><div class="value">{unique_countries:,}</div>
</div>"""
first_chart: list[bool] = [True]
def _chart(fig: go.Figure) -> str:
include = first_chart[0]
first_chart[0] = False
return _chart_div(fig, include_js=include)
def _top_bar(entity: str, limit: int = 20) -> str:
top = get_top_entities(df, entity, limit=limit)
if top.empty:
return ""
fig = px.bar(
top,
x="Plays",
y=entity,
orientation="h",
title=f"Top {limit} {entity.capitalize()}s",
)
fig.update_layout(yaxis={"categoryorder": "total ascending"}, height=500)
apply_dark_theme(fig)
return f'<div class="card" style="margin-top:1rem">{_chart(fig)}</div>'
div_artists = _top_bar("artist")
div_tracks = _top_bar("track")
div_albums = _top_bar("album")
return f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Overview · {date_range_str}</title>
<style>{_html_styles()}</style>
</head>
<body>
<div class="container">
<header class="app-header">
<h1>Overview</h1>
<span class="subtitle">{date_range_str}</span>
</header>
<div class="metrics-row">
<div class="metric-card">
<div class="label">Scrobbles</div><div class="value">{total_scrobbles:,}</div>
</div>
<div class="metric-card">
<div class="label">Artists</div><div class="value">{unique_artists:,}</div>
</div>
<div class="metric-card">
<div class="label">Albums</div><div class="value">{unique_albums:,}</div>
</div>
<div class="metric-card">
<div class="label">Tracks</div><div class="value">{unique_tracks:,}</div>
</div>
<div class="metric-card">
<div class="label">Active Days</div><div class="value">{active_days:,}</div>
</div>
{swarm_html}
</div>
<div class="section-title">Top Artists</div>
{div_artists}
<div class="section-title">Top Tracks</div>
{div_tracks}
<div class="section-title">Top Albums</div>
{div_albums}
<footer class="app-footer">
Generated by Autobiographer — {generated_at}
— all data processed locally, no external network calls.
</footer>
</div>
</body>
</html>"""
# ── Insights page export ─────────────────────────────────────────────────────
def build_insights_page_html(
filtered_df: pd.DataFrame,
generated_at: str,
) -> str:
"""Build a self-contained HTML export of the Insights page.
Args:
filtered_df: Listening history filtered by the page's active filters.
generated_at: Human-readable UTC timestamp shown in the footer.
Returns:
A complete ``<!DOCTYPE html>`` document as a string.
"""
if filtered_df.empty:
return f"""<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">
<title>Insights</title><style>{_html_styles()}</style></head><body>
<div class="container"><header class="app-header"><h1>Insights & Narrative</h1></header>
<p class="empty-msg">No data found for the selected filters.</p>
<footer class="app-footer">Generated by Autobiographer — {generated_at}</footer>
</div></body></html>"""
first_chart: list[bool] = [True]
def _chart(fig: go.Figure) -> str:
include = first_chart[0]
first_chart[0] = False
return _chart_div(fig, include_js=include)
# ── Sunburst ─────────────────────────────────────────────────────────────
div_sunburst = ""
if "album" in filtered_df.columns:
top_artists = get_top_entities(filtered_df, "artist", limit=12)["artist"].tolist()
subset = filtered_df[filtered_df["artist"].isin(top_artists)].copy()
grouped = subset.groupby(["artist", "album"]).size().reset_index(name="scrobbles")
if not grouped.empty:
fig = px.sunburst(
grouped,
path=["artist", "album"],
values="scrobbles",
title="Music Listening Hierarchy",
color_discrete_sequence=COLORWAY,
)
fig.update_traces(textinfo="label+percent parent")
apply_dark_theme(fig)
div_sunburst = f'<div class="card">{_chart(fig)}</div>'
# ── Top entities tables ───────────────────────────────────────────────────
def _top_table(entity: str, limit: int = 20) -> str:
top = get_top_entities(filtered_df, entity, limit=limit)
return _table_html(top)
# ── Time patterns ─────────────────────────────────────────────────────────
hourly = get_hourly_distribution(filtered_df)
div_hourly = ""
if not hourly.empty:
fig = px.bar(hourly, x="hour", y="Plays", title="Listening by Hour of Day")
apply_dark_theme(fig)
div_hourly = f'<div class="card">{_chart(fig)}</div>'
heatmap_pivot = get_day_hour_heatmap(filtered_df)
div_heatmap = ""
if not heatmap_pivot.empty:
fig = px.imshow(
heatmap_pivot,
labels=dict(x="Hour of Day", y="Day of Week", color="Plays"),
title="Listening Intensity (Day vs Hour)",
aspect="auto",
color_continuous_scale=SEQUENTIAL_SCALE,
)
apply_dark_theme(fig)
div_heatmap = f'<div class="card">{_chart(fig)}</div>'
# ── Narrative ─────────────────────────────────────────────────────────────
milestones_html = _table_html(get_milestones(filtered_df))
streaks = get_listening_streaks(filtered_df)
forgotten = get_forgotten_favorites(filtered_df)
forgotten_html = (
_table_html(forgotten)
if not forgotten.empty
else "<p class='empty-msg'>No forgotten favorites identified.</p>"
)
return f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Insights & Narrative</title>
<style>{_html_styles()}</style>
</head>
<body>
<div class="container">
<header class="app-header">
<h1>Insights & Narrative</h1>
</header>
<div class="section-title">Music Listening Hierarchy</div>
{div_sunburst}
<div class="grid-2" style="margin-top:1.5rem">
<div>
<div class="section-title">Top Artists</div>
<div class="card">{_top_table("artist")}</div>
</div>
<div>
<div class="section-title">Top Tracks</div>
<div class="card">{_top_table("track")}</div>
</div>
</div>
<div class="section-title">Time-based Patterns</div>
<div class="grid-2">
{div_hourly}
{div_heatmap}
</div>
<div class="section-title">Autobiographical Milestones</div>
<div class="card">{milestones_html}</div>
<div class="section-title">Listening Streaks</div>
<div class="streak-row">
<div class="streak-card">
<div class="s-label">Longest Streak</div>
<div class="s-value">{streaks["longest_streak"]} days</div>
</div>
<div class="streak-card">
<div class="s-label">Current Streak</div>
<div class="s-value">{streaks["current_streak"]} days</div>
</div>
</div>
<div class="section-title">Forgotten Favorites</div>
<div class="card">{forgotten_html}</div>
<footer class="app-footer">
Generated by Autobiographer — {generated_at}
— all data processed locally, no external network calls.
</footer>
</div>
</body>
</html>"""
# ── Places / Check-in Insights page exports ──────────────────────────────────
def build_checkin_insights_html(
swarm_df: pd.DataFrame,
generated_at: str,
) -> str:
"""Build a self-contained HTML export of the Check-in Insights page.
Args:
swarm_df: Swarm/Foursquare check-in DataFrame.
generated_at: Human-readable UTC timestamp shown in the footer.
Returns:
A complete ``<!DOCTYPE html>`` document as a string.
"""
places_body = _build_places_html(swarm_df)
country_counts = (
swarm_df.groupby("country").size().reset_index(name="Check-ins")
if "country" in swarm_df.columns
else pd.DataFrame(columns=["country", "Check-ins"])
)
country_counts = country_counts.sort_values("Check-ins", ascending=False)
first_chart: list[bool] = [True]
def _chart(fig: go.Figure) -> str:
include = first_chart[0]
first_chart[0] = False
return _chart_div(fig, include_js=include)
div_countries_bar = ""
if not country_counts.empty:
fig = px.bar(
country_counts,
x="country",
y="Check-ins",
title=f"Check-ins across {len(country_counts)} countries",
)
apply_dark_theme(fig)
div_countries_bar = f'<div class="card">{_chart(fig)}</div>'
div_cities_bar = ""
if "city" in swarm_df.columns:
city_counts = (
swarm_df.groupby(["city", "country"]).size().reset_index(name="Check-ins")
if "country" in swarm_df.columns
else swarm_df.groupby("city").size().reset_index(name="Check-ins")
)
city_counts = city_counts.sort_values("Check-ins", ascending=False).head(20)
fig = px.bar(
city_counts,
x="Check-ins",
y="city",
orientation="h",
title="Top 20 Cities",
)
fig.update_layout(yaxis={"categoryorder": "total ascending"}, height=500)
apply_dark_theme(fig)
div_cities_bar = f'<div class="card" style="margin-top:1rem">{_chart(fig)}</div>'
return f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Check-in Insights</title>
<style>{_html_styles()}</style>
</head>
<body>
<div class="container">
<header class="app-header">
<h1>Check-in Insights</h1>
</header>
{places_body}
<div class="section-title">By Country</div>
{div_countries_bar}
<div class="card" style="margin-top:1rem">{_table_html(country_counts)}</div>
<div class="section-title">Top Cities</div>
{div_cities_bar}
<footer class="app-footer">
Generated by Autobiographer — {generated_at}
— all data processed locally, no external network calls.
</footer>
</div>
</body>
</html>"""
def build_places_page_html(
df: pd.DataFrame,
generated_at: str,
) -> str:
"""Build a self-contained HTML export of the Places (spatial) page.
The interactive 3D deck.gl map cannot be exported to static HTML; this
export renders an equivalent Plotly scatter-geo world map with city columns
and country counts.
Args: