Skip to content

Commit d1af853

Browse files
Dale-Blackclaude
andcommitted
Dynamic Plotly plot div: scope to island instead of hardcoded ID
Multiple Plotly islands on the same page shared the hardcoded "therapy-plot" ID, causing them to render into the wrong element. Now uses island.querySelector('div[id]') to find the plot div within the island's own scope. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c695dff commit d1af853

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

src/compiler/packages_plotly.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ end
3737
function _plotly_plot_compiler(ctx, kwargs, pos_args)
3838
# Positional: plotly(divid, traces, layout) OR plot(traces, layout)
3939
# Detect by checking if first arg looks like a string (divid)
40-
el_id = "\"therapy-plot\""
4140
traces_js = "[]"
4241
layout_js = "{}"
42+
el_expr = nothing # will use dynamic island-scoped lookup by default
4343

4444
if length(pos_args) >= 3
4545
# plotly(divid, traces, layout)
46-
el_id = pos_args[1]
46+
el_expr = "document.getElementById($(pos_args[1]))"
4747
traces_js = pos_args[2]
4848
layout_js = pos_args[3]
4949
elseif length(pos_args) >= 2
50-
# plot(traces, layout) or plotly(divid, traces)
50+
# plot(traces, layout)
5151
traces_js = pos_args[1]
5252
layout_js = pos_args[2]
5353
elseif length(pos_args) >= 1
@@ -56,10 +56,16 @@ function _plotly_plot_compiler(ctx, kwargs, pos_args)
5656

5757
# Override with kwargs if present
5858
if haskey(kwargs, :divid)
59-
el_id = kwargs[:divid]
59+
el_expr = "document.getElementById($(kwargs[:divid]))"
6060
end
6161

62-
return "(function() { var _el = document.getElementById($(el_id)); if (_el && typeof Plotly !== 'undefined') { Plotly.react(_el, $(traces_js), $(layout_js), {responsive: true, displayModeBar: false}); } else if (_el) { var _s = document.createElement('script'); _s.src = 'https://cdn.plot.ly/plotly-2.35.2.min.js'; _s.onload = function() { Plotly.newPlot(_el, $(traces_js), $(layout_js), {responsive: true, displayModeBar: false}); }; document.head.appendChild(_s); } }())"
62+
# Default: find the plot div dynamically within the island scope
63+
# Uses the `island` variable from Therapy's hydration IIFE
64+
if el_expr === nothing
65+
el_expr = "(typeof island!=='undefined'?island.querySelector('div[id]'):document.getElementById('therapy-plot'))"
66+
end
67+
68+
return "(function() { var _el = $(el_expr); if (_el && typeof Plotly !== 'undefined') { Plotly.react(_el, $(traces_js), $(layout_js), {responsive: true, displayModeBar: false}); } else if (_el) { var _s = document.createElement('script'); _s.src = 'https://cdn.plot.ly/plotly-2.35.2.min.js'; _s.onload = function() { Plotly.newPlot(_el, $(traces_js), $(layout_js), {responsive: true, displayModeBar: false}); }; document.head.appendChild(_s); } }())"
6369
end
6470

6571
# ─── Registration function (called when a module that has these names is available) ───

0 commit comments

Comments
 (0)