Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Heptazhou committed May 5, 2024
1 parent 14075df commit d08495d
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 96 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ env:
jobs:
test:
runs-on: ${{ matrix.os }}
timeout-minutes: 30
timeout-minutes: 60
strategy:
fail-fast: false
matrix:
Expand All @@ -42,6 +42,7 @@ jobs:
- uses: julia-actions/julia-buildpkg@v1
with:
ignore-no-cache: true
localregistry: https://github.com/0h7z/0hjl.git
- uses: julia-actions/julia-runtest@v1
- uses: heptazhou/julia-codecov@v1
- uses: codecov/[email protected]
Expand Down
17 changes: 2 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
*.jl.cov
*.jl.*.cov
*.jl.mem
.DS_Store
examples/.ipynb_checkpoints/*
examples/meetup/.ipynb_checkpoints/*
deps/plotly-*
deps/build.log
deps/deps.jl

lcov.info
Manifest.toml
dev/
test/tmpplotsave.hdf5
/.benchmarkci
/benchmark/*.json
.vscode/
.CondaPkg/
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ FFMPEG = "c87230d0-a227-11e9-1b43-d7ebe4e7570a"
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
JLFzf = "1019f520-868f-41f5-a6de-eb00f4b6a39c"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
JSON5 = "275fdaeb-5887-4102-8704-6b08b28f797b"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Measures = "442fdcdd-2543-5da2-b0f3-8c86c306513e"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PlotThemes = "ccf2f8ad-2431-5c83-bf29-c5338b663b6a"
PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043"
Expand Down Expand Up @@ -51,11 +52,11 @@ Gaston = "1"
HDF5 = "0.16 - 0.17"
InspectDR = "0.5"
JLFzf = "0.1"
JSON = "0.21, 1"
LaTeXStrings = "1"
Latexify = "0.14 - 0.16"
Measures = "0.3"
NaNMath = "0.3, 1"
OrderedCollections = "1.5"
PGFPlots = "3"
PGFPlotsX = "1"
PlotThemes = "2, 3"
Expand Down
4 changes: 3 additions & 1 deletion src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("@max_m
@eval Base.Experimental.@max_methods 1
end

using OrderedCollections: OrderedDict as ODict
using Pkg, Dates, Printf, Statistics, Base64, LinearAlgebra, SparseArrays, Random
using PrecompileTools, Reexport, RelocatableFolders
using Base.Meta
Expand Down Expand Up @@ -42,7 +43,7 @@ import Downloads
import Showoff
import Unzip
import JLFzf
import JSON
import JSON5 as JSON

#! format: off
export
Expand All @@ -54,6 +55,7 @@ export
wrap,
theme,

Plot,
plot,
plot!,
attr!,
Expand Down
2 changes: 1 addition & 1 deletion src/backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ function _initialize_backend(pkg::PlotlyBackend)
_runtime_init(pkg)
catch err
if err isa ArgumentError
@warn "Failed to load integration with PlotlyBase & PlotlyKaleido." exception =
@debug "Failed to load integration with PlotlyBase & PlotlyKaleido." exception =
(err, catch_backtrace())
else
rethrow(err)
Expand Down
99 changes: 41 additions & 58 deletions src/backends/plotly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ end

plotly_font(font::Font, color = font.color) = KW(
:family => font.family,
:size => round(Int, 1.4font.pointsize),
:size => round(Int, 1.5font.pointsize),
:color => rgba_string(color),
)

Expand Down Expand Up @@ -191,13 +191,21 @@ function plotly_polaraxis(sp::Subplot, axis::Axis)
ax
end

function plotly_layout(plt::Plot)
# https://github.com/plotly/plotly.js/blob/master/src/plot_api/plot_config.js
function plotly_config(plt::Plot, responsive::Bool = true)
KW([:responsive => responsive, :showTips => false])
end

function plotly_layout(plt::Plot, responsive::Bool = true)
plotattributes_out = KW()

w, h = plt[:size]
plotattributes_out[:width], plotattributes_out[:height] = w, h
if !responsive
plotattributes_out[:width], plotattributes_out[:height] = w, h
end
plotattributes_out[:paper_bgcolor] = rgba_string(plt[:background_color_outside])
plotattributes_out[:margin] = KW(:l => 0, :b => 20, :r => 0, :t => 20)
plotattributes_out[:hoverlabel] = KW(:namelength => -1)

plotattributes_out[:annotations] = KW[]

Expand Down Expand Up @@ -1061,65 +1069,40 @@ plotly_url() =
end

function plotly_html_head(plt::Plot)
plotly = plotly_url()

include_mathjax = get(plt[:extra_plot_kwargs], :include_mathjax, "")

mathjax_file = if include_mathjax != "cdn"
"file://" * include_mathjax
else
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML"
end

mathjax_head = if isempty(include_mathjax)
""
else
"<script src=\"$mathjax_file\"></script>\n\t\t"
end

if isijulia()
mathjax_head
else
"$mathjax_head<script src=$(repr(plotly))></script>"
end
# https://cdnjs.com/libraries/mathjax
# https://cdnjs.com/libraries/plotly.js
"""
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg-full.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.32.0/plotly.min.js"></script>
"""
end

function plotly_html_body(plt, style = nothing)
if style === nothing
w, h = plt[:size]
style = "width:$(w)px;height:$(h)px;"
end

requirejs_prefix = requirejs_suffix = ""
if isijulia()
# require.js adds .js automatically
plotly_no_ext = plotly_url() |> splitext |> first

requirejs_prefix = """
requirejs.config({
paths: {
plotly: '$(plotly_no_ext)'
}
});
require(['plotly'], function (Plotly) {
"""
requirejs_suffix = "});"
end

uuid = UUIDs.uuid4()
html = """
<div id=\"$(uuid)\" style=\"$(style)\"></div>
<script>
$(requirejs_prefix)
$(js_body(plt, uuid))
$(requirejs_suffix)
</script>
"""
html
id = uuid4()
"""
<div id="$id"></div>
<!-- beautify ignore:start -->
<script>
/* beautify ignore:start */
$(js_body(plt, id) |> strip)
/* beautify ignore:end */
</script>
<!-- beautify ignore:end -->
"""
end

js_body(plt::Plot, uuid) =
"Plotly.newPlot('$(uuid)', $(plotly_series_json(plt)), $(plotly_layout_json(plt)));"
function js_body(plt::Plot, id)
s = JSON.json(["$id",
(sort! ODict)(plt |> plotly_series),
(sort! ODict)(plt |> plotly_layout),
(sort! ODict)(plt |> plotly_config),
], 4)
"""
Plotly.newPlot(
$(strip(("[\t\n]"), s))
)
"""
end

plotly_show_js(io::IO, plot::Plot) =
JSON.print(io, Dict(:data => plotly_series(plot), :layout => plotly_layout(plot)))
Expand All @@ -1131,6 +1114,6 @@ Base.showable(::MIME"application/prs.juno.plotpane+html", plt::Plot{PlotlyBacken
_show(io::IO, ::MIME"application/vnd.plotly.v1+json", plot::Plot{PlotlyBackend}) =
plotly_show_js(io, plot)

_show(io::IO, ::MIME"text/html", plt::Plot{PlotlyBackend}) = write(io, embeddable_html(plt))
_show(io::IO, ::MIME"text/html", plt::Plot{PlotlyBackend}) = write(io, standalone_html(plt))

_display(plt::Plot{PlotlyBackend}) = standalone_html_window(plt)
39 changes: 23 additions & 16 deletions src/backends/web.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@

# CREDIT: parts of this implementation were inspired by @joshday's PlotlyLocal.jl

standalone_html(
plt::AbstractPlot;
title::AbstractString = get(plt.attr, :window_title, "Plots.jl"),
) = """
<!DOCTYPE html>
<html>
<head>
<title>$title</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
$(html_head(plt))
</head>
<body>
$(html_body(plt))
</body>
</html>
"""
standalone_html(plt::AbstractPlot;
title::AbstractString = get(plt.attr, :window_title, "Plots.jl")) =
"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="color-scheme" content="dark light" />
<meta name="referrer" content="no-referrer" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>$title</title>
<link rel="dns-prefetch" href="https://cdnjs.cloudflare.com/" />
<style>
html, body { display: flex; height: 100%; overflow: hidden; }
html, body, body > div { flex: auto; margin: 0; padding: 0; }
</style>
$(html_head(plt) |> strip)
</head>
<body>
$(html_body(plt) |> strip)
</body>
</html>
"""

embeddable_html(plt::AbstractPlot) = html_head(plt) * html_body(plt)

Expand Down
4 changes: 4 additions & 0 deletions src/output.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ function savefig(plt::Plot, fn) # fn might be an `AbstractString` or an `Abstrac
end
savefig(fn) = savefig(current(), fn)

function Base.write(filename::AbstractString, p::AbstractPlot)
filesize(savefig(p, filename))
end

# ---------------------------------------------------------

"""
Expand Down
1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import ImageMagick
import FreeType # for `unicodeplots`
import LibGit2
import Aqua
import JSON

using VisualRegressionTests
using RecipesPipeline
Expand Down
2 changes: 1 addition & 1 deletion test/test_backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ else
end
push!(blacklist, 50) # NOTE: remove when github.com/jheinen/GR.jl/issues/507 is resolved

@testset "GR - reference images" begin
is_pkgeval() && @testset "GR - reference images" begin
Plots.with(:gr) do
# NOTE: use `ENV["VISUAL_REGRESSION_TESTS_AUTO"] = true;` to automatically replace reference images
@test backend() == Plots.GRBackend()
Expand Down

0 comments on commit d08495d

Please sign in to comment.