Skip to content

Commit 4626422

Browse files
committed
display_data() function
1 parent 4bfda80 commit 4626422

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

docs/src/library/public.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ IJulia.load_string
4646
IJulia.readprompt
4747
IJulia.set_max_stdio
4848
```
49+
50+
51+
## Multimedia display
52+
```@docs
53+
IJulia.display_data
54+
```

src/display.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,51 @@ register_mime(x::AbstractVector{<:MIME}) = push!(ijulia_mime_types, Vector{Mime}
4545
register_jsonmime(x::Union{MIME, Vector{MIME}}) = push!(ijulia_jsonmime_types, x)
4646
register_jsonmime(x::AbstractVector{<:MIME}) = push!(ijulia_jsonmime_types, Vector{Mime}(x))
4747

48+
49+
# Check mime bundle dict key type and convert to string keys for JSON
50+
_format_mime_key(k::String) = k
51+
_format_mime_key(k::MIME) = string(k)
52+
_format_mime_key(k) = error("MIME bundle keys should be instances of String or MIME")
53+
_format_mimebundle(d::Dict{String}) = d
54+
_format_mimebundle(d::AbstractDict) = Dict(_format_mime_key(k) => v for (k, v) in pairs(d))
55+
56+
"""
57+
display_data(mime::Union{MIME, String}, data; metadata::Dict=Dict())
58+
display_data(mimebundle::Dict; metadata::Dict=Dict())
59+
60+
Publish raw data to be displayed on all Jupyter front ends.
61+
The first form of the function takes a single MIME type `mime` and encoded data
62+
`data`, which should be one of the following:
63+
64+
* A string representing plain-text data (e.g. for MIME types `text/html' or
65+
`application/javascript`) or base64-encoded binary data (e.g. for `image/png`).
66+
* Any other value which can be converted to a JSON string by `JSON.json`, including
67+
`JSON.JSONText`.
68+
69+
The second form of the function takes a MIME bundle, which is a dictionary of
70+
all possible representations of the data keyed by MIME type. The front end will
71+
automatically select the richest supported type to display.
72+
73+
`metadata` is an additional JSON dictionary describing the output. See the
74+
[jupyter client documentation](https://jupyter-client.readthedocs.io/en/latest/messaging.html#id6)
75+
for the keys defined by IPython, notable ones are `width::Int` and `height::Int`
76+
to control the size of displayed images.
77+
When using the second form of the function, the metadata dictionary may have
78+
additional sub-dictionaries keyed by specific MIME types.
79+
"""
80+
function display_data(mimebundle::AbstractDict, metadata::Dict=Dict())
81+
content = Dict("data" => _format_mimebundle(mimebundle), "metadata" => metadata)
82+
send_ipython(publish[], msg_pub(execute_msg, "display_data", content))
83+
end
84+
85+
function display_data(mime::Union{MIME, AbstractString}, data, metadata::Dict=Dict())
86+
mt = string(mime)
87+
d = Dict{String, Any}(mt => data)
88+
mt != "text/plain" && (d["text/plain"] = "Unable to display data with MIME type $mt") # Fallback
89+
display_data(d, metadata)
90+
end
91+
92+
4893
# return a String=>Any dictionary to attach as metadata
4994
# in Jupyter display_data and pyout messages
5095
metadata(x) = Dict()

0 commit comments

Comments
 (0)