File size: 8,421 Bytes
b555842 | 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 | """
theme.py — A-LEMS global theme injection
Drop this in gui/ and call inject_theme() at the top of every page.
Handles dark / light mode toggle stored in st.session_state["theme"].
"""
import streamlit as st
# ── Colour tokens ─────────────────────────────────────────────────────────────
DARK = {
"bg0": "#0d1117", # page background
"bg1": "#111827", # card background
"bg2": "#1f2937", # inset / secondary surface
"bg3": "#374151", # borders, tracks, empty bars
"t1": "#f1f5f9", # primary text
"t2": "#94a3b8", # secondary text / tabs / sidebar labels
"t3": "#475569", # muted / metric labels
"brd": "#1f2937", # card border
"brd2": "#374151", # input border / dividers
"accent": "#3b82f6",
}
LIGHT = {
"bg0": "#f3f4f6", # soft off-white page bg — less harsh than pure white
"bg1": "#ffffff", # cards pop off page bg with contrast
"bg2": "#f9fafb", # input / secondary surface
"bg3": "#d1d5db", # borders / tracks — visible but not harsh
"t1": "#1f2937", # main text — softer than pure black
"t2": "#4b5563", # secondary text / tabs / sidebar
"t3": "#6b7280", # muted labels — readable on bg1 and bg2
"brd": "#d1d5db", # card border — clearly visible
"brd2": "#9ca3af", # input border / subtle lines
"accent": "#3b82f6",
}
def _tokens(dark: bool) -> dict:
return DARK if dark else LIGHT
def inject_theme():
"""
Call once at the top of every page module.
Reads st.session_state["theme"] ("dark" | "light").
Injects global CSS overriding Streamlit's default colours.
"""
dark = st.session_state.get("theme", "dark") == "dark"
t = _tokens(dark)
st.markdown(f"""
<style>
/* ── Page & app background ──────────────────────────────────── */
.stApp, [data-testid="stAppViewContainer"] {{
background-color: {t["bg0"]} !important;
}}
[data-testid="stHeader"] {{
background-color: {t["bg0"]} !important;
border-bottom: 0.5px solid {t["brd"]};
}}
/* ── Sidebar ────────────────────────────────────────────────── */
[data-testid="stSidebar"], [data-testid="stSidebarContent"] {{
background-color: {t["bg1"]} !important;
border-right: 0.5px solid {t["brd"]};
}}
[data-testid="stSidebar"] * {{
color: {t["t2"]} !important;
}}
[data-testid="stSidebar"] h1,
[data-testid="stSidebar"] h2,
[data-testid="stSidebar"] h3 {{
color: {t["t1"]} !important;
}}
/* ── Generic text ────────────────────────────────────────────── */
.stMarkdown, .stText, p, span, div {{
color: {t["t1"]};
}}
/* ── Metric cards ────────────────────────────────────────────── */
[data-testid="stMetric"] {{
background: {t["bg1"]};
border: 0.5px solid {t["brd"]};
border-top: 3px solid {t["accent"]};
border-radius: 8px;
padding: 10px 14px;
}}
[data-testid="stMetricLabel"] {{ color: {t["t3"]} !important; font-size: 11px !important; }}
[data-testid="stMetricValue"] {{ color: {t["t1"]} !important; }}
[data-testid="stMetricDelta"] {{ font-size: 10px !important; }}
/* ── Expanders ───────────────────────────────────────────────── */
[data-testid="stExpander"] {{
background: {t["bg1"]};
border: 0.5px solid {t["brd"]};
border-radius: 8px;
}}
/* ── Tabs ────────────────────────────────────────────────────── */
[data-testid="stTabs"] [data-baseweb="tab-list"] {{
background: {t["bg1"]};
border-bottom: 0.5px solid {t["brd"]};
}}
[data-testid="stTabs"] [data-baseweb="tab"] {{
color: {t["t2"]} !important;
font-size: 11px;
}}
[data-testid="stTabs"] [aria-selected="true"] {{
color: {t["t1"]} !important;
border-bottom: 2px solid {t["accent"]} !important;
}}
/* ── Inputs & selects ────────────────────────────────────────── */
[data-testid="stTextInput"] input,
[data-testid="stSelectbox"] > div,
[data-testid="stNumberInput"] input {{
background: {t["bg2"]} !important;
color: {t["t1"]} !important;
border: 0.5px solid {t["brd2"]} !important;
border-radius: 6px;
}}
/* ── Buttons ─────────────────────────────────────────────────── */
[data-testid="stButton"] > button {{
background: {t["bg2"]};
color: {t["t1"]};
border: 0.5px solid {t["brd2"]};
border-radius: 6px;
font-size: 12px;
}}
[data-testid="stButton"] > button:hover {{
background: {t["bg3"]};
border-color: {t["accent"]};
}}
/* ── DataFrames / tables ─────────────────────────────────────── */
[data-testid="stDataFrame"] {{
background: {t["bg1"]};
border: 0.5px solid {t["brd"]};
border-radius: 8px;
}}
/* ── Plotly chart backgrounds ────────────────────────────────── */
.js-plotly-plot .plotly .bg {{
fill: {t["bg1"]} !important;
}}
/* ── Custom card utility class ───────────────────────────────── */
.alems-card {{
background: {t["bg1"]};
border: 0.5px solid {t["brd"]};
border-radius: 10px;
padding: 14px 16px;
margin-bottom: 10px;
}}
.alems-card-red {{ border-top: 3px solid #dc2626; }}
.alems-card-blue {{ border-top: 3px solid #3b82f6; }}
.alems-card-green {{ border-top: 3px solid #16a34a; }}
.alems-card-amber {{ border-top: 3px solid #d97706; }}
/* ── Scrollbar ───────────────────────────────────────────────── */
::-webkit-scrollbar {{ width: 5px; height: 5px; }}
::-webkit-scrollbar-track {{ background: {t["bg0"]}; }}
::-webkit-scrollbar-thumb {{ background: {t["bg3"]}; border-radius: 3px; }}
</style>
""", unsafe_allow_html=True)
def theme_toggle_button():
"""
Renders a small ☀/☾ toggle button in sidebar or top bar.
Call after inject_theme().
"""
dark = st.session_state.get("theme", "dark") == "dark"
label = "☀ Light mode" if dark else "☾ Dark mode"
if st.button(label, key="theme_toggle_btn"):
st.session_state["theme"] = "light" if dark else "dark"
st.rerun()
def plotly_layout(dark: bool = None) -> dict:
"""
Returns a Plotly layout dict matching the current theme.
Merge with fig.update_layout(**plotly_layout()).
"""
if dark is None:
dark = st.session_state.get("theme", "dark") == "dark"
t = _tokens(dark)
return dict(
paper_bgcolor = t["bg1"],
plot_bgcolor = t["bg2"],
font = dict(color=t["t1"], size=10),
xaxis = dict(gridcolor=t["brd2"], zerolinecolor=t["brd2"],
tickfont=dict(color=t["t3"])),
yaxis = dict(gridcolor=t["brd2"], zerolinecolor=t["brd2"],
tickfont=dict(color=t["t3"])),
legend = dict(bgcolor=t["bg1"], bordercolor=t["brd"],
borderwidth=0.5, font=dict(color=t["t2"])),
margin = dict(t=40, b=40, l=50, r=20),
)
|