Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max_json_bytes to avoid sending large JSON #36

Merged
merged 1 commit into from
Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/ElectronDisplay.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Base.@kwdef mutable struct ElectronDisplayConfig
showable = electron_showable
single_window::Bool = false
focus::Bool = true
max_json_bytes::Int = 2^20
end

"""
Expand All @@ -22,11 +23,13 @@ setconfig(
showable = config.showable,
single_window::Bool = config.single_window,
focus::Bool = config.focus,
max_json_bytes::Int = config.max_json_bytes,
) =
ElectronDisplayConfig(
showable = showable,
single_window = single_window,
focus = focus,
max_json_bytes = max_json_bytes,
)

struct ElectronDisplayType <: Base.AbstractDisplay
Expand Down Expand Up @@ -57,6 +60,11 @@ Configuration for ElectronDisplay.

* `focus::Bool = true`: Focus the Electron window on `display` if `true`
(default).

* `max_json_bytes::Int = $(ElectronDisplayConfig().max_json_bytes)`:
Maximum size in bytes for which JSON representation is used. Otherwise,
convert visualization locally in a binary form before sending it to the
Electron display. Currently only Vega and Vega-Lite support this.
"""
const CONFIG = ElectronDisplayConfig()

Expand Down
16 changes: 16 additions & 0 deletions src/vega.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@ Base.displayable(d::ElectronDisplayType, ::MIME{Symbol("application/vnd.vega.v3+
Base.displayable(d::ElectronDisplayType, ::MIME{Symbol("application/vnd.vega.v4+json")}) = true
Base.displayable(d::ElectronDisplayType, ::MIME{Symbol("application/vnd.vega.v5+json")}) = true

function _maybe_fallback_to(mime, d, x, payload)
if sizeof(payload) > d.config.max_json_bytes
@warn string(
"The size of JSON representation (", sizeof(payload), ")",
" exceeds `max_json_bytes = ", d.config.max_json_bytes, "`.",
" Falling back to $mime."
)
return display(d, mime, x)
end
return nothing
end

function _display_vegalite(d, major_version_vegalite, major_version_vega, x)
payload = stringmime(MIME("application/vnd.vegalite.v$major_version_vegalite+json"), x)
ans = _maybe_fallback_to(MIME"image/png"(), d, x, payload)
ans === nothing || return ans

html_page = """
<html>
Expand Down Expand Up @@ -49,6 +63,8 @@ end

function _display_vega(d, major_version, x)
payload = stringmime(MIME("application/vnd.vega.v$major_version+json"), x)
ans = _maybe_fallback_to(MIME"image/png"(), d, x, payload)
ans === nothing || return ans

html_page = """
<html>
Expand Down
17 changes: 17 additions & 0 deletions test/construct_vega_specs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,20 @@ vg5 = DummyDisplayable{MIME"application/vnd.vega.v5+json"}("""
]
}
""")

# based on https://github.com/mathiasbynens/small
dummy_png_bytes = UInt8[
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00,
0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x00, 0x01, 0x00, 0x00,
0x05, 0x00, 0x01,
]

struct DummyVegaLitePNGHybrid end
Base.show(io::IO, ::MIME"application/vnd.vegalite.v3+json", ::DummyVegaLitePNGHybrid) =
print(io, vl3)
Base.show(io::IO, ::MIME"image/png", ::DummyVegaLitePNGHybrid) =
write(io, dummy_png_bytes)

vl3png = DummyVegaLitePNGHybrid()
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ eldt = ElectronDisplay.ElectronDisplayType()
@test electrondisplay(vg4) isa Electron.Window
@test electrondisplay(vg5) isa Electron.Window

@test_logs(
(:warn, r"The size of JSON representation.*exceeds.*max_json_bytes"),
electrondisplay(vl3png; max_json_bytes=-1)::Electron.Window
)

mdo = DummyDisplayable{MIME"text/markdown"}("""foo""")
@test electrondisplay(mdo) isa Electron.Window

Expand Down