From 5308fc3d9586b4ea72770793144a0aee85a59f30 Mon Sep 17 00:00:00 2001 From: Helmut Haensel Date: Tue, 2 Jun 2020 14:38:19 +0200 Subject: [PATCH 1/2] support include_mathjax option for Blink display --- Project.toml | 1 + src/PlotlyJS.jl | 3 ++- src/display.jl | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c32dd389..c142b91d 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ authors = ["Spencer Lyon "] version = "0.13.1" [deps] +AssetRegistry = "bf4720bc-e11a-5d0c-854e-bdca1663c893" Blink = "ad839575-38b3-5650-b840-f874b8c74a25" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" diff --git a/src/PlotlyJS.jl b/src/PlotlyJS.jl index 67a188dc..a09b991c 100644 --- a/src/PlotlyJS.jl +++ b/src/PlotlyJS.jl @@ -14,6 +14,7 @@ import PlotlyBase: react, react! using WebIO +using AssetRegistry using JSExpr using JSExpr: @var, @new using Blink @@ -26,7 +27,7 @@ const _pkg_root = dirname(dirname(@__FILE__)) const _js_path = joinpath(_pkg_root, "assets", "plotly-latest.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 1342e50e..5b67c682 100644 --- a/src/display.jl +++ b/src/display.jl @@ -156,9 +156,57 @@ function display_blink(p::SyncPlot) plotSize = size(p.plot) windowOptions = Dict("width"=>floor(Int,plotSize[1]*sizeBuffer), "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, :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) From 2088c5f6c36c650367cc94e35b2ef6a8c2b865a0 Mon Sep 17 00:00:00 2001 From: Helmut Haensel Date: Tue, 2 Jun 2020 16:19:04 +0200 Subject: [PATCH 2/2] modify mathjax option handling for plots from plotlyjs() backend --- src/display.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/display.jl b/src/display.jl index 5b67c682..2cefb8b2 100644 --- a/src/display.jl +++ b/src/display.jl @@ -201,7 +201,7 @@ function mathjax_path(p::SyncPlot) 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, :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)