Update utils_layout.py
Browse files- utils_layout.py +103 -35
utils_layout.py
CHANGED
|
@@ -1,79 +1,147 @@
|
|
| 1 |
-
#
|
|
|
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
-
def _resolve_logo_path():
|
| 8 |
"""
|
| 9 |
Resolve automaticamente o caminho da logo:
|
| 10 |
-
1. Variável de ambiente LOGO_PATH (opcional, via Secrets)
|
| 11 |
-
2. logo.png na raiz
|
| 12 |
-
3. assets/logo.png
|
| 13 |
-
4. images/logo.png
|
| 14 |
-
5. static/logo.png
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""
|
|
|
|
| 18 |
candidates = []
|
| 19 |
|
| 20 |
-
# 1)
|
| 21 |
env_path = os.getenv("LOGO_PATH")
|
| 22 |
if env_path:
|
| 23 |
candidates.append(env_path)
|
| 24 |
|
| 25 |
-
# 2)
|
| 26 |
candidates += [
|
| 27 |
"logo.png",
|
| 28 |
os.path.join("assets", "logo.png"),
|
| 29 |
os.path.join("images", "logo.png"),
|
| 30 |
os.path.join("static", "logo.png"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
]
|
| 32 |
|
| 33 |
-
base
|
|
|
|
| 34 |
|
| 35 |
for p in candidates:
|
|
|
|
|
|
|
| 36 |
full = p if os.path.isabs(p) else os.path.join(base, p)
|
| 37 |
if os.path.exists(full):
|
|
|
|
| 38 |
return full
|
| 39 |
|
|
|
|
| 40 |
return None
|
| 41 |
|
| 42 |
|
| 43 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
"""
|
| 45 |
-
Exibe a logo
|
| 46 |
-
|
| 47 |
"""
|
| 48 |
logo_path = _resolve_logo_path()
|
| 49 |
|
| 50 |
if not logo_path:
|
| 51 |
-
# Fallback
|
| 52 |
if top:
|
| 53 |
st.markdown("### IOI‑RUN")
|
| 54 |
if sidebar:
|
| 55 |
st.sidebar.markdown("### IOI‑RUN")
|
| 56 |
return
|
| 57 |
|
| 58 |
-
|
| 59 |
-
logo = Image.open(logo_path)
|
| 60 |
-
except Exception as e:
|
| 61 |
-
if top:
|
| 62 |
-
st.markdown(f"### IOI‑RUN (logo indisponível: {e})")
|
| 63 |
-
if sidebar:
|
| 64 |
-
st.sidebar.markdown("### IOI‑RUN")
|
| 65 |
-
return
|
| 66 |
-
|
| 67 |
-
# -----------------------
|
| 68 |
-
# LOGO NO TOPO (CENTRADA)
|
| 69 |
-
# -----------------------
|
| 70 |
if top:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
# LOGO NO SIDEBAR
|
| 77 |
-
# -----------------------
|
| 78 |
if sidebar:
|
| 79 |
-
|
|
|
|
| 1 |
+
# utils.py
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
import os
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
import streamlit as st
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
from PIL import Image
|
| 10 |
+
_HAS_PIL = True
|
| 11 |
+
except Exception:
|
| 12 |
+
_HAS_PIL = False
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _debug(msg: str):
|
| 16 |
+
"""Mostra diagnóstico leve quando LOGO_DEBUG=1 (Settings → Secrets)."""
|
| 17 |
+
if os.getenv("LOGO_DEBUG", "0") == "1":
|
| 18 |
+
st.caption(f"🧪 LOGO_DEBUG: {msg}")
|
| 19 |
|
| 20 |
|
| 21 |
+
def _resolve_logo_path() -> Optional[str]:
|
| 22 |
"""
|
| 23 |
Resolve automaticamente o caminho da logo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
1) LOGO_PATH (Secrets/ENV) — aceita relativo ou absoluto
|
| 26 |
+
2) logo.(png|svg|jpg|jpeg) na raiz
|
| 27 |
+
3) assets/logo.(png|svg|jpg|jpeg)
|
| 28 |
+
4) images/logo.(png|svg|jpg|jpeg)
|
| 29 |
+
5) static/logo.(png|svg|jpg|jpeg)
|
| 30 |
+
|
| 31 |
+
Retorna caminho **absoluto** se existir, senão None.
|
| 32 |
"""
|
| 33 |
+
base = os.path.abspath(os.getcwd())
|
| 34 |
candidates = []
|
| 35 |
|
| 36 |
+
# 1) via Secrets/ENV
|
| 37 |
env_path = os.getenv("LOGO_PATH")
|
| 38 |
if env_path:
|
| 39 |
candidates.append(env_path)
|
| 40 |
|
| 41 |
+
# 2–5) Padrões comuns (PNG → SVG → JPG/JPEG)
|
| 42 |
candidates += [
|
| 43 |
"logo.png",
|
| 44 |
os.path.join("assets", "logo.png"),
|
| 45 |
os.path.join("images", "logo.png"),
|
| 46 |
os.path.join("static", "logo.png"),
|
| 47 |
+
|
| 48 |
+
"logo.svg",
|
| 49 |
+
os.path.join("assets", "logo.svg"),
|
| 50 |
+
os.path.join("images", "logo.svg"),
|
| 51 |
+
os.path.join("static", "logo.svg"),
|
| 52 |
+
|
| 53 |
+
"logo.jpg", "logo.jpeg",
|
| 54 |
+
os.path.join("assets", "logo.jpg"),
|
| 55 |
+
os.path.join("assets", "logo.jpeg"),
|
| 56 |
+
os.path.join("images", "logo.jpg"),
|
| 57 |
+
os.path.join("images", "logo.jpeg"),
|
| 58 |
+
os.path.join("static", "logo.jpg"),
|
| 59 |
+
os.path.join("static", "logo.jpeg"),
|
| 60 |
]
|
| 61 |
|
| 62 |
+
_debug(f"base cwd: {base}")
|
| 63 |
+
_debug(f"candidatos: {candidates}")
|
| 64 |
|
| 65 |
for p in candidates:
|
| 66 |
+
if not p:
|
| 67 |
+
continue
|
| 68 |
full = p if os.path.isabs(p) else os.path.join(base, p)
|
| 69 |
if os.path.exists(full):
|
| 70 |
+
_debug(f"encontrado: {full}")
|
| 71 |
return full
|
| 72 |
|
| 73 |
+
_debug("nenhum candidato encontrado")
|
| 74 |
return None
|
| 75 |
|
| 76 |
|
| 77 |
+
def _render_image_any(path: str, target: str = "top", width: Optional[int] = None):
|
| 78 |
+
"""
|
| 79 |
+
Renderiza imagem:
|
| 80 |
+
- PNG/JPG/JPEG → PIL (se disponível) ou bytes
|
| 81 |
+
- SVG → bytes (Pillow não lê SVG)
|
| 82 |
+
"""
|
| 83 |
+
try:
|
| 84 |
+
_, ext = os.path.splitext(path.lower())
|
| 85 |
+
|
| 86 |
+
if ext == ".svg":
|
| 87 |
+
with open(path, "rb") as f:
|
| 88 |
+
data = f.read()
|
| 89 |
+
if target == "sidebar":
|
| 90 |
+
st.sidebar.image(data)
|
| 91 |
+
else:
|
| 92 |
+
st.image(data, width=width)
|
| 93 |
+
return
|
| 94 |
+
|
| 95 |
+
if _HAS_PIL:
|
| 96 |
+
img = Image.open(path)
|
| 97 |
+
if target == "sidebar":
|
| 98 |
+
st.sidebar.image(img, use_container_width=True)
|
| 99 |
+
else:
|
| 100 |
+
st.image(img, use_container_width=(width is None), width=width)
|
| 101 |
+
else:
|
| 102 |
+
with open(path, "rb") as f:
|
| 103 |
+
data = f.read()
|
| 104 |
+
if target == "sidebar":
|
| 105 |
+
st.sidebar.image(data, use_container_width=True)
|
| 106 |
+
else:
|
| 107 |
+
st.image(data, use_container_width=(width is None), width=width)
|
| 108 |
+
|
| 109 |
+
except Exception as e:
|
| 110 |
+
# Fallback amigável (não quebra a página)
|
| 111 |
+
if target == "sidebar":
|
| 112 |
+
st.sidebar.markdown(f"**IOI‑RUN** _(logo indisponível: {e})_")
|
| 113 |
+
else:
|
| 114 |
+
st.markdown(f"### IOI‑RUN _(logo indisponível: {e})_")
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def exibir_logo(top: bool = True, sidebar: bool = True, top_width: Optional[int] = None):
|
| 118 |
"""
|
| 119 |
+
Exibe a logo no topo e/ou na sidebar.
|
| 120 |
+
- top_width: largura opcional (px) para a imagem do topo.
|
| 121 |
"""
|
| 122 |
logo_path = _resolve_logo_path()
|
| 123 |
|
| 124 |
if not logo_path:
|
| 125 |
+
# Fallback textual — evita exceções se o arquivo não existir
|
| 126 |
if top:
|
| 127 |
st.markdown("### IOI‑RUN")
|
| 128 |
if sidebar:
|
| 129 |
st.sidebar.markdown("### IOI‑RUN")
|
| 130 |
return
|
| 131 |
|
| 132 |
+
# TOPO (centralizado)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
if top:
|
| 134 |
+
try:
|
| 135 |
+
col1, col2, col3 = st.columns([2, 3, 2])
|
| 136 |
+
except Exception:
|
| 137 |
+
col1 = col2 = col3 = None
|
| 138 |
+
|
| 139 |
+
if col2:
|
| 140 |
+
with col2:
|
| 141 |
+
_render_image_any(logo_path, target="top", width=top_width)
|
| 142 |
+
else:
|
| 143 |
+
_render_image_any(logo_path, target="top", width=top_width)
|
| 144 |
|
| 145 |
+
# SIDEBAR
|
|
|
|
|
|
|
| 146 |
if sidebar:
|
| 147 |
+
_render_image_any(logo_path, target="sidebar")
|