Skip to content

Commit 7b94150

Browse files
twavvshashi
authored andcommitted
Add InlineIOContext with Jupyter property. (#876)
* Add InlineIOContext with Jupyter property. * Fix test case.
1 parent 0df1f5f commit 7b94150

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/inline.jl

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

29+
InlineIOContext(io, KVs::Pair...) = IOContext(
30+
io,
31+
:limit=>true, :color=>true, :jupyter=>true,
32+
KVs...
33+
)
34+
2935
# convert x to a string of type mime, making sure to use an
3036
# IOContext that tells the underlying show function to limit output
3137
function limitstringmime(mime::MIME, x)
@@ -34,14 +40,14 @@ function limitstringmime(mime::MIME, x)
3440
if israwtext(mime, x)
3541
return String(x)
3642
else
37-
show(IOContext(buf, :limit=>true, :color=>true), mime, x)
43+
show(InlineIOContext(buf), mime, x)
3844
end
3945
else
4046
b64 = Base64EncodePipe(buf)
4147
if isa(x, Vector{UInt8})
4248
write(b64, x) # x assumed to be raw binary data
4349
else
44-
show(IOContext(b64, :limit=>true, :color=>true), mime, x)
50+
show(InlineIOContext(b64), mime, x)
4551
end
4652
close(b64)
4753
end

test/inline.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Test
2+
import IJulia: InlineIOContext
3+
4+
@testset "Custom Jupyter inline display" begin
5+
@eval struct TestDataType
6+
payload
7+
end
8+
9+
function Base.show(io::IO, m::MIME"text/plain", data::TestDataType)
10+
print(io, "TestDataType: ")
11+
if get(io, :jupyter, false)
12+
print(io, "Jupyter: ")
13+
end
14+
Base.show(io, m, data.payload)
15+
end
16+
17+
data = TestDataType("foo")
18+
buf = IOBuffer()
19+
20+
Base.show(buf, MIME("text/plain"), data)
21+
@test String(take!(buf)) == "TestDataType: \"foo\""
22+
23+
Base.show(InlineIOContext(buf), MIME("text/plain"), data)
24+
@test String(take!(buf)) == "TestDataType: Jupyter: \"foo\""
25+
end

test/runtests.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
for file in ["install.jl","comm.jl", "msg.jl", "execute_request.jl", "stdio.jl"]
1+
const TEST_FILES = [
2+
"install.jl", "comm.jl", "msg.jl", "execute_request.jl", "stdio.jl",
3+
"inline.jl",
4+
]
5+
6+
for file in TEST_FILES
27
println(file)
38
include(file)
49
end

0 commit comments

Comments
 (0)