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

Move things to IJuliaCore.jl #1016

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "1.23.2"
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
IJuliaCore = "ccee42ee-1239-4771-b50a-e8cbc7e05233"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Expand All @@ -22,6 +23,7 @@ ZMQ = "c2297ded-f4af-51ae-bb23-16f91089e4e1"

[compat]
Conda = "1"
IJuliaCore = "1"
JSON = "0.18,0.19,0.20,0.21,1"
MbedTLS = "0.5,0.6,0.7,1"
SoftGlobalScope = "1"
Expand Down
34 changes: 6 additions & 28 deletions src/IJulia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ The `IJulia` module is used in three ways
module IJulia
export notebook, jupyterlab, installkernel

using ZMQ, JSON, SoftGlobalScope
using ZMQ, JSON, SoftGlobalScope, IJuliaCore
using IJuliaCore: @vprintln, orig_stdin, orig_stdout, orig_stderr, display_dict,
register_mime, register_jsonmime
import Base.invokelatest
import Dates
using Dates: now
Expand All @@ -49,23 +51,7 @@ const depfile = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")
isfile(depfile) || error("IJulia not properly installed. Please run Pkg.build(\"IJulia\")")
include(depfile) # generated by Pkg.build("IJulia")

#######################################################################
# Debugging IJulia

# in the Jupyter front-end, enable verbose output via IJulia.set_verbose()
verbose = IJULIA_DEBUG
"""
set_verbose(v=true)

This function enables (or disables, for `set_verbose(false)`) verbose
output from the IJulia kernel, when called within a running notebook.
This consists of log messages printed to the terminal window where
`jupyter` was launched, displaying information about every message sent
or received by the kernel. Used for debugging IJulia.
"""
function set_verbose(v::Bool=true)
global verbose = v
end
IJuliaCore.verbose = IJULIA_DEBUG

"""
`inited` is a global variable that is set to `true` if the IJulia
Expand All @@ -75,16 +61,8 @@ whether you are in an IJulia notebook, therefore, you can check
"""
inited = false

# set this to false for debugging, to disable stderr redirection
"""
The IJulia kernel captures all [stdout and stderr](https://en.wikipedia.org/wiki/Standard_streams)
output and redirects it to the notebook. When debugging IJulia problems,
however, it can be more convenient to *not* capture stdout and stderr output
(since the notebook may not be functioning). This can be done by editing
`IJulia.jl` to set `capture_stderr` and/or `capture_stdout` to `false`.
"""
const capture_stdout = true
const capture_stderr = !IJULIA_DEBUG
# TODO Reenable something like this again
# const capture_stderr = !IJULIA_DEBUG

set_current_module(m::Module) = current_module[] = m
const current_module = Ref{Module}(Main)
Expand Down
121 changes: 0 additions & 121 deletions src/display.jl
Original file line number Diff line number Diff line change
@@ -1,128 +1,7 @@

# define our own method to avoid type piracy with Base.showable
_showable(a::AbstractVector{<:MIME}, x) = any(m -> showable(m, x), a)
_showable(m, x) = showable(m, x)

"""
A vector of MIME types (or vectors of MIME types) that IJulia will try to
render. IJulia will try to render every MIME type specified in the first level
of the vector. If a vector of MIME types is specified, IJulia will include only
the first MIME type that is renderable (this allows for the expression of
priority and exclusion of redundant data).

For example, since "text/plain" is specified as a first-child of the array,
IJulia will always try to include a "text/plain" representation of anything that
is displayed. Since markdown and html are specified within a sub-vector, IJulia
will always try to render "text/markdown", and will only try to render
"text/html" if markdown isn't possible.
"""
const ijulia_mime_types = Vector{Union{MIME, AbstractVector{MIME}}}([
MIME("text/plain"),
MIME("image/svg+xml"),
[MIME("image/png"),MIME("image/jpeg")],
[
MIME("text/markdown"),
MIME("text/html"),
],
MIME("text/latex"),
])

"""
MIME types that when rendered (via stringmime) return JSON data. See
`ijulia_mime_types` for a description of how MIME types are selected.

This is necessary to embed the JSON as is in the displaydata bundle (rather than
as stringify'd JSON).
"""
const ijulia_jsonmime_types = Vector{Union{MIME, Vector{MIME}}}([
[[MIME("application/vnd.vegalite.v$n+json") for n in 4:-1:2]...,
[MIME("application/vnd.vega.v$n+json") for n in 5:-1:3]...],
MIME("application/vnd.dataresource+json"), MIME("application/vnd.plotly.v1+json")
])

register_mime(x::Union{MIME, Vector{MIME}})= push!(ijulia_mime_types, x)
register_mime(x::AbstractVector{<:MIME}) = push!(ijulia_mime_types, Vector{Mime}(x))
register_jsonmime(x::Union{MIME, Vector{MIME}}) = push!(ijulia_jsonmime_types, x)
register_jsonmime(x::AbstractVector{<:MIME}) = push!(ijulia_jsonmime_types, Vector{Mime}(x))

# return a String=>Any dictionary to attach as metadata
# in Jupyter display_data and pyout messages
metadata(x) = Dict()

"""
Generate the preferred MIME representation of x.

Returns a tuple with the selected MIME type and the representation of the data
using that MIME type.
"""
function display_mimestring(mime_array::Vector{MIME}, x)
for m in mime_array
if _showable(m, x)
return display_mimestring(m, x)
end
end
error("No displayable MIME types in mime array.")
end

display_mimestring(m::MIME, x) = (m, limitstringmime(m, x))

# text/plain output must have valid Unicode data to display in Jupyter
function display_mimestring(m::MIME"text/plain", x)
s = limitstringmime(m, x)
return m, (isvalid(s) ? s : "(binary data)")
end

"""
Generate the preferred json-MIME representation of x.

Returns a tuple with the selected MIME type and the representation of the data
using that MIME type (as a `JSONText`).
"""
function display_mimejson(mime_array::Vector{MIME}, x)
for m in mime_array
if _showable(m, x)
return display_mimejson(m, x)
end
end
error("No displayable MIME types in mime array.")
end

display_mimejson(m::MIME, x) = (m, JSON.JSONText(limitstringmime(m, x)))

"""
Generate a dictionary of `mime_type => data` pairs for all registered MIME
types. This is the format that Jupyter expects in display_data and
execute_result messages.
"""
function display_dict(x)
data = Dict{String, Union{String, JSONText}}()
for m in ijulia_mime_types
try
if _showable(m, x)
mime, mime_repr = display_mimestring(m, x)
data[string(mime)] = mime_repr
end
catch
if m == MIME("text/plain")
rethrow() # text/plain is required
end
end
end

for m in ijulia_jsonmime_types
try
if _showable(m, x)
mime, mime_repr = display_mimejson(m, x)
data[string(mime)] = mime_repr
end
catch
end
end

return data

end

# queue of objects to display at end of cell execution
const displayqueue = Any[]

Expand Down
3 changes: 0 additions & 3 deletions src/execute_request.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import Pkg

# global variable so that display can be done in the correct Msg context
execute_msg = Msg(["julia"], Dict("username"=>"jlkernel", "session"=>uuid4()), Dict())
# global variable tracking the number of bytes written in the current execution
# request
const stdio_bytes = Ref(0)

import REPL: helpmode

Expand Down
11 changes: 3 additions & 8 deletions src/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ const IJulia_RNG = seed!(Random.MersenneTwister(0))
import UUIDs
uuid4() = string(UUIDs.uuid4(IJulia_RNG))

const orig_stdin = Ref{IO}()
const orig_stdout = Ref{IO}()
const orig_stderr = Ref{IO}()
const SOFTSCOPE = Ref{Bool}()
function __init__()
seed!(IJulia_RNG)
Expand All @@ -25,8 +22,6 @@ const requests = Ref{Socket}()
const control = Ref{Socket}()
const heartbeat = Ref{Socket}()
const profile = Dict{String,Any}()
const read_stdout = Ref{Base.PipeEndpoint}()
const read_stderr = Ref{Base.PipeEndpoint}()
const socket_locks = Dict{Socket,ReentrantLock}()

# similar to Pkg.REPLMode.MiniREPL, a minimal REPL-like emulator
Expand Down Expand Up @@ -100,13 +95,13 @@ function init(args)
start_heartbeat(heartbeat[])
if capture_stdout
read_stdout[], = redirect_stdout()
redirect_stdout(IJuliaStdio(stdout,"stdout"))
redirect_stdout(IJuliaStdio(stdout,send_callback,"stdout"))
end
if capture_stderr
read_stderr[], = redirect_stderr()
redirect_stderr(IJuliaStdio(stderr,"stderr"))
redirect_stderr(IJuliaStdio(stderr,send_callback,"stderr"))
end
redirect_stdin(IJuliaStdio(stdin,"stdin"))
redirect_stdin(IJuliaStdio(stdin,send_callback,"stdin"))
minirepl[] = MiniREPL(TextDisplay(stdout))

logger = Base.CoreLogging.SimpleLogger(Base.stderr)
Expand Down
35 changes: 0 additions & 35 deletions src/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,6 @@ const ipy_mime = [
"application/javascript"
]

# need special handling for showing a string as a textmime
# type, since in that case the string is assumed to be
# raw data unless it is text/plain
israwtext(::MIME, x::AbstractString) = true
israwtext(::MIME"text/plain", x::AbstractString) = false
israwtext(::MIME, x) = false

InlineIOContext(io, KVs::Pair...) = IOContext(
io,
:limit=>true, :color=>true, :jupyter=>true,
KVs...
)

# convert x to a string of type mime, making sure to use an
# IOContext that tells the underlying show function to limit output
function limitstringmime(mime::MIME, x)
buf = IOBuffer()
if istextmime(mime)
if israwtext(mime, x)
return String(x)
else
show(InlineIOContext(buf), mime, x)
end
else
b64 = Base64EncodePipe(buf)
if isa(x, Vector{UInt8})
write(b64, x) # x assumed to be raw binary data
else
show(InlineIOContext(b64), mime, x)
end
close(b64)
end
return String(take!(buf))
end

for mime in ipy_mime
@eval begin
function display(d::InlineDisplay, ::MIME{Symbol($mime)}, x)
Expand Down
2 changes: 1 addition & 1 deletion src/kernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pushdisplay(IJulia.InlineDisplay())
ccall(:jl_exit_on_sigint, Cvoid, (Cint,), 0)

println(IJulia.orig_stdout[], "Starting kernel event loops.")
IJulia.watch_stdio()
IJulia.watch_stdio(send_callback)

# workaround JuliaLang/julia#4259
delete!(task_local_storage(),:SOURCE_PATH)
Expand Down
Loading