Skip to content

Commit 44225af

Browse files
authored
Merge pull request #36 from tkf/maxpayload
Add max_json_bytes to avoid sending large JSON
2 parents de9d797 + 289fc69 commit 44225af

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/ElectronDisplay.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Base.@kwdef mutable struct ElectronDisplayConfig
1010
showable = electron_showable
1111
single_window::Bool = false
1212
focus::Bool = true
13+
max_json_bytes::Int = 2^20
1314
end
1415

1516
"""
@@ -22,11 +23,13 @@ setconfig(
2223
showable = config.showable,
2324
single_window::Bool = config.single_window,
2425
focus::Bool = config.focus,
26+
max_json_bytes::Int = config.max_json_bytes,
2527
) =
2628
ElectronDisplayConfig(
2729
showable = showable,
2830
single_window = single_window,
2931
focus = focus,
32+
max_json_bytes = max_json_bytes,
3033
)
3134

3235
struct ElectronDisplayType <: Base.AbstractDisplay
@@ -57,6 +60,11 @@ Configuration for ElectronDisplay.
5760
5861
* `focus::Bool = true`: Focus the Electron window on `display` if `true`
5962
(default).
63+
64+
* `max_json_bytes::Int = $(ElectronDisplayConfig().max_json_bytes)`:
65+
Maximum size in bytes for which JSON representation is used. Otherwise,
66+
convert visualization locally in a binary form before sending it to the
67+
Electron display. Currently only Vega and Vega-Lite support this.
6068
"""
6169
const CONFIG = ElectronDisplayConfig()
6270

src/vega.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,22 @@ Base.displayable(d::ElectronDisplayType, ::MIME{Symbol("application/vnd.vega.v3+
44
Base.displayable(d::ElectronDisplayType, ::MIME{Symbol("application/vnd.vega.v4+json")}) = true
55
Base.displayable(d::ElectronDisplayType, ::MIME{Symbol("application/vnd.vega.v5+json")}) = true
66

7+
function _maybe_fallback_to(mime, d, x, payload)
8+
if sizeof(payload) > d.config.max_json_bytes
9+
@warn string(
10+
"The size of JSON representation (", sizeof(payload), ")",
11+
" exceeds `max_json_bytes = ", d.config.max_json_bytes, "`.",
12+
" Falling back to $mime."
13+
)
14+
return display(d, mime, x)
15+
end
16+
return nothing
17+
end
18+
719
function _display_vegalite(d, major_version_vegalite, major_version_vega, x)
820
payload = stringmime(MIME("application/vnd.vegalite.v$major_version_vegalite+json"), x)
21+
ans = _maybe_fallback_to(MIME"image/png"(), d, x, payload)
22+
ans === nothing || return ans
923

1024
html_page = """
1125
<html>
@@ -49,6 +63,8 @@ end
4963

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

5369
html_page = """
5470
<html>

test/construct_vega_specs.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,20 @@ vg5 = DummyDisplayable{MIME"application/vnd.vega.v5+json"}("""
318318
]
319319
}
320320
""")
321+
322+
# based on https://github.com/mathiasbynens/small
323+
dummy_png_bytes = UInt8[
324+
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
325+
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
326+
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00,
327+
0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x00, 0x01, 0x00, 0x00,
328+
0x05, 0x00, 0x01,
329+
]
330+
331+
struct DummyVegaLitePNGHybrid end
332+
Base.show(io::IO, ::MIME"application/vnd.vegalite.v3+json", ::DummyVegaLitePNGHybrid) =
333+
print(io, vl3)
334+
Base.show(io::IO, ::MIME"image/png", ::DummyVegaLitePNGHybrid) =
335+
write(io, dummy_png_bytes)
336+
337+
vl3png = DummyVegaLitePNGHybrid()

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ eldt = ElectronDisplay.ElectronDisplayType()
4040
@test electrondisplay(vg4) isa Electron.Window
4141
@test electrondisplay(vg5) isa Electron.Window
4242

43+
@test_logs(
44+
(:warn, r"The size of JSON representation.*exceeds.*max_json_bytes"),
45+
electrondisplay(vl3png; max_json_bytes=-1)::Electron.Window
46+
)
47+
4348
mdo = DummyDisplayable{MIME"text/markdown"}("""foo""")
4449
@test electrondisplay(mdo) isa Electron.Window
4550

0 commit comments

Comments
 (0)