sayou-visualizer 0.0.8__tar.gz → 0.0.10__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (19) hide show
  1. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/.gitignore +10 -0
  2. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/PKG-INFO +1 -1
  3. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/pyproject.toml +1 -1
  4. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/src/sayou/visualizer/pipeline.py +39 -8
  5. sayou_visualizer-0.0.10/src/sayou/visualizer/renderer/analytic_kg_renderer.py +453 -0
  6. sayou_visualizer-0.0.10/src/sayou/visualizer/renderer/showcase_kg_renderer.py +303 -0
  7. sayou_visualizer-0.0.10/src/sayou/visualizer/tracer/graph_tracer.py +101 -0
  8. sayou_visualizer-0.0.8/src/sayou/visualizer/tracer/graph_tracer.py +0 -94
  9. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/README.md +0 -0
  10. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/examples/quick_start.py +0 -0
  11. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/examples/quick_start_ws_client.py +0 -0
  12. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/examples/quick_start_ws_server.py +0 -0
  13. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/src/sayou/visualizer/__init__.py +0 -0
  14. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/src/sayou/visualizer/core/exceptions.py +0 -0
  15. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/src/sayou/visualizer/core/schemas.py +0 -0
  16. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/src/sayou/visualizer/interfaces/base_renderer.py +0 -0
  17. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/src/sayou/visualizer/renderer/pyvis_renderer.py +0 -0
  18. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/src/sayou/visualizer/tracer/rich_tracer.py +0 -0
  19. {sayou_visualizer-0.0.8 → sayou_visualizer-0.0.10}/src/sayou/visualizer/tracer/websocket_tracer.py +0 -0
@@ -3,6 +3,12 @@ __pycache__/
3
3
  *.py[codz]
4
4
  *$py.class
5
5
 
6
+ sayou-stock/tests/tests_fibo.py
7
+ sayou-stock/tests/tests_ontology.py
8
+ sayou-stock/tests/tests_rsi.py
9
+ sayou-stock/tests/tests_trading.py
10
+ sayou-stock/tests/tests_trading_pip.py
11
+ sayou-stock/src/sayou/stock/ontology/
6
12
  # C extensions
7
13
  *.so
8
14
 
@@ -205,3 +211,7 @@ cython_debug/
205
211
  marimo/_static/
206
212
  marimo/_lsp/
207
213
  __marimo__/
214
+
215
+ # MacOS
216
+ .DS_Store
217
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sayou-visualizer
3
- Version: 0.0.8
3
+ Version: 0.0.10
4
4
  Summary: Visualizer components for the Sayou Data Platform
5
5
  Project-URL: Homepage, https://www.sayouzone.com/
6
6
  Project-URL: Documentation, https://sayouzone.github.io/sayou-fabric/
@@ -7,7 +7,7 @@ build-backend = "hatchling.build"
7
7
  # -----------------
8
8
  [project]
9
9
  name = "sayou-visualizer"
10
- version = "0.0.8"
10
+ version = "0.0.10"
11
11
  authors = [
12
12
  { name = "Sayouzone", email = "contact@sayouzone.com" },
13
13
  ]
@@ -1,5 +1,6 @@
1
1
  from sayou.core.base_component import BaseComponent
2
2
 
3
+ from .renderer.kg_renderer import KGRenderer
3
4
  from .renderer.pyvis_renderer import PyVisRenderer
4
5
  from .tracer.graph_tracer import GraphTracer
5
6
  from .tracer.rich_tracer import RichConsoleTracer
@@ -22,30 +23,53 @@ class VisualizerPipeline(BaseComponent):
22
23
  self._rich_tracer = None
23
24
  self._ws_tracer = None
24
25
  self._renderer = None
26
+ self._kg_renderer = None
25
27
 
26
28
  def attach_to(self, target_pipeline: BaseComponent, mode: str = "report", **kwargs):
27
29
  """
28
- Connects this visualizer to a target pipeline (Connector, Refinery, etc.).
30
+ Connects this visualizer to a target pipeline AND its children recursively.
29
31
  """
32
+ tracer = None
30
33
  if mode == "report":
31
34
  self._graph_tracer = GraphTracer()
32
- target_pipeline.add_callback(self._graph_tracer)
35
+ tracer = self._graph_tracer
33
36
  self._log("Attached GraphTracer for HTML reporting.")
34
-
35
37
  elif mode == "live":
36
38
  self._rich_tracer = RichConsoleTracer()
37
- target_pipeline.add_callback(self._rich_tracer)
39
+ tracer = self._rich_tracer
38
40
  self._log("Attached RichConsoleTracer for live monitoring.")
39
-
40
41
  elif mode == "websocket":
41
42
  url = kwargs.get("url")
42
43
  if not url:
43
- raise ValueError("WebSocket mode requires a 'url' parameter.")
44
-
44
+ raise ValueError("WebSocket mode requires 'url'.")
45
45
  self._ws_tracer = WebSocketTracer(server_url=url)
46
- target_pipeline.add_callback(self._ws_tracer)
46
+ tracer = self._ws_tracer
47
47
  self._log(f"Attached WebSocketTracer to {url}")
48
48
 
49
+ if tracer:
50
+ self._recursive_attach(target_pipeline, tracer)
51
+ else:
52
+ self._log("No tracer attached. Visualizer will not record events.")
53
+
54
+ def _recursive_attach(self, component: BaseComponent, tracer):
55
+ """
56
+ Helper to attach tracer to the component and all its sub-components.
57
+ """
58
+ if hasattr(component, "_callbacks") and tracer in component._callbacks:
59
+ return
60
+
61
+ component.add_callback(tracer)
62
+ self._log(f"Attached tracer to {component.component_name}")
63
+
64
+ for attr_name, attr_value in component.__dict__.items():
65
+ if isinstance(attr_value, BaseComponent):
66
+ self._recursive_attach(attr_value, tracer)
67
+
68
+ elif isinstance(attr_value, list):
69
+ for item in attr_value:
70
+ if isinstance(item, BaseComponent):
71
+ self._recursive_attach(item, tracer)
72
+
49
73
  def report(self, output_path: str = "report.html", **kwargs):
50
74
  if self._graph_tracer.graph.number_of_nodes() == 0:
51
75
  self._log("No events recorded. The graph is empty.", level="warning")
@@ -57,6 +81,13 @@ class VisualizerPipeline(BaseComponent):
57
81
  self._renderer = PyVisRenderer()
58
82
  self._renderer.render(self._graph_tracer.graph, output_path, **kwargs)
59
83
 
84
+ def render_kg(self, json_path: str, output_path: str = "kg_view.html"):
85
+ """
86
+ Visualizes the OUTPUT JSON (Knowledge Graph).
87
+ """
88
+ self._kg_renderer = KGRenderer()
89
+ self._kg_renderer.render(json_path, output_path)
90
+
60
91
  def save_live_log(self, output_path="live_status.html"):
61
92
  if self._rich_tracer:
62
93
  self._rich_tracer.save_html(output_path)
@@ -0,0 +1,453 @@
1
+ import json
2
+ import os
3
+
4
+ from sayou.core.base_component import BaseComponent
5
+
6
+
7
+ class AnalyticKGRenderer(BaseComponent):
8
+ """
9
+ Renders an interactive 2D Knowledge Graph optimized for deep analysis and topology exploration.
10
+
11
+ Agnostic to data domains (Code, Subtitles, Text), it visualizes structural relationships
12
+ using a 'Hub & Spoke' layout. Features interactive filtering, search capabilities,
13
+ and detailed node inspection (Visual IDE/Viewer) to derive insights from complex connections.
14
+ """
15
+
16
+ component_name = "AnalyticKGRenderer"
17
+
18
+ STYLE_SHEET = [
19
+ # [Global Nodes]
20
+ {
21
+ "selector": "node",
22
+ "style": {
23
+ "label": "data(label)",
24
+ "color": "#ecf0f1",
25
+ "font-size": "10px",
26
+ "text-valign": "center",
27
+ "text-halign": "center",
28
+ "text-wrap": "wrap",
29
+ "text-max-width": "100px",
30
+ "background-color": "#95a5a6",
31
+ "border-width": 1,
32
+ "border-color": "#7f8c8d",
33
+ },
34
+ },
35
+ # [File Node]
36
+ {
37
+ "selector": "node[type='file']",
38
+ "style": {
39
+ "shape": "rectangle",
40
+ "background-color": "#2c3e50",
41
+ "width": 60,
42
+ "height": 60,
43
+ "font-size": "12px",
44
+ "font-weight": "bold",
45
+ "border-width": 2,
46
+ "border-color": "#00d2d3",
47
+ },
48
+ },
49
+ # [Class Node]
50
+ {
51
+ "selector": "node[type='class']",
52
+ "style": {
53
+ "shape": "diamond",
54
+ "background-color": "#8e44ad",
55
+ "width": 40,
56
+ "height": 40,
57
+ },
58
+ },
59
+ # [Method/Function]
60
+ {
61
+ "selector": "node[type='method'], node[type='function']",
62
+ "style": {
63
+ "shape": "ellipse",
64
+ "background-color": "#e67e22",
65
+ "width": 25,
66
+ "height": 25,
67
+ },
68
+ },
69
+ # [Code Chunk]
70
+ {
71
+ "selector": "node[type='code_block']",
72
+ "style": {
73
+ "shape": "round-rectangle",
74
+ "background-color": "#7f8c8d",
75
+ "width": 15,
76
+ "height": 15,
77
+ "label": "",
78
+ },
79
+ },
80
+ # [Package/Library]
81
+ {
82
+ "selector": "node[type='library'], node[type='package']",
83
+ "style": {
84
+ "shape": "hexagon",
85
+ "background-color": "#16a085",
86
+ "width": 50,
87
+ "height": 50,
88
+ },
89
+ },
90
+ # [Edges]
91
+ {
92
+ "selector": "edge",
93
+ "style": {
94
+ "width": 1,
95
+ "curve-style": "bezier",
96
+ "opacity": 0.6,
97
+ "arrow-scale": 1,
98
+ },
99
+ },
100
+ # 1. Structure Line (contains) -> Gray Dashed Line (Skeleton)
101
+ {
102
+ "selector": "edge[edgeType='sayou:contains']",
103
+ "style": {
104
+ "line-color": "#7f8c8d",
105
+ "target-arrow-color": "#7f8c8d",
106
+ "target-arrow-shape": "circle",
107
+ "width": 1.5,
108
+ "line-style": "dashed",
109
+ "opacity": 0.7,
110
+ },
111
+ },
112
+ # 2. Logic Line (imports) -> Cyan Dashed Line (Flow)
113
+ {
114
+ "selector": "edge[edgeType='sayou:imports']",
115
+ "style": {
116
+ "line-color": "#00d2d3",
117
+ "target-arrow-color": "#00d2d3",
118
+ "target-arrow-shape": "triangle",
119
+ "line-style": "dashed",
120
+ "width": 2,
121
+ "opacity": 0.9,
122
+ },
123
+ },
124
+ # 3. Inheritance Line (inherits) -> Red Solid Line
125
+ {
126
+ "selector": "edge[edgeType='sayou:inherits']",
127
+ "style": {
128
+ "line-color": "#ff6b6b",
129
+ "target-arrow-color": "#ff6b6b",
130
+ "target-arrow-shape": "triangle",
131
+ "width": 3,
132
+ },
133
+ },
134
+ # [Interaction]
135
+ {
136
+ "selector": ".highlighted",
137
+ "style": {
138
+ "background-color": "#f1c40f",
139
+ "line-color": "#f1c40f",
140
+ "target-arrow-color": "#f1c40f",
141
+ "opacity": 1,
142
+ "z-index": 999,
143
+ },
144
+ },
145
+ {
146
+ "selector": ".faded",
147
+ "style": {"opacity": 0.05, "label": ""},
148
+ },
149
+ {
150
+ "selector": ".found",
151
+ "style": {
152
+ "border-width": 4,
153
+ "border-color": "#e056fd",
154
+ "background-color": "#e056fd",
155
+ },
156
+ },
157
+ {
158
+ "selector": "node.no-label",
159
+ "style": {
160
+ "text-opacity": 0,
161
+ "text-background-opacity": 0,
162
+ "text-border-opacity": 0,
163
+ },
164
+ },
165
+ {
166
+ "selector": "edge.hidden-edge",
167
+ "style": {"display": "none"},
168
+ },
169
+ ]
170
+
171
+ def render(self, json_path: str, output_path: str = "sayou_analyst_view.html"):
172
+ if not os.path.exists(json_path):
173
+ return
174
+
175
+ with open(json_path, "r", encoding="utf-8") as f:
176
+ raw_data = json.load(f)
177
+
178
+ elements = []
179
+
180
+ # 1. Nodes (No Parents logic)
181
+ for node in raw_data.get("nodes", []):
182
+ node_id = node.get("node_id")
183
+ attrs = node.get("attributes", {})
184
+ n_cls = node.get("node_class", "unknown").lower()
185
+
186
+ # Type Check
187
+ cy_type = "unknown"
188
+ if "file" in n_cls:
189
+ cy_type = "file"
190
+ elif "class" in n_cls:
191
+ cy_type = "class"
192
+ elif "method" in n_cls:
193
+ cy_type = "method"
194
+ elif "function" in n_cls:
195
+ cy_type = "function"
196
+ elif "library" in n_cls:
197
+ cy_type = "library"
198
+ elif "code" in n_cls:
199
+ cy_type = "code_block"
200
+
201
+ # Labeling
202
+ label = attrs.get("label") or node.get("friendly_name") or node_id
203
+ if cy_type == "file":
204
+ label = os.path.basename(attrs.get("sayou:filePath", label))
205
+ elif cy_type == "class":
206
+ label = attrs.get("meta:class_name", label)
207
+ elif cy_type in ["method", "function"]:
208
+ label = attrs.get("function_name", label)
209
+
210
+ # Code Text
211
+ code_text = attrs.get("schema:text", "")
212
+ cy_data = {
213
+ "id": node_id,
214
+ "label": label,
215
+ "type": cy_type,
216
+ "code": code_text,
217
+ "meta": attrs,
218
+ }
219
+ elements.append({"group": "nodes", "data": cy_data})
220
+
221
+ # 2. Edges
222
+ for edge in raw_data.get("edges", []):
223
+ elements.append(
224
+ {
225
+ "group": "edges",
226
+ "data": {
227
+ "source": edge.get("source"),
228
+ "target": edge.get("target"),
229
+ "edgeType": edge.get("type", "relates"),
230
+ "label": edge.get("type", "").split(":")[-1],
231
+ },
232
+ }
233
+ )
234
+
235
+ self._generate_html(elements, output_path)
236
+ self._log(f"✅ Pure Graph View saved to: {output_path}")
237
+
238
+ def _generate_html(self, elements: list, output_path: str):
239
+ style_json = json.dumps(self.STYLE_SHEET)
240
+ elements_json = json.dumps(elements)
241
+
242
+ html_content = f"""
243
+ <!DOCTYPE html>
244
+ <html>
245
+ <head>
246
+ <meta charset="UTF-8">
247
+ <title>Sayou Code Universe</title>
248
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.28.1/cytoscape.min.js"></script>
249
+ <script src="https://unpkg.com/layout-base/layout-base.js"></script>
250
+ <script src="https://unpkg.com/cose-base/cose-base.js"></script>
251
+ <script src="https://unpkg.com/cytoscape-fcose/cytoscape-fcose.js"></script>
252
+
253
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
254
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
255
+
256
+ <style>
257
+ body {{ font-family: 'Segoe UI', sans-serif; margin: 0; background: #1e272e; color: #dcdde1; overflow: hidden; display: flex; }}
258
+ #cy {{ flex-grow: 1; height: 100vh; }}
259
+
260
+ /* Sidebar & Controls */
261
+ #controls {{
262
+ position: absolute; top: 20px; left: 20px; z-index: 100;
263
+ background: rgba(47, 54, 64, 0.95); padding: 15px; border-radius: 8px;
264
+ box-shadow: 0 4px 10px rgba(0,0,0,0.3); border: 1px solid #4b6584; width: 280px;
265
+ }}
266
+ #tooltip {{
267
+ position: absolute;
268
+ display: none;
269
+ background: rgba(0, 0, 0, 0.8);
270
+ color: #fff;
271
+ padding: 5px 10px;
272
+ border-radius: 4px;
273
+ font-size: 11px;
274
+ pointer-events: none;
275
+ z-index: 1000;
276
+ border: 1px solid #00d2d3;
277
+ }}
278
+ .search-group {{ display: flex; gap: 5px; margin-bottom: 10px; }}
279
+ input[type="text"] {{ flex-grow: 1; background: #222; border: 1px solid #57606f; color: white; padding: 6px; border-radius: 4px; }}
280
+ button {{ background: #4b6584; border: none; color: white; padding: 6px 12px; border-radius: 4px; cursor: pointer; }}
281
+ button:hover {{ background: #00d2d3; color: #000; }}
282
+
283
+ #sidebar {{
284
+ width: 450px; height: 100vh; background: #2f3640; border-left: 1px solid #4b6584;
285
+ display: flex; flex-direction: column; transform: translateX(450px); transition: 0.3s;
286
+ position: absolute; right: 0; top: 0; z-index: 200; box-shadow: -5px 0 15px rgba(0,0,0,0.5);
287
+ }}
288
+ #sidebar.open {{ transform: translateX(0); }}
289
+ .header {{ padding: 20px; border-bottom: 1px solid #4b6584; background: #252a34; }}
290
+ .node-title {{ font-size: 18px; color: #fff; font-weight: bold; margin: 0; }}
291
+ .content {{ flex-grow: 1; overflow-y: auto; padding: 0; background: #282c34; }}
292
+ pre {{ margin: 0; }}
293
+ </style>
294
+ </head>
295
+ <body>
296
+ <div id="controls">
297
+ <div style="font-weight:bold; color:#00d2d3; margin-bottom:10px;">Sayou Graph</div>
298
+ <div class="search-group">
299
+ <input type="text" id="search-input" placeholder="Search..." onkeyup="if(event.key === 'Enter') searchNode()">
300
+ <button onclick="searchNode()">🔍</button>
301
+ </div>
302
+ <div style="margin-bottom: 10px; display: flex; gap: 5px;">
303
+ <button id="btn-label" class="active" onclick="toggleLabels()" style="flex:1;">Labels ON</button>
304
+ <button id="btn-line" class="active" onclick="toggleLines()" style="flex:1;">Lines ON</button>
305
+ </div>
306
+ <div style="font-size:11px; color:#aaa; border-top:1px solid #57606f; padding-top:10px;">
307
+ <button onclick="runLayout()">Re-Layout</button>
308
+ <button onclick="resetView()">Reset Cam</button>
309
+ </div>
310
+ </div>
311
+
312
+ <div id="cy"></div>
313
+ <div id="tooltip"></div>
314
+
315
+ <div id="sidebar">
316
+ <div class="header">
317
+ <h2 class="node-title" id="sb-title">Details</h2>
318
+ <div id="sb-desc" style="font-size:12px; color:#aaa; margin-top:5px;"></div>
319
+ </div>
320
+ <div class="content">
321
+ <pre><code id="sb-code" class="language-python"></code></pre>
322
+ </div>
323
+ </div>
324
+
325
+ <script>
326
+ var cy = cytoscape({{
327
+ container: document.getElementById('cy'),
328
+ elements: {elements_json},
329
+ style: {style_json},
330
+ layout: {{
331
+ name: 'fcose',
332
+ quality: 'proof',
333
+ nodeSeparation: 75,
334
+ idealEdgeLength: edge => edge.data('edgeType') === 'sayou:contains' ? 50 : 200,
335
+ animate: false
336
+ }}
337
+ }});
338
+
339
+ function runLayout() {{
340
+ cy.layout({{ name: 'fcose', animate: true, animationDuration: 800 }}).run();
341
+ }}
342
+
343
+ // [Search]
344
+ function searchNode() {{
345
+ var query = document.getElementById('search-input').value.toLowerCase();
346
+ if(!query) return;
347
+ cy.elements().removeClass('found');
348
+ var found = cy.nodes().filter(ele => ele.data('label').toLowerCase().includes(query));
349
+ if(found.length > 0) {{
350
+ found.addClass('found');
351
+ // cy.animate({{ fit: {{ eles: found, padding: 50 }}, duration: 500 }});
352
+ // cy.center(found);
353
+ }}
354
+ }}
355
+
356
+ function resetView() {{
357
+ cy.elements().removeClass('highlighted faded found');
358
+ document.getElementById('sidebar').classList.remove('open');
359
+ cy.animate({{ fit: {{ padding: 50 }} }});
360
+ }}
361
+
362
+ // 1. Label Toggle (ON/OFF)
363
+ var labelsVisible = true;
364
+ function toggleLabels() {{
365
+ labelsVisible = !labelsVisible;
366
+ var btn = document.getElementById('btn-label');
367
+
368
+ if (labelsVisible) {{
369
+ cy.nodes().removeClass('no-label');
370
+ btn.innerText = "Labels ON";
371
+ btn.style.background = "#4b6584";
372
+ btn.style.color = "white";
373
+ }} else {{
374
+ cy.nodes().addClass('no-label');
375
+ btn.innerText = "Labels OFF";
376
+ btn.style.background = "#2f3542";
377
+ btn.style.color = "#747d8c";
378
+ }}
379
+ }}
380
+
381
+ // 2. Line Toggle (ON/OFF)
382
+ var linesVisible = true;
383
+ function toggleLines() {{
384
+ linesVisible = !linesVisible;
385
+ var btn = document.getElementById('btn-line');
386
+ // var edges = cy.edges('[edgeType="sayou:contains"]');
387
+ var edges = cy.edges();
388
+
389
+ if (linesVisible) {{
390
+ edges.removeClass('hidden-edge');
391
+ btn.innerText = "Lines ON";
392
+ btn.style.background = "#4b6584";
393
+ btn.style.color = "white";
394
+ }} else {{
395
+ edges.addClass('hidden-edge');
396
+ btn.innerText = "Lines OFF";
397
+ btn.style.background = "#2f3542";
398
+ btn.style.color = "#747d8c";
399
+ }}
400
+ }}
401
+
402
+ // [Click Interaction]
403
+ cy.on('tap', 'node', function(evt){{
404
+ var node = evt.target;
405
+
406
+ // Highlight neighbors
407
+ cy.elements().removeClass('highlighted faded');
408
+ var neighbors = node.neighborhood().add(node);
409
+ cy.elements().addClass('faded');
410
+ neighbors.removeClass('faded').addClass('highlighted');
411
+
412
+ // Sidebar
413
+ document.getElementById('sidebar').classList.add('open');
414
+ document.getElementById('sb-title').innerText = node.data('label');
415
+ document.getElementById('sb-desc').innerText = (node.data('type') || 'Unknown').toUpperCase() + ' | ' + node.id();
416
+
417
+ var codeArea = document.getElementById('sb-code');
418
+ codeArea.innerText = node.data('code') || JSON.stringify(node.data('meta'), null, 2);
419
+ hljs.highlightElement(codeArea);
420
+ }});
421
+
422
+ cy.on('tap', function(evt){{
423
+ if(evt.target === cy) resetView();
424
+ }});
425
+
426
+ var tooltip = document.getElementById('tooltip');
427
+
428
+ cy.on('mouseover', 'node', function(evt){{
429
+ var node = evt.target;
430
+ var label = node.data('label') || node.data('id');
431
+
432
+ tooltip.style.display = 'block';
433
+ tooltip.innerText = label;
434
+ tooltip.style.left = evt.renderedPosition.x + 'px';
435
+ // var pos = node.renderedPosition();
436
+ // tooltip.style.left = (pos.x + 10) + 'px';
437
+ // tooltip.style.top = (pos.y + 10) + 'px';
438
+ }});
439
+
440
+ cy.on('mousemove', function(evt){{
441
+ tooltip.style.left = (evt.renderedPosition.x + 15) + 'px';
442
+ tooltip.style.top = (evt.renderedPosition.y + 15) + 'px';
443
+ }});
444
+
445
+ cy.on('mouseout', 'node', function(){{
446
+ tooltip.style.display = 'none';
447
+ }});
448
+ </script>
449
+ </body>
450
+ </html>
451
+ """
452
+ with open(output_path, "w", encoding="utf-8") as f:
453
+ f.write(html_content)
@@ -0,0 +1,303 @@
1
+ import json
2
+ import os
3
+ from collections import defaultdict
4
+
5
+ from sayou.core.base_component import BaseComponent
6
+
7
+
8
+ class ShowcaseKGRenderer(BaseComponent):
9
+ """
10
+ Renders an immersive 3D Knowledge Graph designed for high-level visualization and presentation.
11
+
12
+ Focuses on the 'Big Picture' and aesthetic impact using spatial layouts and semantic coloring.
13
+ Ideal for demonstrating data scale, clustering patterns, and global connectivity (Wow Effect),
14
+ rather than granular node analysis.
15
+ """
16
+
17
+ component_name = "ShowcaseKGRenderer"
18
+
19
+ def render(self, json_path: str, output_path: str = "kg_view_3d.html"):
20
+ if not os.path.exists(json_path):
21
+ self._log(f"Output file not found: {json_path}", level="error")
22
+ return
23
+
24
+ with open(json_path, "r", encoding="utf-8") as f:
25
+ raw_data = json.load(f)
26
+
27
+ # ---------------------------------------------------------
28
+ # [Python Logic]
29
+ # ---------------------------------------------------------
30
+ nodes_by_source = defaultdict(list)
31
+ final_nodes = []
32
+ final_links = []
33
+
34
+ existing_ids = set()
35
+
36
+ for node in raw_data.get("nodes", []):
37
+ node_id = node.get("node_id")
38
+ existing_ids.add(node_id)
39
+ attrs = node.get("attributes", {})
40
+ meta = node.get("metadata", {})
41
+
42
+ source = meta.get("source") or attrs.get("sayou:source") or "Unknown Source"
43
+ nodes_by_source[source].append(node_id)
44
+
45
+ sem_type = attrs.get("sayou:semanticType", "text")
46
+
47
+ clean_attrs = {}
48
+ for k, v in attrs.items():
49
+ if isinstance(v, str) and len(v) > 300:
50
+ clean_attrs[k] = v[:200] + "..."
51
+ else:
52
+ clean_attrs[k] = v
53
+
54
+ display_label = attrs.get("schema:text", node_id)
55
+ if len(display_label) > 30:
56
+ display_label = display_label[:30] + "..."
57
+
58
+ group = "Chunk"
59
+ color = "#1e90ff"
60
+ val = 5
61
+
62
+ if sem_type in ["h1", "h2", "h3", "title"]:
63
+ group = "Header"
64
+ color = "#ffa502"
65
+ val = 12
66
+ elif "list" in sem_type:
67
+ group = "List"
68
+ color = "#2ed573"
69
+ val = 4
70
+ elif "table" in sem_type:
71
+ group = "Table"
72
+ color = "#a55eea"
73
+ val = 10
74
+ elif "code" in sem_type:
75
+ group = "Code"
76
+ color = "#ff4757"
77
+ val = 8
78
+
79
+ final_nodes.append(
80
+ {
81
+ "id": node_id,
82
+ "label": display_label,
83
+ "group": group,
84
+ "sem_type": sem_type,
85
+ "color": color,
86
+ "val": val,
87
+ "attributes": clean_attrs,
88
+ "source": source,
89
+ }
90
+ )
91
+
92
+ for edge in raw_data.get("links", []) + raw_data.get("edges", []):
93
+ final_links.append(
94
+ {
95
+ "source": edge.get("source"),
96
+ "target": edge.get("target"),
97
+ "relation": edge.get("relation", "relates_to"),
98
+ }
99
+ )
100
+
101
+ for source_name, child_ids in nodes_by_source.items():
102
+ if source_name in existing_ids:
103
+ continue
104
+
105
+ virtual_doc_id = f"VIRTUAL_DOC:{source_name}"
106
+
107
+ final_nodes.append(
108
+ {
109
+ "id": virtual_doc_id,
110
+ "label": source_name,
111
+ "group": "Document",
112
+ "color": "#ff4757",
113
+ "val": 30,
114
+ "attributes": {
115
+ "type": "Virtual Parent",
116
+ "child_count": len(child_ids),
117
+ },
118
+ "is_virtual": True,
119
+ }
120
+ )
121
+
122
+ for child_id in child_ids:
123
+ final_links.append(
124
+ {
125
+ "source": virtual_doc_id,
126
+ "target": child_id,
127
+ "relation": "CONTAINS",
128
+ }
129
+ )
130
+
131
+ graph_data = {"nodes": final_nodes, "links": final_links}
132
+ self._log(f"Rendering KG with Virtual Hierarchy ({len(final_nodes)} nodes)...")
133
+
134
+ # ---------------------------------------------------------
135
+ # [JS Logic] Renderer
136
+ # ---------------------------------------------------------
137
+ html_content = f"""
138
+ <!DOCTYPE html>
139
+ <html lang="en">
140
+ <head>
141
+ <meta charset="UTF-8">
142
+ <title>Sayou Dataverse</title>
143
+ <style>
144
+ body {{ margin: 0; background-color: #000205; overflow: hidden; }}
145
+ #graph {{ width: 100%; height: 100vh; }}
146
+
147
+ #info {{
148
+ position: absolute; top: 20px; right: 20px; width: 300px;
149
+ background: rgba(5, 10, 20, 0.85);
150
+ border: 1px solid rgba(0, 242, 255, 0.3);
151
+ border-left: 3px solid #00f2ff;
152
+ box-shadow: 0 0 20px rgba(0, 242, 255, 0.1);
153
+ color: #dff9fb; padding: 20px;
154
+ font-family: 'Consolas', 'Monaco', monospace;
155
+ backdrop-filter: blur(5px);
156
+ display: none; pointer-events: none;
157
+ border-radius: 0 10px 10px 0;
158
+ }}
159
+ .tag {{
160
+ display: inline-block; padding: 2px 8px; border-radius: 2px;
161
+ font-size: 9px; font-weight: 800; color: #000; margin-bottom: 12px;
162
+ text-transform: uppercase; letter-spacing: 1px;
163
+ }}
164
+ h3 {{
165
+ margin: 0 0 12px 0; color: #fff;
166
+ border-bottom: 1px dashed #576574; padding-bottom: 8px;
167
+ font-size: 14px; line-height: 1.4;
168
+ }}
169
+ .row {{ font-size: 11px; margin-bottom: 4px; color: #a4b0be; }}
170
+ .key {{ color: #00d2d3; margin-right: 5px; }}
171
+ </style>
172
+ <script src="https://unpkg.com/three@0.160.0/build/three.min.js"></script>
173
+ <script src="https://unpkg.com/3d-force-graph@1.73.1/dist/3d-force-graph.min.js"></script>
174
+ </head>
175
+ <body>
176
+ <div id="graph"></div>
177
+ <div id="info"></div>
178
+
179
+ <script>
180
+ const gData = {json.dumps(graph_data)};
181
+ const infoDiv = document.getElementById('info');
182
+
183
+ const Graph = ForceGraph3D()(document.getElementById('graph'))
184
+ .graphData(gData)
185
+ .backgroundColor('#000205')
186
+ .showNavInfo(false)
187
+
188
+ // [Design Logic]
189
+ .nodeThreeObject(node => {{
190
+ let geometry, material;
191
+
192
+ // 1. Virtual Document
193
+ if (node.group === "Document") {{
194
+ const size = node.val;
195
+ geometry = new THREE.BoxGeometry(size, size, size);
196
+
197
+ const edges = new THREE.EdgesGeometry(geometry);
198
+ material = new THREE.LineBasicMaterial({{
199
+ color: node.color,
200
+ transparent: true,
201
+ opacity: 0.4
202
+ }});
203
+ const wireframe = new THREE.LineSegments(edges, material);
204
+
205
+ const coreGeo = new THREE.BoxGeometry(size*0.2, size*0.2, size*0.2);
206
+ const coreMat = new THREE.MeshBasicMaterial({{ color: node.color, wireframe: true }});
207
+ wireframe.add(new THREE.Mesh(coreGeo, coreMat));
208
+
209
+ return wireframe;
210
+ }}
211
+
212
+ // 2. Headers (H1~H3): [발광 다이아몬드]
213
+ else if (node.group === "Header") {{
214
+ geometry = new THREE.OctahedronGeometry(4);
215
+ material = new THREE.MeshPhongMaterial({{
216
+ color: node.color,
217
+ emissive: node.color,
218
+ emissiveIntensity: 0.5,
219
+ shininess: 100,
220
+ flatShading: true
221
+ }});
222
+ }}
223
+
224
+ // 3. Chunk / Text: [데이터 오브]
225
+ else {{
226
+ // 단순 구 대신 Icosahedron(정이십면체)을 써서 디지털 느낌
227
+ geometry = new THREE.IcosahedronGeometry(3, 1);
228
+ material = new THREE.MeshLambertMaterial({{
229
+ color: node.color,
230
+ transparent: true,
231
+ opacity: 0.8
232
+ }});
233
+ }}
234
+
235
+ return new THREE.Mesh(geometry, material);
236
+ }})
237
+
238
+ // [Link Design]
239
+ .linkWidth(link => link.relation === "CONTAINS" ? 0 : 0.5)
240
+ .linkColor(() => '#2f3542')
241
+ .linkDirectionalParticles(link => link.relation === "CONTAINS" ? 1 : 3)
242
+ .linkDirectionalParticleWidth(1.2)
243
+ .linkDirectionalParticleSpeed(0.006)
244
+ .linkDirectionalParticleColor(link => link.relation === "CONTAINS" ? '#57606f' : '#00f2ff')
245
+
246
+ // [Interaction]
247
+ .onNodeHover(node => {{
248
+ document.body.style.cursor = node ? 'crosshair' : null;
249
+ if (node) {{
250
+ infoDiv.style.display = 'block';
251
+
252
+ let tagColor = node.color;
253
+ if(node.group === 'Document') tagColor = '#ff4757';
254
+
255
+ let html = `<span class='tag' style='background:${{tagColor}}'>${{node.group}}</span>`;
256
+ if(node.sem_type) html += `<span class='tag' style='background:#2f3542; color:#fff; margin-left:5px'>${{node.sem_type}}</span>`;
257
+
258
+ html += `<h3>${{node.label}}</h3>`;
259
+
260
+ if (node.attributes) {{
261
+ for (const [k, v] of Object.entries(node.attributes)) {{
262
+ if(k === 'type' || k === 'child_count') continue;
263
+ let key = k.includes(':') ? k.split(':').pop() : k;
264
+ html += `<div class='row'><span class='key'>${{key}}:</span> ${{v}}</div>`;
265
+ }}
266
+ }}
267
+ infoDiv.innerHTML = html;
268
+ }} else {{
269
+ infoDiv.style.display = 'none';
270
+ }}
271
+ }})
272
+ .onNodeClick(node => {{
273
+ const distance = 70;
274
+ const distRatio = 1 + distance/Math.hypot(node.x, node.y, node.z);
275
+ const newPos = node.x || node.y || node.z
276
+ ? {{ x: node.x * distRatio, y: node.y * distRatio, z: node.z * distRatio }}
277
+ : {{ x: 0, y: 0, z: distance }};
278
+ Graph.cameraPosition(newPos, node, 1500);
279
+ }});
280
+
281
+ // [Lighting]
282
+ const ambientLight = new THREE.AmbientLight(0x222222); // 어두운 기본광
283
+ Graph.scene().add(ambientLight);
284
+
285
+ const blueLight = new THREE.PointLight(0x00f2ff, 1, 100); // 청록색 포인트 조명
286
+ blueLight.position.set(50, 50, 50);
287
+ Graph.scene().add(blueLight);
288
+
289
+ const pinkLight = new THREE.PointLight(0xff00ff, 0.5, 100); // 핑크색 포인트 조명 (반대편)
290
+ pinkLight.position.set(-50, -50, -50);
291
+ Graph.scene().add(pinkLight);
292
+
293
+ // [Physics]
294
+ Graph.d3Force('charge').strength(-50);
295
+ Graph.d3Force('link').distance(link => link.relation === "CONTAINS" ? 60 : 30);
296
+
297
+ </script>
298
+ </body>
299
+ </html>
300
+ """
301
+ with open(output_path, "w", encoding="utf-8") as f:
302
+ f.write(html_content)
303
+ self._log(f"✅ Semantic 3D KG Showcase saved to: {output_path}")
@@ -0,0 +1,101 @@
1
+ import networkx as nx
2
+ from sayou.core.callbacks import BaseCallback
3
+
4
+
5
+ class GraphTracer(BaseCallback):
6
+ def __init__(self):
7
+ self.graph = nx.DiGraph()
8
+ self._execution_stack = ["Root"]
9
+ self.graph.add_node(
10
+ "Root",
11
+ label="Sayou Pipeline",
12
+ color="#ffffff",
13
+ shape="dot",
14
+ size=25,
15
+ font={"size": 20, "color": "black"},
16
+ )
17
+
18
+ def on_start(self, component_name, input_data, **kwargs):
19
+ try:
20
+ comp_node = f"Comp:{component_name}_{len(self._execution_stack)}"
21
+
22
+ color = "#a5b1c2"
23
+ size = 15
24
+ if "Pipeline" in component_name:
25
+ color = "#ff6b81"
26
+ size = 20
27
+ elif "Generator" in component_name or "Fetcher" in component_name:
28
+ color = "#2ed573"
29
+ elif "Parser" in component_name or "Converter" in component_name:
30
+ color = "#eccc68"
31
+ elif "Splitter" in component_name:
32
+ color = "#70a1ff"
33
+
34
+ if not self.graph.has_node(comp_node):
35
+ self.graph.add_node(
36
+ comp_node, label=component_name, shape="dot", color=color, size=size
37
+ )
38
+ parent_node = self._execution_stack[-1]
39
+
40
+ if "Generator" in parent_node and "Pipeline" in component_name:
41
+ for ancestor in reversed(self._execution_stack):
42
+ if "Pipeline" in ancestor and "Connector" not in ancestor:
43
+ parent_node = ancestor
44
+ break
45
+ self.graph.add_edge(parent_node, comp_node)
46
+ self._execution_stack.append(comp_node)
47
+
48
+ data_id = self._get_data_id(input_data)
49
+ if data_id:
50
+ data_node_id = f"Data:{data_id}"
51
+ if not self.graph.has_node(data_node_id):
52
+ label_text = str(data_id)
53
+ if len(label_text) > 20:
54
+ label_text = label_text[:17] + "..."
55
+ self.graph.add_node(
56
+ data_node_id,
57
+ label=label_text,
58
+ shape="square",
59
+ size=8,
60
+ color="#57606f",
61
+ )
62
+ self.graph.add_edge(comp_node, data_node_id)
63
+
64
+ except Exception as e:
65
+ print(f"!!! TRACER ERROR: {e}")
66
+
67
+ def on_finish(self, component_name, result_data, success, **kwargs):
68
+ try:
69
+ if len(self._execution_stack) > 1:
70
+ if component_name in self._execution_stack[-1]:
71
+ self._execution_stack.pop()
72
+ else:
73
+ for i in range(len(self._execution_stack) - 1, 0, -1):
74
+ if component_name in self._execution_stack[i]:
75
+ self._execution_stack = self._execution_stack[:i]
76
+ break
77
+ except Exception as e:
78
+ print(f"!!! TRACER POP ERROR: {e}")
79
+
80
+ def _get_data_id(self, data):
81
+ if isinstance(data, dict):
82
+ return data.get("source") or data.get("uri") or data.get("filename")
83
+
84
+ uri = getattr(data, "uri", None)
85
+ if uri:
86
+ return uri
87
+
88
+ source = getattr(data, "source", None)
89
+ if source:
90
+ return source
91
+
92
+ filename = getattr(data, "filename", None)
93
+ if filename:
94
+ return filename
95
+
96
+ return None
97
+
98
+ def _get_data_id_from_result(self, data):
99
+ if hasattr(data, "task") and hasattr(data.task, "uri"):
100
+ return data.task.uri
101
+ return None
@@ -1,94 +0,0 @@
1
- import networkx as nx
2
- from sayou.core.callbacks import BaseCallback
3
-
4
-
5
- class GraphTracer(BaseCallback):
6
- def __init__(self):
7
- self.graph = nx.DiGraph()
8
- self.graph.add_node(
9
- "Root",
10
- label="Sayou Pipeline",
11
- color="#ffffff",
12
- shape="dot",
13
- size=25,
14
- font={"size": 20, "color": "black"},
15
- )
16
-
17
- def on_start(self, component_name, input_data, **kwargs):
18
- try:
19
- comp_node = f"Comp:{component_name}"
20
-
21
- color = "#a5b1c2"
22
- size = 15
23
-
24
- if "ConnectorPipeline" in component_name:
25
- color = "#ffffff"
26
- size = 20
27
- elif "Generator" in component_name:
28
- color = "#00d2d3"
29
- size = 15
30
- elif "Fetcher" in component_name:
31
- color = "#ff9f43"
32
- size = 15
33
-
34
- if not self.graph.has_node(comp_node):
35
- self.graph.add_node(
36
- comp_node, label=component_name, shape="dot", color=color, size=size
37
- )
38
-
39
- if "Pipeline" in component_name:
40
- self.graph.add_edge("Root", comp_node)
41
- else:
42
- parent = "Comp:ConnectorPipeline"
43
- if not self.graph.has_node(parent):
44
- parent = "Root"
45
- self.graph.add_edge(parent, comp_node)
46
-
47
- data_id = self._get_data_id(input_data)
48
- if data_id:
49
- label_text = str(data_id)
50
- if len(label_text) > 25:
51
- label_text = label_text[:22] + "..."
52
-
53
- node_id = f"Data:{data_id}"
54
- if not self.graph.has_node(node_id):
55
- self.graph.add_node(
56
- node_id,
57
- label=label_text,
58
- title=str(data_id),
59
- shape="dot",
60
- size=8,
61
- color={"background": "#57606f", "border": "#57606f"},
62
- )
63
-
64
- self.graph.add_edge(comp_node, node_id)
65
-
66
- except Exception as e:
67
- print(f"!!! TRACER ERROR: {e}")
68
-
69
- def on_finish(self, component_name, result_data, success, **kwargs):
70
- try:
71
- data_id = self._get_data_id_from_result(result_data)
72
- if data_id:
73
- node_id = f"Data:{data_id}"
74
- if self.graph.has_node(node_id):
75
- color = "#54a0ff" if success else "#ff6b6b"
76
- self.graph.nodes[node_id]["color"] = color
77
- self.graph.nodes[node_id]["size"] = 10
78
- except Exception as e:
79
- print(f"!!! TRACER ERROR in on_finish: {e}")
80
- pass
81
-
82
- def _get_data_id(self, data):
83
- if isinstance(data, dict):
84
- return data.get("source") or data.get("uri")
85
- if hasattr(data, "uri"):
86
- return data.uri
87
- if hasattr(data, "source"):
88
- return data.source
89
- return None
90
-
91
- def _get_data_id_from_result(self, data):
92
- if hasattr(data, "task") and hasattr(data.task, "uri"):
93
- return data.task.uri
94
- return None