Spaces:
Running
Running
File size: 7,666 Bytes
0a13764 | 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 | import importlib.util
import io
import re
from pathlib import Path
import pandas as pd
import plotly.graph_objects as go
import polars as pl
from great_tables import GT
from shiny import ui
# ---------------------------------------------------
# Mardown Files
# ------------
BASE_DIR = Path(__file__).resolve().parent.parent
INTRO_MD = (BASE_DIR / "md_files" / "intro.md").read_text(encoding="utf-8")
# ---------------------------------------------------
# Data Preliminaries
# ---------------------------------------------------
DATA_PATH = BASE_DIR / "data" / "daioe_scb_years_processed.parquet"
lf = pl.scan_parquet(DATA_PATH)
lf.collect_schema()
# ---------------------------------------------------
# Defining Input Values
# ---------------------------------------------------
# 1. SSYK12 Levels
LEVELS = lf.select(pl.col("level").unique().sort()).collect().to_series().to_list()
def build_choices_by_level(
lf: pl.LazyFrame,
levels: list[str],
) -> dict[str, dict[str, str]]:
out = {}
for lvl in levels:
occs = (
lf.filter(pl.col("level") == lvl)
.select(pl.col("occupation").unique().sort())
.collect()
.to_series()
.to_list()
)
out[lvl] = {o: o for o in occs}
return out
# 2. Men and Women
SEXES = lf.select(pl.col("sex").unique().sort()).collect().to_series().to_list()
# 3. Age groupings
AGE_ORDER = [
"Early Career 1 (16-24)",
"Early Career 2 (25-29)",
"Developing (30-34)",
"Mid-Career 1 (35-39)",
"Mid-Career 1 (40-44)",
"Mid-Career 2 (45-49)",
"Senior (50+)",
]
present = lf.select(pl.col("age_group").unique()).collect().to_series().to_list()
AGES = [x for x in AGE_ORDER if x in present]
YEARS = lf.select(pl.col("year").unique().sort()).collect().to_series().to_list()
# 4. Years from the dataset
YEAR_MIN, YEAR_MAX = min(YEARS), max(YEARS)
# 5. AI Sub-Indexes
METRICS: dict[str, str] = {
"daioe_genai": "๐ง Generative AI",
"daioe_allapps": "๐ All Applications",
"daioe_stratgames": "โ๏ธ Strategy Games",
"daioe_videogames": "๐ฎ Video Games (Real-Time)",
"daioe_imgrec": "๐ผ๏ธ๐ Image Recognition",
"daioe_imgcompr": "๐งฉ๐ผ๏ธ Image Comprehension",
"daioe_imggen": "๐๏ธ๐ผ๏ธ Image Generation",
"daioe_readcompr": "๐ Reading Comprehension",
"daioe_lngmod": "โ๏ธ๐ค Language Modeling",
"daioe_translat": "๐๐ค Translation",
"daioe_speechrec": "๐ฃ๏ธ๐๏ธ Speech Recognition",
}
first_cols = [
"level",
"ssyk_code",
"occupation",
"year",
"sex",
"age",
"age_group",
"count",
"weight_sum",
"chg_1y",
"chg_3y",
"chg_5y",
"pct_chg_1y",
"pct_chg_3y",
"pct_chg_5y",
]
# ---------------------------------------------------
# Shared UI Helpers
# ---------------------------------------------------
def apply_plot_style(fig: go.Figure, brand: dict[str, str]) -> go.Figure:
"""Apply a consistent visual style to Plotly charts."""
fig.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font={"family": "Nunito Sans", "color": brand["text"]},
hoverlabel={"bgcolor": "white", "font_size": 12},
margin={"l": 20, "r": 20, "t": 40, "b": 20},
)
fig.update_xaxes(gridcolor="#E5E5E5", zeroline=False)
fig.update_yaxes(gridcolor="#E5E5E5", zeroline=False)
return fig
def empty_figure(message: str, brand: dict[str, str]) -> go.Figure:
"""Create a styled empty Plotly figure with a centered message."""
fig = go.Figure()
fig.add_annotation(text=message, showarrow=False, font_size=16)
fig.update_xaxes(visible=False)
fig.update_yaxes(visible=False)
return apply_plot_style(fig, brand)
# ---------------------------------------------------
# Shared Table/Label Helpers
# ---------------------------------------------------
def metric_display_name(metric_key: str, metrics: dict[str, str]) -> str:
"""Return a clean human-readable metric label without leading icons."""
label = metrics.get(metric_key, metric_key.replace("_", " ").title())
return re.sub(r"^[^A-Za-z0-9]+\s*", "", label).strip()
def readable_column_name(col: str, metrics: dict[str, str]) -> str:
"""Convert raw dataset column names into readable table headers."""
exact = {
"ssyk_code": "SSYK Code",
"age_group": "Age Group",
"count": "Employees",
"year": "Year",
"sex": "Sex",
"level": "SSYK Level",
"occupation": "Occupation",
"chg_1y": "1-year Change",
"chg_3y": "3-year Change",
"chg_5y": "5-year Change",
}
if col in exact:
return exact[col]
col_l = col.lower()
if col_l.startswith("pctl_") and col_l.endswith("_wavg"):
metric_key = col[5:-5]
return f"{metric_display_name(metric_key, metrics)} Percentile (Weighted Avg)"
if col_l.endswith("_wavg"):
metric_key = col[:-5]
return f"{metric_display_name(metric_key, metrics)} (Weighted Avg)"
if col_l.endswith("_avg"):
metric_key = col[:-4]
return f"{metric_display_name(metric_key, metrics)} (Average)"
if col_l.endswith("_level_exposure"):
metric_key = col[: -len("_level_exposure")]
return f"{metric_display_name(metric_key, metrics)} Exposure Level"
fallback = col.replace("_", " ").title()
return (
fallback.replace("Ssyk", "SSYK").replace("Ai", "AI").replace("Daioe", "DAIOE")
)
def as_great_table_html(df, metrics: dict[str, str]) -> ui.TagChild:
"""Render a pandas DataFrame as Great Tables HTML with readable headers."""
if df.empty:
return ui.p("No data available for the selected filters.")
df_display = df.rename(
columns={c: readable_column_name(c, metrics) for c in df.columns},
)
float_cols = [
c
for c in df_display.columns
if c != "Year" and pd.api.types.is_float_dtype(df_display[c])
]
gt = (
GT(df_display)
.opt_row_striping()
.tab_options(table_font_names=["Nunito Sans", "Arial", "sans-serif"])
.opt_stylize(style=2, color="blue")
)
if float_cols:
gt = gt.fmt_number(columns=float_cols, decimals=2)
return ui.HTML(gt.as_raw_html())
# ---------------------------------------------------
# Shared Download Helpers
# ---------------------------------------------------
def download_extension(fmt: str) -> str:
"""Map selected download format to its file extension."""
return {"csv": "csv", "parquet": "parquet", "excel": "xlsx"}.get(fmt, "csv")
def download_media_type(fmt: str) -> str:
"""Return browser media type for each supported download format."""
if fmt == "parquet":
return "application/octet-stream"
if fmt == "excel":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
return "text/csv"
def export_filtered_data(df, fmt: str) -> str | bytes:
"""Export a pandas DataFrame to csv/parquet/excel payload for Shiny download."""
if fmt == "parquet":
return df.to_parquet(index=False)
if fmt == "excel":
engine = None
if importlib.util.find_spec("openpyxl") is not None:
engine = "openpyxl"
elif importlib.util.find_spec("xlsxwriter") is not None:
engine = "xlsxwriter"
else:
raise RuntimeError("Excel export requires openpyxl or xlsxwriter.")
buffer = io.BytesIO()
df.to_excel(buffer, index=False, engine=engine)
return buffer.getvalue()
return df.to_csv(index=False)
|