Skip to content

Commit 89f4443

Browse files
committed
display_data() function
1 parent cc2a9bf commit 89f4443

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-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/inline.jl

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,101 @@ israwtext(::MIME, x::AbstractString) = true
2626
israwtext(::MIME"text/plain", x::AbstractString) = false
2727
israwtext(::MIME, x) = false
2828

29+
30+
# Check mime bundle dict key type and convert to string keys for JSON
31+
_format_mime_key(k::String) = k
32+
_format_mime_key(k::MIME) = string(k)
33+
_format_mime_key(k) = error("MIME bundle keys should be instances of String or MIME")
34+
_format_mimebundle(d::Dict{String}) = d
35+
_format_mimebundle(d::AbstractDict) = Dict(_format_mime_key(k) => v for (k, v) in pairs(d))
36+
37+
"""
38+
display_data(mime::Union{MIME, String}, data, metadata::AbstractDict=Dict())
39+
display_data(mimebundle::AbstractDict, metadata::AbstractDict=Dict())
40+
41+
Publish encoded multimedia data to be displayed all Jupyter front ends.
42+
43+
This is a low-level function which acts as a direct interface to Jupyter's display system. It does
44+
not perform any additional processing on the input data, use `display(::IJulia.InlineDisplay, x)` to
45+
calculate and display the multimedia representation of an arbitrary object `x`.
46+
47+
In the Jupyter notebook/lab the data will be displayed in the output area of the cell being executed.
48+
This will appear in addition to the display of the cell's execution result, if any. Multiple calls
49+
to this function within the same cell will result in multiple displays within the same output area.
50+
51+
The first form of the function takes a single MIME type `mime` and encoded data `data`, which should
52+
be one of the following:
53+
54+
* A string containing text data (e.g. for MIME types `text/html` or `application/javascript`) or
55+
base64-encoded binary data (e.g. for `image/png`).
56+
* Any other value which can be converted to a JSON string by `JSON.json`, including `JSON.JSONText`.
57+
58+
The second form of the function takes a MIME bundle, which is a dictionary containing multiple
59+
representations of the data keyed by MIME type. The front end will automatically select the richest
60+
supported type to display.
61+
62+
`metadata` is an additional JSON dictionary describing the output. See the
63+
[jupyter client documentation](https://jupyter-client.readthedocs.io/en/latest/messaging.html#display-data)
64+
for the keys used by IPython, notable ones are `width::Int` and `height::Int` to control the size
65+
of displayed images. When using the second form of the function the argument should be a dictionary
66+
of dictionaries keyed by MIME type.
67+
68+
69+
# Examples
70+
71+
Displaying a MIME bundle containing rich text in three different formats (the front end
72+
will select only the richest type to display):
73+
74+
```julia
75+
bundle = Dict(
76+
"text/plain" => "text/plain: foo bar baz",
77+
"text/html" => "<code>text/html</code>: foo <strong>bar</strong> <em>baz</em>",
78+
"text/markdown" => "`text/markdown`: foo **bar** *baz*",
79+
)
80+
81+
IJulia.display_data(bundle)
82+
```
83+
84+
Display each of these types individually:
85+
86+
```julia
87+
for (mime, data) in pairs(bundle)
88+
IJulia.display_data(mime, data)
89+
end
90+
```
91+
92+
Displaying base64-encoded PNG image data:
93+
94+
```julia
95+
using Base64
96+
97+
data = open(read, "example.png") # Array{UInt8}
98+
data_enc = base64encode(data) # String
99+
100+
IJulia.display_data("image/png", data_enc)
101+
```
102+
103+
Adjust the size of the displayed image by passing a metadata dictionary:
104+
105+
```julia
106+
IJulia.display_data("image/png", data_enc, Dict("width" => 800, "height" => 600))
107+
```
108+
"""
109+
function display_data(mimebundle::AbstractDict, metadata::AbstractDict=Dict())
110+
content = Dict("data" => _format_mimebundle(mimebundle), "metadata" => _format_mimebundle(metadata))
111+
flush_all() # so that previous stream output appears in order
112+
send_ipython(publish[], msg_pub(execute_msg, "display_data", content))
113+
end
114+
115+
function display_data(mime::Union{MIME, AbstractString}, data, metadata::AbstractDict=Dict())
116+
mt = string(mime)
117+
d = Dict{String, Any}(mt => data)
118+
md = Dict{String, Any}(mt => metadata)
119+
mt != "text/plain" && (d["text/plain"] = "Unable to display data with MIME type $mt") # Fallback
120+
display_data(d, md)
121+
end
122+
123+
29124
InlineIOContext(io, KVs::Pair...) = IOContext(
30125
io,
31126
:limit=>true, :color=>true, :jupyter=>true,

0 commit comments

Comments
 (0)