Skip to content

Commit d4e97a9

Browse files
committed
fix display in Jupyter
1 parent 733efca commit d4e97a9

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ Manifest.toml
1212
*.tar.gz
1313
deps/temp*
1414
notebooks/
15+
*.ipynb
16+
*.html
17+
docs/demo_files

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ PlotlyKaleido.savefig(p, "myplot.png")
109109

110110
# ⚙️ Defaults
111111

112-
A common workflow is to create multiple plots with a similar style. Rather then setting nearly identical layouts for multiple plots, you can set default values for a variety of items. HTML defaults (`class`/`style`/`parent_class`/`parent_style`) are chosen to make the plot reactive to the browser window size.
112+
You can set default values for the `layout`, `config`, and a number of other options that affect how the plot displays in your browser. HTML defaults (`class`/`style`/`parent_class`/`parent_style`) are chosen to make the plot reactive to the browser window size.
113113

114114
```julia
115115
module Defaults
@@ -133,10 +133,10 @@ end
133133
</div>
134134
```
135135

136-
- Default values can be set e.g.
136+
- Default values are `Ref`s and can be changed e.g.
137137

138138
```julia
139-
PlotlyLight.Defaults.layout[].title="Default Title"
139+
PlotlyLight.Defaults.layout[].title.text = "Default Title"
140140
```
141141

142142
- Revert back to the original defaults with `Defaults.reset!()`

src/PlotlyLight.jl

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ end
4747
end # Defaults module
4848

4949
#-----------------------------------------------------------------------------# src!
50-
struct CDN
51-
url::String
52-
CDN(url="https://cdn.plot.ly/$version.min.js") = new(url)
53-
end
54-
55-
5650
src_opts = [:cdn, :local, :standalone, :none]
5751
"""
5852
src!(x::Symbol) # `x` must be one of: $src_opts
@@ -137,23 +131,23 @@ Base.display(::Cobweb.CobwebDisplay, o::Plot) = display(Cobweb.CobwebDisplay(),
137131

138132
Base.show(io::IO, ::MIME"juliavscode/html", o::Plot) = show(io, MIME"text/html"(), o)
139133

140-
function Base.show(io::IO, M::MIME"text/html", o::Plot)
134+
function write_plot_div(io::IO, o::Plot)
141135
class, style = Defaults.class, Defaults.style
142136
parent_class, parent_style = Defaults.parent_class, Defaults.parent_style
143-
parent_style = if get(io, :is_pluto, false)
144-
s = replace(parent_style[], r"height.*;" => "")
145-
"height: 400px;" * s
146-
else
137+
parent_style = get(io, :is_pluto, false) || get(io, :jupyter, false) ?
138+
"height:400px;" * parent_style[] :
147139
parent_style[]
148-
end
149-
src = Defaults.src[]
150-
src in [:cdn, :standalone, :none, :local] || error("`src` must be :cdn, :standalone, :none, or :local")
151140
println(io, "<div class=\"", parent_class[], "\" style=\"", parent_style, "\" id=\"", "parent-of-", o.id, "\">")
152141
println(io, " <div class=\"", class[], "\" style=\"", style[], "\" id=\"", o.id, "\"></div>")
153142
println(io, "</div>")
143+
end
144+
145+
function write_load_plotly(io)
146+
src = Defaults.src[]
147+
src in [:cdn, :standalone, :none, :local] || error("`src` must be :cdn, :standalone, :none, or :local")
154148

155149
if src === :cdn
156-
println(io, "<script src=$(repr(cdn_url))></script>")
150+
println(io, "<script src=", repr(cdn_url), "></script>")
157151
elseif src === :standalone
158152
print(io, "<script>")
159153
for line in eachline(plotlyjs)
@@ -162,20 +156,56 @@ function Base.show(io::IO, M::MIME"text/html", o::Plot)
162156
println(io, "</script>")
163157
elseif src === :local
164158
println(io, "<script src=\"", plotlyjs, "\"></script>")
165-
else
166-
# :none
167159
end
160+
end
168161

169-
println(io, "<script>")
162+
function write_newplot(io::IO, o::Plot)
170163
print(io, "Plotly.newPlot(\"", o.id, "\", ")
171164
JSON3.write(io, o.data)
172165
print(io, ", ")
173166
JSON3.write(io, o.layout)
174167
print(io, ", ")
175168
JSON3.write(io, o.config)
176169
println(io, ");")
177-
show(io, MIME"text/javascript"(), o.js)
178-
print(io, "</script>\n")
170+
end
171+
172+
function write_require_config(io)
173+
write(io, """
174+
<script type="text/javascript">
175+
if (typeof require !== 'undefined') {
176+
require.undef("plotly");
177+
requirejs.config({
178+
paths: {
179+
'plotly': '$(cdn_url[7:end-3])'
180+
}
181+
});
182+
require(['plotly'], function(Plotly) {
183+
window._Plotly = Plotly;
184+
});
185+
}
186+
</script>
187+
""")
188+
end
189+
190+
function Base.show(io::IO, M::MIME"text/html", o::Plot)
191+
write_plot_div(io, o)
192+
193+
if :jupyter in keys(io)
194+
write_require_config(io)
195+
println(io, "<script>")
196+
write(io, "require([\"plotly\"], function(Plotly) {\n")
197+
write_newplot(io, o)
198+
show(io, MIME"text/javascript"(), o.js)
199+
write(io, "});")
200+
println(io, "</script>")
201+
else
202+
# write_plot_div(io, o)
203+
write_load_plotly(io)
204+
println(io, "<script>")
205+
write_newplot(io, o)
206+
show(io, MIME"text/javascript"(), o.js)
207+
print(io, "</script>\n")
208+
end
179209
end
180210

181211
#-----------------------------------------------------------------------------# vecvec

0 commit comments

Comments
 (0)