This (unregisted) package demonstates how you can expose Plotly in a Julia package, in a way that works reliably in all Julia programming environments.
See [How to test plotly.md](How to test plotly.md) for examples in each environment.
In short:
begin
import Pkg
Pkg.activate(temp=true)
Pkg.add([
Pkg.PackageSpec(
url="https://github.com/JuliaPluto/PlotlyDemoPackage.jl", # fill in package URL
rev="main", # fill in branch name here
),
])
using Plots
endplot(1:10, rand(10))This demo package only supports:
plot(x::Vector, y::Vector)By default, plots get JS assets from the jsdelivr CDN. To use offline mode, set PlotlyDemoPackage.use_cdn[] = false and re-display a plot.
Notable are:
- No requireJS (it has been deprecated/abandoned for a long time)
<script type="module">for top-level await- E6 dynamic
importto dynamically load libraries or not, depending on what is already loaded. E.g. Pluto and Jupyter preload MathJax. - top-level
awaitto avoid redundant work when rendering multiple plots at the same time. - Julia Artifacts to make the JS assets available in a very reliable way.
- No Pkg build step for reliability
- Data URL (using Base64) to make the local JS assets available without relying on file server dynamics.
It works in all environments!
You can write the HTML repr to a file and open it in a browser:
p = plot(1:100, rand(100))
write("/Users/fons/Downloads/test.html", repr(MIME"text/html"(), p))
This HTML snippet also works when embedded in a larger page. This is done in:
- Documenter.jl
- Genie.jl
- Franklin.jl
- Weave.jl
- etc
It works regardless of whether Plotly and MathJax are already loaded.
Using specific API for Pluto, we could make the experience even better in Pluto. Right now, this is not done for simplicity. What would be added:
AbstractPlutoDingetjes.Display.published_to_jsfor:- offline support without performance penalty.
- displaying large amounts of data more quickly.
AbstractPlutoDingetjes.Display.is_inside_plutoto display different content when rendered in a Pluto(-compatible) display.Plotly.reactand some of Pluto's JS API to dynamically update plots when the data changes, for ultra-fast updates without layout shift.
These technologies are used in the https://github.com/JuliaPluto/PlutoPlotly.jl package, but it would be nice to make some of it available by default in other plotting package.