Fix graph interactivity: embed JS/CSS inline in HTML
Previous approach used ui.run_javascript with getElement() which failed due to Vue rendering timing. Now embeds the script and style directly inside the HTML content so there are no DOM lookup or timing issues — the script runs inline when parsed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -427,62 +427,58 @@ def _render_graphviz(dot_source: str, selected_node_id: str | None = None):
|
|||||||
src = graphviz.Source(dot_source)
|
src = graphviz.Source(dot_source)
|
||||||
svg = src.pipe(format='svg').decode('utf-8')
|
svg = src.pipe(format='svg').decode('utf-8')
|
||||||
|
|
||||||
# (a) Keep SVG at natural size, let scroll container handle overflow
|
|
||||||
html_el = ui.html(
|
|
||||||
f'<div style="overflow: auto; max-height: 500px; width: 100%;">'
|
|
||||||
f'{svg}</div>'
|
|
||||||
)
|
|
||||||
|
|
||||||
# (b) + (c) JS click handlers + visual feedback
|
|
||||||
# Use NiceGUI's element ID (getElement) for reliable DOM lookup
|
|
||||||
sel_escaped = selected_node_id.replace("'", "\\'") if selected_node_id else ''
|
sel_escaped = selected_node_id.replace("'", "\\'") if selected_node_id else ''
|
||||||
ui.run_javascript(f'''
|
|
||||||
requestAnimationFrame(() => {{
|
# Embed CSS + JS inline so there are no timing/lookup issues
|
||||||
const wrapper = getElement({html_el.id});
|
inline_script = '''
|
||||||
if (!wrapper) return;
|
<style>
|
||||||
const container = wrapper.querySelector('div');
|
.timeline-graph g.node { cursor: pointer; }
|
||||||
|
.timeline-graph g.node:hover { filter: brightness(1.3); }
|
||||||
|
.timeline-graph g.node.selected ellipse,
|
||||||
|
.timeline-graph g.node.selected polygon[stroke]:not([stroke="none"]) {
|
||||||
|
stroke: #f59e0b !important;
|
||||||
|
stroke-width: 3px !important;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<script>
|
||||||
|
(function() {
|
||||||
|
// Find the container we just rendered into
|
||||||
|
var scripts = document.querySelectorAll('script');
|
||||||
|
var thisScript = scripts[scripts.length - 1];
|
||||||
|
var container = thisScript.closest('.timeline-graph');
|
||||||
if (!container) return;
|
if (!container) return;
|
||||||
|
|
||||||
// CSS for interactivity
|
container.querySelectorAll('g.node').forEach(function(g) {
|
||||||
const style = document.createElement('style');
|
g.addEventListener('click', function() {
|
||||||
const uid = 'graph-' + {html_el.id};
|
var title = g.querySelector('title');
|
||||||
container.id = uid;
|
if (title) {
|
||||||
style.textContent = `
|
|
||||||
#${{uid}} g.node {{ cursor: pointer; }}
|
|
||||||
#${{uid}} g.node:hover {{ filter: brightness(1.3); }}
|
|
||||||
#${{uid}} g.node.selected ellipse,
|
|
||||||
#${{uid}} g.node.selected polygon[stroke]:not([stroke="none"]) {{
|
|
||||||
stroke: #f59e0b !important;
|
|
||||||
stroke-width: 3px !important;
|
|
||||||
}}
|
|
||||||
`;
|
|
||||||
container.appendChild(style);
|
|
||||||
|
|
||||||
// Attach click handlers
|
|
||||||
container.querySelectorAll('g.node').forEach(function(g) {{
|
|
||||||
g.addEventListener('click', function() {{
|
|
||||||
const title = g.querySelector('title');
|
|
||||||
if (title) {{
|
|
||||||
window.graphSelectedNode = title.textContent.trim();
|
window.graphSelectedNode = title.textContent.trim();
|
||||||
container.querySelectorAll('g.node.selected').forEach(
|
container.querySelectorAll('g.node.selected').forEach(
|
||||||
el => el.classList.remove('selected'));
|
function(el) { el.classList.remove('selected'); });
|
||||||
g.classList.add('selected');
|
g.classList.add('selected');
|
||||||
}}
|
}
|
||||||
}});
|
});
|
||||||
}});
|
});
|
||||||
|
|
||||||
// Re-apply selected class if we already have a selection
|
// Re-apply selected class
|
||||||
const selId = '{sel_escaped}';
|
var selId = '%s';
|
||||||
if (selId) {{
|
if (selId) {
|
||||||
container.querySelectorAll('g.node').forEach(function(g) {{
|
container.querySelectorAll('g.node').forEach(function(g) {
|
||||||
const title = g.querySelector('title');
|
var title = g.querySelector('title');
|
||||||
if (title && title.textContent.trim() === selId) {{
|
if (title && title.textContent.trim() === selId) {
|
||||||
g.classList.add('selected');
|
g.classList.add('selected');
|
||||||
}}
|
}
|
||||||
}});
|
});
|
||||||
}}
|
}
|
||||||
}});
|
})();
|
||||||
''')
|
</script>
|
||||||
|
''' % sel_escaped
|
||||||
|
|
||||||
|
ui.html(
|
||||||
|
f'<div class="timeline-graph"'
|
||||||
|
f' style="overflow: auto; max-height: 500px; width: 100%;">'
|
||||||
|
f'{svg}{inline_script}</div>'
|
||||||
|
)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
ui.label('Install graphviz Python package for graph rendering.').classes('text-warning')
|
ui.label('Install graphviz Python package for graph rendering.').classes('text-warning')
|
||||||
ui.code(dot_source).classes('w-full')
|
ui.code(dot_source).classes('w-full')
|
||||||
|
|||||||
Reference in New Issue
Block a user