This commit is contained in:
2026-05-12 15:41:55 +02:00
parent b3ba440269
commit 2054432757
12 changed files with 930 additions and 132 deletions
+117
View File
@@ -0,0 +1,117 @@
# PanoramaContacts — CLAUDE.md
## Účel adresáře
Import kontaktů středisek (site contacts) z exportů systému PANORAMA (CTMS) do MySQL a jejich zobrazení ve Streamlit web reportu.
Filtruje pouze záznamy pro **Czechia**. Aktuálně pokryté protokoly:
| Protocol ID | TA |
|---|---|
| `77242113UCO3001` | Immunology |
| `42847922MDD3003` | Neuroscience |
---
## Soubory
| Soubor | Účel |
|---|---|
| `import_CZ_contacts.py` | Import xlsx → MySQL |
| `webreport.py` | Streamlit web report |
| `run_webreport.py` | PyCharm launcher (`streamlit run webreport.py`) |
| `sql/create_CTMS_contacts.sql` | DDL tabulky `CTMS_contacts` |
| `SourceData/*.xlsx` | PANORAMA Dashboard exporty (zdrojová data) |
| `filter_state.json` | Automaticky ukládaný stav filtrů (generuje app) |
---
## MySQL
- **Host:** 192.168.1.76:3306 · **DB:** `studie` · **Tabulka:** `CTMS_contacts`
- **Sheet v xlsx:** `Site Contacts`, header na řádku 6 (0-based index 5)
### Klíčové sloupce tabulky
| Sloupec | Typ | Poznámka |
|---|---|---|
| `file_date` | DATE | Z `dcterms:created` v docProps/core.xml xlsx |
| `imported_at` | DATETIME | Auto timestamp importu |
| `protocol_id` | VARCHAR(20) | Identifikátor studie |
| `site_id` | VARCHAR(15) | Středisko (např. `DD5-CZ10006`) |
| `contact_role` | VARCHAR(50) | Role kontaktu (PI, Study Coordinator, …) |
| `contact_start_date` | DATE | Začátek platnosti kontaktu |
| `contact_end_date` | DATE | Konec platnosti — NULL = stále aktivní |
| `email` | VARCHAR(100) | Hlavní e-mail |
---
## import_CZ_contacts.py
- Zpracuje všechny `*.xlsx` v `SourceData/`
- Přeskočí soubory, jejichž `file_date` ≠ dnešní datum (UTC)
- Přepis: DELETE + INSERT podle `(file_date, protocol_id, country_name)`
- `clean_value()` převede NaN / NaT / Timestamp na typy přijatelné MySQL driverem
---
## webreport.py — Streamlit app
### Filtry (sidebar)
| Filtr | Widget | Logika options |
|---|---|---|
| **Střediska** | radio | Aktivní / Neaktivní / Všechna |
| **Protokol** | selectbox | Z celé DB |
| **Role** | multiselect | Filtrováno dle protokolu + aktivní/neaktivní |
| **Site** | multiselect | Filtrováno dle protokolu + aktivní/neaktivní |
| **Hledání** | text_input | Fulltext přes všechny sloupce řádku |
### Logika filtru Střediska
| Hodnota | Site podmínka | End Date podmínka |
|---|---|---|
| **Aktivní** | `site_id` v `ACTIVE_SITES` | `contact_end_date IS NULL` |
| **Neaktivní** | `site_id` NOT v `ACTIVE_SITES` | bez omezení |
| **Všechna** | bez omezení | bez omezení |
### Aktivní střediska (ACTIVE_SITES)
```python
"77242113UCO3001": {
"DD5-CZ10001", "DD5-CZ10003", "DD5-CZ10006", "DD5-CZ10009",
"DD5-CZ10010", "DD5-CZ10012", "DD5-CZ10013", "DD5-CZ10015",
"DD5-CZ10016", "DD5-CZ10020", "DD5-CZ10021", "DD5-CZ10022",
}
"42847922MDD3003": {
"S10-CZ10004", "S10-CZ10008", "S10-CZ10011", "S10-CZ10012",
}
```
### Perzistence filtrů
- Stav se ukládá do `filter_state.json` při každé změně filtru (`on_change=save_filter_state`)
- Načítá se jednou za session přes flag `filters_initialized` v `st.session_state`
- Při načítání se hodnoty validují vůči aktuálním options (ochrana před zastaralými daty)
### Clipboard tlačítko
- Knihovna `pyperclip` — kopíruje přímo do Windows clipboardu ze serverové strany
- Formát: `Jméno Příjmení <email@domain.cz>; …`
- Reaguje na aktuálně zobrazené (filtrované) záznamy
### Cache
- `@st.cache_data(ttl=300)` — data se drží 5 minut
- Tlačítko 🔄 Obnovit data volá `st.cache_data.clear()` + `st.rerun()`
---
## Závislosti (venv)
```
mysql-connector-python
pandas
openpyxl
streamlit
pyperclip
```
-132
View File
@@ -1,132 +0,0 @@
"""
create_report.py
Streamlit report kontaktů z MySQL tabulky CTMS_contacts.
Spuštění: streamlit run create_report.py
"""
from datetime import date
import mysql.connector
import pandas as pd
import streamlit as st
# ── Konfigurace ────────────────────────────────────────────────────────────────
DB_CONFIG = {
"host": "192.168.1.76",
"port": 3306,
"user": "root",
"password": "Vlado9674+",
"database": "studie",
"charset": "utf8mb4",
}
TABLE = "CTMS_contacts"
DISPLAY_COLS = {
"site_id": "Site ID",
"institution_name": "Institution",
"pi_full_name": "PI",
"contact_title": "Title",
"last_name": "Last Name",
"first_name": "First Name",
"contact_role": "Role",
"primary_indicator": "Primary",
"phone": "Phone",
"phone_mobile": "Mobile",
"email": "Email",
"contact_start_date": "Start Date",
"contact_end_date": "End Date",
}
# ── Data ───────────────────────────────────────────────────────────────────────
@st.cache_data(ttl=300)
def load_data() -> pd.DataFrame:
cols = ", ".join(DISPLAY_COLS.keys())
sql = (
f"SELECT protocol_id, file_date, {cols} "
f"FROM {TABLE} "
f"ORDER BY protocol_id, site_id, contact_role, last_name, first_name"
)
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor(dictionary=True)
cursor.execute(sql)
rows = cursor.fetchall()
cursor.close()
conn.close()
return pd.DataFrame(rows)
# ── Aplikace ───────────────────────────────────────────────────────────────────
st.set_page_config(page_title="CTMS Contacts", page_icon="🏥", layout="wide")
st.title("🏥 CTMS Contacts — Czechia")
try:
df = load_data()
except Exception as e:
st.error(f"Chyba připojení k MySQL: {e}")
st.stop()
# ── Sidebar filtry ─────────────────────────────────────────────────────────────
with st.sidebar:
st.header("Filtry")
protocols = ["Všechny"] + sorted(df["protocol_id"].unique().tolist())
sel_proto = st.selectbox("Protokol", protocols)
roles = ["Všechny"] + sorted(df["contact_role"].dropna().unique().tolist())
sel_role = st.selectbox("Role", roles)
sites = ["Všechny"] + sorted(df["site_id"].dropna().unique().tolist())
sel_site = st.selectbox("Site", sites)
search = st.text_input("Hledat (jméno, email…)")
st.divider()
if st.button("🔄 Obnovit data"):
st.cache_data.clear()
st.rerun()
st.caption(f"Naposledy načteno: {pd.Timestamp.now().strftime('%H:%M:%S')}")
# ── Filtrování ─────────────────────────────────────────────────────────────────
filtered = df.copy()
if sel_proto != "Všechny":
filtered = filtered[filtered["protocol_id"] == sel_proto]
if sel_role != "Všechny":
filtered = filtered[filtered["contact_role"] == sel_role]
if sel_site != "Všechny":
filtered = filtered[filtered["site_id"] == sel_site]
if search:
mask = filtered.apply(
lambda row: row.astype(str).str.contains(search, case=False, na=False).any(),
axis=1,
)
filtered = filtered[mask]
# ── Metriky ────────────────────────────────────────────────────────────────────
col1, col2, col3, col4 = st.columns(4)
col1.metric("Kontaktů celkem", len(filtered))
col2.metric("Protokolů", filtered["protocol_id"].nunique())
col3.metric("Středisek", filtered["site_id"].nunique())
col4.metric("Rolí", filtered["contact_role"].nunique())
st.divider()
# ── Tabulka ────────────────────────────────────────────────────────────────────
display = filtered[["protocol_id", "file_date"] + list(DISPLAY_COLS.keys())].copy()
display = display.rename(columns={"protocol_id": "Protocol", "file_date": "File Date", **DISPLAY_COLS})
st.dataframe(
display,
width="stretch",
hide_index=True,
column_config={
"Email": st.column_config.LinkColumn("Email", display_text=".*"),
"Start Date": st.column_config.DateColumn("Start Date", format="DD-MMM-YYYY"),
"End Date": st.column_config.DateColumn("End Date", format="DD-MMM-YYYY"),
},
)
st.caption(f"Zobrazeno {len(filtered)} z {len(df)} záznamů")
+22
View File
@@ -0,0 +1,22 @@
{
"sel_status": "Všechna",
"sel_proto": "77242113UCO3001",
"sel_role": [
"Principal Investigator",
"Sub-Investigator",
"Study Coordinator"
],
"sel_site": [
"DD5-CZ10001",
"DD5-CZ10003",
"DD5-CZ10006",
"DD5-CZ10009",
"DD5-CZ10010",
"DD5-CZ10012",
"DD5-CZ10013",
"DD5-CZ10015",
"DD5-CZ10016",
"DD5-CZ10020",
"DD5-CZ10021"
]
}
+6
View File
@@ -0,0 +1,6 @@
import subprocess
import sys
from pathlib import Path
app = Path(__file__).parent / "webreport.py"
subprocess.run([sys.executable, "-m", "streamlit", "run", str(app)])
+223
View File
@@ -0,0 +1,223 @@
"""
create_report.py
Streamlit report kontaktů z MySQL tabulky CTMS_contacts.
Spuštění: streamlit run create_report.py
"""
import json
from pathlib import Path
import mysql.connector
import pandas as pd
import pyperclip
import streamlit as st
# ── Konfigurace ────────────────────────────────────────────────────────────────
DB_CONFIG = {
"host": "192.168.1.76",
"port": 3306,
"user": "root",
"password": "Vlado9674+",
"database": "studie",
"charset": "utf8mb4",
}
TABLE = "CTMS_contacts"
STATE_FILE = Path(__file__).parent / "filter_state.json"
ACTIVE_SITES = {
"77242113UCO3001": {
"DD5-CZ10001", "DD5-CZ10003", "DD5-CZ10006", "DD5-CZ10009",
"DD5-CZ10010", "DD5-CZ10012", "DD5-CZ10013", "DD5-CZ10015",
"DD5-CZ10016", "DD5-CZ10020", "DD5-CZ10021", "DD5-CZ10022",
},
"42847922MDD3003": {
"S10-CZ10004", "S10-CZ10008", "S10-CZ10011", "S10-CZ10012",
},
}
DISPLAY_COLS = {
"site_id": "Site ID",
"institution_name": "Institution",
"pi_full_name": "PI",
"contact_title": "Title",
"last_name": "Last Name",
"first_name": "First Name",
"contact_role": "Role",
"primary_indicator": "Primary",
"phone": "Phone",
"phone_mobile": "Mobile",
"email": "Email",
"contact_start_date": "Start Date",
"contact_end_date": "End Date",
}
STATUS_OPTIONS = ["Aktivní", "Neaktivní", "Všechna"]
DEFAULT_STATUS = "Aktivní"
# ── Perzistence filtrů ─────────────────────────────────────────────────────────
def load_filter_state() -> dict:
if STATE_FILE.exists():
try:
return json.loads(STATE_FILE.read_text(encoding="utf-8"))
except Exception:
pass
return {}
def save_filter_state():
state = {
"sel_status": st.session_state.get("sel_status", DEFAULT_STATUS),
"sel_proto": st.session_state.get("sel_proto", "Všechny"),
"sel_role": st.session_state.get("sel_role", []),
"sel_site": st.session_state.get("sel_site", []),
}
STATE_FILE.write_text(json.dumps(state, ensure_ascii=False, indent=2), encoding="utf-8")
# ── Data ───────────────────────────────────────────────────────────────────────
@st.cache_data(ttl=300)
def load_data() -> pd.DataFrame:
cols = ", ".join(DISPLAY_COLS.keys())
sql = (
f"SELECT protocol_id, file_date, {cols} "
f"FROM {TABLE} "
f"ORDER BY protocol_id, site_id, contact_role, last_name, first_name"
)
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor(dictionary=True)
cursor.execute(sql)
rows = cursor.fetchall()
cursor.close()
conn.close()
return pd.DataFrame(rows)
# ── Aplikace ───────────────────────────────────────────────────────────────────
st.set_page_config(page_title="CTMS Contacts", page_icon="🏥", layout="wide")
st.title("🏥 CTMS Contacts — Czechia")
try:
df = load_data()
except Exception as e:
st.error(f"Chyba připojení k MySQL: {e}")
st.stop()
protocols = ["Všechny"] + sorted(df["protocol_id"].unique().tolist())
# Načti uložený stav jednou za session
if "filters_initialized" not in st.session_state:
saved = load_filter_state()
st.session_state["sel_status"] = saved.get("sel_status", DEFAULT_STATUS) if saved.get("sel_status") in STATUS_OPTIONS else DEFAULT_STATUS
st.session_state["sel_proto"] = saved.get("sel_proto", "Všechny") if saved.get("sel_proto") in protocols else "Všechny"
st.session_state["sel_role"] = saved.get("sel_role", [])
st.session_state["sel_site"] = saved.get("sel_site", [])
st.session_state["filters_initialized"] = True
# Role a centra podle vybraného protokolu + aktivní/neaktivní
all_active = set().union(*ACTIVE_SITES.values())
df_opts = df.copy()
if st.session_state["sel_proto"] != "Všechny":
df_opts = df_opts[df_opts["protocol_id"] == st.session_state["sel_proto"]]
if st.session_state["sel_status"] == "Aktivní":
df_opts = df_opts[df_opts["site_id"].isin(all_active) & df_opts["contact_end_date"].isna()]
elif st.session_state["sel_status"] == "Neaktivní":
df_opts = df_opts[~df_opts["site_id"].isin(all_active)]
roles = sorted(df_opts["contact_role"].dropna().unique().tolist())
sites = sorted(df_opts["site_id"].dropna().unique().tolist())
# Pročisti neplatné výběry po změně protokolu
st.session_state["sel_role"] = [r for r in st.session_state["sel_role"] if r in roles]
st.session_state["sel_site"] = [s for s in st.session_state["sel_site"] if s in sites]
# ── Sidebar filtry ─────────────────────────────────────────────────────────────
with st.sidebar:
st.header("Filtry")
st.radio(
"Střediska", STATUS_OPTIONS, horizontal=True,
key="sel_status", on_change=save_filter_state,
)
st.selectbox(
"Protokol", protocols,
key="sel_proto", on_change=save_filter_state,
)
st.multiselect(
"Role", roles,
key="sel_role", on_change=save_filter_state,
)
st.multiselect(
"Site", sites,
key="sel_site", on_change=save_filter_state,
)
search = st.text_input("Hledat (jméno, email…)")
st.divider()
if st.button("🔄 Obnovit data"):
st.cache_data.clear()
st.rerun()
st.caption(f"Naposledy načteno: {pd.Timestamp.now().strftime('%H:%M:%S')}")
# ── Filtrování ─────────────────────────────────────────────────────────────────
filtered = df.copy()
if st.session_state["sel_proto"] != "Všechny":
filtered = filtered[filtered["protocol_id"] == st.session_state["sel_proto"]]
if st.session_state["sel_status"] == "Aktivní":
filtered = filtered[filtered["site_id"].isin(all_active) & filtered["contact_end_date"].isna()]
elif st.session_state["sel_status"] == "Neaktivní":
filtered = filtered[~filtered["site_id"].isin(all_active)]
if st.session_state["sel_role"]:
filtered = filtered[filtered["contact_role"].isin(st.session_state["sel_role"])]
if st.session_state["sel_site"]:
filtered = filtered[filtered["site_id"].isin(st.session_state["sel_site"])]
if search:
mask = filtered.apply(
lambda row: row.astype(str).str.contains(search, case=False, na=False).any(),
axis=1,
)
filtered = filtered[mask]
# ── Metriky ────────────────────────────────────────────────────────────────────
col1, col2, col3, col4 = st.columns(4)
col1.metric("Kontaktů celkem", len(filtered))
col2.metric("Protokolů", filtered["protocol_id"].nunique())
col3.metric("Středisek", filtered["site_id"].nunique())
col4.metric("Rolí", filtered["contact_role"].nunique())
st.divider()
# ── Tabulka ────────────────────────────────────────────────────────────────────
display = filtered[["protocol_id", "file_date"] + list(DISPLAY_COLS.keys())].copy()
display = display.rename(columns={"protocol_id": "Protocol", "file_date": "File Date", **DISPLAY_COLS})
st.dataframe(
display,
width="stretch",
hide_index=True,
column_config={
"Email": st.column_config.LinkColumn("Email", display_text=".*"),
"Start Date": st.column_config.DateColumn("Start Date", format="DD-MMM-YYYY"),
"End Date": st.column_config.DateColumn("End Date", format="DD-MMM-YYYY"),
},
)
st.caption(f"Zobrazeno {len(filtered)} z {len(df)} záznamů")
st.divider()
email_rows = filtered[["first_name", "last_name", "email"]].dropna(subset=["email"])
email_rows = email_rows[email_rows["email"].str.strip() != ""]
entries = [
f"{row.first_name} {row.last_name} <{row.email}>"
for row in email_rows.itertuples()
]
email_str = "; ".join(entries)
if st.button(f"📋 Kopírovat emaily do clipboardu ({len(entries)} adres)"):
if entries:
pyperclip.copy(email_str)
st.success(f"✅ Zkopírováno {len(entries)} adres — vlož přímo do pole Komu.")