mindnet/app/frontend/ui_graph_cytoscape.py

254 lines
11 KiB
Python
Raw Normal View History

2025-12-14 14:57:34 +01:00
import streamlit as st
2025-12-14 15:05:24 +01:00
from st_cytoscape import cytoscape
2025-12-14 14:57:34 +01:00
from qdrant_client import models
from ui_config import COLLECTION_PREFIX, GRAPH_COLORS
from ui_callbacks import switch_to_editor_callback
def render_graph_explorer_cytoscape(graph_service):
st.header("🕸️ Graph Explorer (Cytoscape)")
2025-12-14 15:31:52 +01:00
# --- STATE INITIALISIERUNG ---
2025-12-14 14:57:34 +01:00
if "graph_center_id" not in st.session_state:
st.session_state.graph_center_id = None
2025-12-14 15:31:52 +01:00
2025-12-14 16:15:32 +01:00
# Getrennter State für Inspektion (Gelber Rahmen) vs. Navigation (Roter Rahmen)
2025-12-14 15:31:52 +01:00
if "graph_inspected_id" not in st.session_state:
st.session_state.graph_inspected_id = None
2025-12-14 14:57:34 +01:00
2025-12-14 15:31:52 +01:00
# Defaults für Layout
2025-12-14 15:16:01 +01:00
st.session_state.setdefault("cy_node_repulsion", 1000000)
st.session_state.setdefault("cy_ideal_edge_len", 150)
st.session_state.setdefault("cy_depth", 2)
2025-12-14 14:57:34 +01:00
col_ctrl, col_graph = st.columns([1, 4])
2025-12-14 16:12:19 +01:00
# --- LINKES PANEL ---
2025-12-14 14:57:34 +01:00
with col_ctrl:
st.subheader("Fokus")
search_term = st.text_input("Suche Notiz", placeholder="Titel eingeben...", key="cy_search")
if search_term:
hits, _ = graph_service.client.scroll(
collection_name=f"{COLLECTION_PREFIX}_notes",
limit=10,
scroll_filter=models.Filter(must=[models.FieldCondition(key="title", match=models.MatchText(text=search_term))])
)
options = {h.payload['title']: h.payload['note_id'] for h in hits}
if options:
selected_title = st.selectbox("Ergebnisse:", list(options.keys()), key="cy_select")
if st.button("Laden", use_container_width=True, key="cy_load"):
2025-12-14 15:31:52 +01:00
new_id = options[selected_title]
st.session_state.graph_center_id = new_id
2025-12-14 15:35:39 +01:00
st.session_state.graph_inspected_id = new_id
2025-12-14 14:57:34 +01:00
st.rerun()
st.divider()
2025-12-14 15:16:01 +01:00
2025-12-14 15:31:52 +01:00
with st.expander("👁️ Layout Einstellungen", expanded=True):
2025-12-14 15:16:01 +01:00
st.session_state.cy_depth = st.slider("Tiefe (Tier)", 1, 3, st.session_state.cy_depth, key="cy_depth_slider")
st.markdown("**COSE Layout**")
st.session_state.cy_ideal_edge_len = st.slider("Kantenlänge", 50, 600, st.session_state.cy_ideal_edge_len, key="cy_len_slider")
st.session_state.cy_node_repulsion = st.slider("Knoten-Abstoßung", 100000, 5000000, st.session_state.cy_node_repulsion, step=100000, key="cy_rep_slider")
2025-12-14 14:57:34 +01:00
2025-12-14 15:31:52 +01:00
if st.button("Neu berechnen", key="cy_rerun"):
2025-12-14 14:57:34 +01:00
st.rerun()
st.divider()
st.caption("Legende")
for k, v in list(GRAPH_COLORS.items())[:8]:
st.markdown(f"<span style='color:{v}'>●</span> {k}", unsafe_allow_html=True)
2025-12-14 16:12:19 +01:00
# --- RECHTES PANEL ---
2025-12-14 14:57:34 +01:00
with col_graph:
center_id = st.session_state.graph_center_id
2025-12-14 16:12:19 +01:00
# Init Fallback
2025-12-14 15:31:52 +01:00
if not center_id and st.session_state.graph_inspected_id:
center_id = st.session_state.graph_inspected_id
st.session_state.graph_center_id = center_id
2025-12-14 14:57:34 +01:00
2025-12-14 15:31:52 +01:00
if center_id:
if not st.session_state.graph_inspected_id:
st.session_state.graph_inspected_id = center_id
inspected_id = st.session_state.graph_inspected_id
# --- DATEN LADEN ---
2025-12-14 15:16:01 +01:00
with st.spinner(f"Lade Graph (Tiefe {st.session_state.cy_depth})..."):
nodes_data, edges_data = graph_service.get_ego_graph(
center_id,
depth=st.session_state.cy_depth
)
2025-12-14 16:15:32 +01:00
# Daten für den INSPECTOR laden
2025-12-14 15:31:52 +01:00
inspected_data = graph_service.get_note_with_full_content(inspected_id)
2025-12-14 14:57:34 +01:00
# --- ACTION BAR ---
2025-12-14 15:31:52 +01:00
action_container = st.container()
2025-12-14 14:57:34 +01:00
with action_container:
2025-12-14 15:31:52 +01:00
c1, c2, c3 = st.columns([2, 1, 1])
with c1:
2025-12-14 15:35:39 +01:00
title_show = inspected_data.get('title', inspected_id) if inspected_data else inspected_id
2025-12-14 16:12:19 +01:00
st.info(f"**Info:** {title_show}")
2025-12-14 15:31:52 +01:00
with c2:
2025-12-14 16:12:19 +01:00
# NAVIGATION
2025-12-14 15:31:52 +01:00
if inspected_id != center_id:
if st.button("🎯 Als Zentrum setzen", use_container_width=True, key="cy_nav_btn"):
st.session_state.graph_center_id = inspected_id
st.rerun()
else:
2025-12-14 16:12:19 +01:00
st.caption("_(Zentrum)_")
2025-12-14 15:31:52 +01:00
with c3:
2025-12-14 15:35:39 +01:00
# EDITIEREN
2025-12-14 15:31:52 +01:00
if inspected_data:
st.button("📝 Bearbeiten",
use_container_width=True,
on_click=switch_to_editor_callback,
args=(inspected_data,),
key="cy_edit_btn")
2025-12-14 16:15:32 +01:00
# --- DATA INSPECTOR (Eingeklappt) ---
2025-12-14 16:12:19 +01:00
with st.expander("🕵️ Data Inspector", expanded=False):
2025-12-14 15:31:52 +01:00
if inspected_data:
col_i1, col_i2 = st.columns(2)
with col_i1:
2025-12-14 15:31:52 +01:00
st.markdown(f"**ID:** `{inspected_data.get('note_id')}`")
st.markdown(f"**Typ:** `{inspected_data.get('type')}`")
with col_i2:
2025-12-14 15:31:52 +01:00
st.markdown(f"**Tags:** {', '.join(inspected_data.get('tags', []))}")
2025-12-14 16:12:19 +01:00
path_check = "✅" if inspected_data.get('path') else "❌"
st.markdown(f"**Pfad:** {path_check}")
st.text_area("Inhalt", inspected_data.get('fulltext', '')[:1000], height=200, disabled=True)
else:
2025-12-14 16:12:19 +01:00
st.warning("Keine Daten geladen.")
2025-12-14 16:12:19 +01:00
# --- GRAPH ELEMENTS ---
2025-12-14 14:57:34 +01:00
cy_elements = []
for n in nodes_data:
2025-12-14 15:31:52 +01:00
is_center = (n.id == center_id)
2025-12-14 16:15:32 +01:00
# Selektion wird visuell über Stylesheet gesteuert
2025-12-14 15:31:52 +01:00
is_inspected = (n.id == inspected_id)
2025-12-14 15:16:01 +01:00
2025-12-14 15:31:52 +01:00
tooltip_text = n.title if n.title else n.label
2025-12-14 15:16:01 +01:00
display_label = n.label
if len(display_label) > 15 and " " in display_label:
display_label = display_label.replace(" ", "\n", 1)
2025-12-14 15:05:24 +01:00
2025-12-14 14:57:34 +01:00
cy_node = {
"data": {
"id": n.id,
2025-12-14 15:16:01 +01:00
"label": display_label,
2025-12-14 15:35:39 +01:00
"bg_color": n.color,
"tooltip": tooltip_text
2025-12-14 14:57:34 +01:00
},
2025-12-14 15:31:52 +01:00
"selected": is_inspected,
"classes": "center" if is_center else ""
2025-12-14 14:57:34 +01:00
}
cy_elements.append(cy_node)
for e in edges_data:
2025-12-14 15:01:58 +01:00
target_id = getattr(e, "to", getattr(e, "target", None))
2025-12-14 15:05:24 +01:00
if target_id:
cy_edge = {
"data": {
2025-12-14 15:35:39 +01:00
"source": e.source,
"target": target_id,
"label": e.label,
"line_color": e.color
2025-12-14 15:05:24 +01:00
}
2025-12-14 14:57:34 +01:00
}
2025-12-14 15:05:24 +01:00
cy_elements.append(cy_edge)
2025-12-14 14:57:34 +01:00
2025-12-14 16:12:19 +01:00
# --- STYLESHEET ---
2025-12-14 14:57:34 +01:00
stylesheet = [
{
"selector": "node",
"style": {
"label": "data(label)",
2025-12-14 15:31:52 +01:00
"width": "30px", "height": "30px",
2025-12-14 15:35:39 +01:00
"background-color": "data(bg_color)",
2025-12-14 15:31:52 +01:00
"color": "#333", "font-size": "12px",
"text-valign": "center", "text-halign": "center",
"text-wrap": "wrap", "text-max-width": "90px",
"border-width": 2, "border-color": "#fff",
"title": "data(tooltip)"
2025-12-14 14:57:34 +01:00
}
},
{
"selector": "node:selected",
2025-12-14 15:31:52 +01:00
"style": {
"border-width": 6,
2025-12-14 16:12:19 +01:00
"border-color": "#FFC300",
2025-12-14 15:31:52 +01:00
"width": "50px", "height": "50px",
2025-12-14 16:12:19 +01:00
"font-weight": "bold"
2025-12-14 15:31:52 +01:00
}
},
{
"selector": ".center",
2025-12-14 14:57:34 +01:00
"style": {
2025-12-14 15:16:01 +01:00
"border-width": 4,
2025-12-14 16:12:19 +01:00
"border-color": "#FF5733",
2025-12-14 15:31:52 +01:00
"width": "40px", "height": "40px"
2025-12-14 14:57:34 +01:00
}
},
{
"selector": "edge",
"style": {
2025-12-14 15:16:01 +01:00
"width": 2,
"line-color": "data(line_color)",
"target-arrow-color": "data(line_color)",
2025-12-14 14:57:34 +01:00
"target-arrow-shape": "triangle",
"curve-style": "bezier",
"label": "data(label)",
2025-12-14 15:31:52 +01:00
"font-size": "10px", "color": "#666",
"text-background-opacity": 0.8, "text-background-color": "#fff"
2025-12-14 14:57:34 +01:00
}
}
]
2025-12-14 16:12:19 +01:00
# --- RENDER ---
graph_key = f"cy_{center_id}_{st.session_state.cy_depth}_{st.session_state.cy_ideal_edge_len}"
clicked_elements = cytoscape(
2025-12-14 15:16:01 +01:00
elements=cy_elements,
stylesheet=stylesheet,
2025-12-14 16:15:32 +01:00
# Layout Config (config Argument entfernt!)
2025-12-14 15:16:01 +01:00
layout={
"name": "cose",
"idealEdgeLength": st.session_state.cy_ideal_edge_len,
2025-12-14 16:12:19 +01:00
"nodeOverlap": 20,
"refresh": 20,
"fit": True,
"padding": 50,
2025-12-14 16:15:32 +01:00
"randomize": False,
2025-12-14 16:12:19 +01:00
"componentSpacing": 100,
2025-12-14 15:16:01 +01:00
"nodeRepulsion": st.session_state.cy_node_repulsion,
2025-12-14 16:12:19 +01:00
"edgeElasticity": 100,
"nestingFactor": 5,
"gravity": 80,
"numIter": 1000,
"initialTemp": 200,
"coolingFactor": 0.95,
"minTemp": 1.0,
2025-12-14 16:15:32 +01:00
"animate": False
2025-12-14 15:16:01 +01:00
},
key=graph_key,
2025-12-14 15:31:52 +01:00
height="700px"
2025-12-14 15:16:01 +01:00
)
2025-12-14 14:57:34 +01:00
2025-12-14 16:12:19 +01:00
# --- INTERACTION ---
if clicked_elements:
2025-12-14 15:31:52 +01:00
clicked_nodes = clicked_elements.get("nodes", [])
if clicked_nodes:
clicked_id = clicked_nodes[0]
2025-12-14 16:12:19 +01:00
# Wenn neuer Knoten geklickt wurde -> Inspektion ändern
2025-12-14 15:31:52 +01:00
if clicked_id != st.session_state.graph_inspected_id:
st.session_state.graph_inspected_id = clicked_id
st.rerun()
2025-12-14 14:57:34 +01:00
else:
st.info("👈 Bitte wähle links eine Notiz aus.")