Skip to content

JuliaPluto/PlotlyDemoPackage.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PlotlyDemoPackage.jl

This (unregisted) package demonstates how you can expose Plotly in a Julia package, in a way that works reliably in all Julia programming environments.

How to use

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
end
plot(1:10, rand(10))

This demo package only supports:

plot(x::Vector, y::Vector)

Offline mode

By default, plots get JS assets from the jsdelivr CDN. To use offline mode, set PlotlyDemoPackage.use_cdn[] = false and re-display a plot.

Used technologies

Notable are:

  • No requireJS (it has been deprecated/abandoned for a long time)
  • <script type="module"> for top-level await
  • E6 dynamic import to dynamically load libraries or not, depending on what is already loaded. E.g. Pluto and Jupyter preload MathJax.
  • top-level await to 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.

Environment support

It works in all environments!

In Jupyter

Scherm­afbeelding 2025-08-19 om 17 33 29

In Pluto

Scherm­afbeelding 2025-08-19 om 17 35 51

In VS Code notebooks

Scherm­afbeelding 2025-08-19 om 17 35 24

In VS Code results

Scherm­afbeelding 2025-08-21 om 09 26 47

In the web

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))
Scherm­afbeelding 2025-08-19 om 17 36 42

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.

Pluto optimizations (not in this package)

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:

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.

About

Demonstrating how to use plotlyjs in a Julia package that works in all environments

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages