diff --git a/Project.toml b/Project.toml index ce62a48d..78904ce3 100644 --- a/Project.toml +++ b/Project.toml @@ -5,6 +5,7 @@ version = "0.18.8" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" +AssetRegistry = "bf4720bc-e11a-5d0c-854e-bdca1663c893" Blink = "ad839575-38b3-5650-b840-f874b8c74a25" DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" JSExpr = "97c1335a-c9c5-57fe-bc5d-ec35cebe8660" diff --git a/src/PlotlyJS.jl b/src/PlotlyJS.jl index 9d3e514e..406532c0 100644 --- a/src/PlotlyJS.jl +++ b/src/PlotlyJS.jl @@ -15,6 +15,7 @@ import PlotlyBase: react, react!, add_trace! using WebIO +using AssetRegistry using JSExpr using JSExpr: @var, @new using Blink @@ -28,7 +29,7 @@ const _pkg_root = dirname(dirname(@__FILE__)) const _js_path = joinpath(artifact"plotly-artifacts", "plotly.min.js") const _js_cdn_path = "https://cdn.plot.ly/plotly-latest.min.js" const _mathjax_cdn_path = - "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_SVG" + "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-AMS-MML_SVG" struct PlotlyJSDisplay <: AbstractDisplay end diff --git a/src/display.jl b/src/display.jl index ba1c50ad..aa12abb1 100644 --- a/src/display.jl +++ b/src/display.jl @@ -167,9 +167,57 @@ function display_blink(p::SyncPlot) "height" => floor(Int, plotSize[2] * sizeBuffer) ) p.window = Blink.Window(windowOptions) + + # check if the inlcude_mathjax option is set and add the respective header + # the syntax is chosen identical to the python version: Layout(include_mathjax = ) + mathjax = mathjax_path(p) + mathjax == "" || try + # if file is local then add the mathjax path to the asset registry + # Currently `WebIO` does not support registering of directories, PR is pending ... + # otherwise we could do: `mathjax = WebIO.dep2url(mathjax)` + # so we have to use `AssetRegistry` directly + if WebIO.islocal(mathjax) + mathjax = joinpath(AssetRegistry.register(dirname(mathjax)), basename(mathjax)) + end + # add the mathjax file to the section + Blink.loadjs!(p.window, mathjax) + catch + @warn """Could not verify mathjax path! + + Consider installing IJulia. + Currently, however, TexFonts need to be installed manually... :-( + """ + end + Blink.body!(p.window, p.scope) end +# convert "cdn" and "local" to the respective online or local mathjax-paths +function mathjax_path(mj::AbstractString) + if mj == "cdn" + return _mathjax_cdn_path + elseif mj == "local" + mj = abspath(first(DEPOT_PATH), "conda", "3", "Lib", "site-packages", + "notebook", "static", "components", "MathJax", "MathJax.js") + return isfile(mj) ? (mj * "?config=TeX-AMS-MML_HTMLorMML-full") : "" + end + return mj +end + +# retrieve the mathjax option from the SyncPlot +function mathjax_path(p::SyncPlot) + local mathjax + try + # Layout(include_mathjax = ...) results in a layout field :include with a dictionary entry :mathjax + mathjax = p.plot.layout[:include][:mathjax] + catch + # if called from Plot's plotlyjs() backend, the extra_kwargs are passed literally, i.e `:include_mathjax` + mathjax = get(p.plot.layout.fields, :include_mathjax, "") + end + # convert "cdn" and "local" to the respective online or local mathjax paths + return mathjax_path(mathjax) +end + function Base.close(p::SyncPlot) if p.window !== nothing && Blink.active(p.window) close(p.window)