Internals
Initialization
IJulia.init — Functioninit(args, kernel)Initialize a kernel. args may either be empty or have one element containing the path to an existing connection file. If args is empty a connection file will be generated.
Messaging
IJulia.Msg — TypeIPython message struct.
IJulia.msg_header — Functionmsg_header(m::Msg, msg_type::String)Create a header for a Msg.
IJulia.send_ipython — Functionsend_ipython(socket, kernel, m::Msg)Send a message m. This will lock socket.
IJulia.recv_ipython — Functionrecv_ipython(socket, kernel)Wait for and get a message. This will lock socket.
IJulia.set_cur_msg — FunctionJupyter associates cells with message headers. Once a cell's execution state has been set as to idle, it will silently drop stream messages (i.e. output to stdout and stderr) - see https://github.com/jupyter/notebook/issues/518. When using Interact, and a widget's state changes, a new message header is sent to the IJulia kernel, and while Reactive is updating Signal graph state, it's execution state is busy, meaning Jupyter will not drop stream messages if Interact can set the header message under which the stream messages will be sent. Hence the need for this function.
IJulia.send_status — Functionsend_status(state::AbstractString, kernel, parent_msg::Msg=execute_msg)Publish a status message.
Request handlers
IJulia.connect_request — Functionconnect_request(socket, kernel, msg)Handle a connect request.
IJulia.execute_request — Functionexecute_request(socket, kernel, msg)Handle a execute request. This will execute Julia code, along with Pkg and shell commands.
IJulia.shutdown_request — Functionshutdown_request(socket, kernel, msg)Handle a shutdown request. After sending the reply this will exit the process.
IJulia.interrupt_request — Functioninterrupt_request(socket, kernel, msg)Handle a interrupt request. This will throw an InterruptException to the currently executing request handler.
IJulia.inspect_request — Functioninspect_request(socket, kernel, msg)Handle a introspection request.
IJulia.history_request — Functionhistory_request(socket, kernel, msg)Handle a history request. This is currently only a dummy implementation that doesn't actually do anything.
IJulia.complete_request — Functioncomplete_request(socket, kernel, msg)Handle a completion request.
IJulia.kernel_info_request — Functionkernel_info_request(socket, kernel, msg)Handle a kernel info request.
IJulia.is_complete_request — Functionis_complete_request(socket, kernel, msg)Handle a completeness request.
Event loop
IJulia.eventloop — Functioneventloop(socket, kernel)Generic event loop for one of the kernel sockets.
IJulia.waitloop — Functionwaitloop(kernel)Main loop of a kernel. Runs the event loops for the control and shell sockets (note: in IJulia the shell socket is called requests).
IO
IJulia.IJuliaStdio — TypeWrapper type around redirected stdio streams, both for overloading things like flush and so that we can set properties like color.
IJulia.watch_stream — Functionwatch_stream(rd::IO, name::AbstractString)Continually read from (size limited) Libuv/OS buffer into an IObuffer to avoid problems when the Libuv/OS buffer gets full (https://github.com/JuliaLang/julia/issues/8789). Send data immediately when buffer contains more than max_bytes bytes. Otherwise, if data is available it will be sent every stream_interval seconds (see the Timer's set up in watch_stdio). Truncate the output to max_output_per_request bytes per execution request since excessive output can bring browsers to a grinding halt.
Multimedia display
IJulia.InlineDisplay — TypeStruct to dispatch on for inline display.
IJulia.InlineIOContext — FunctionInlineIOContext(io, KVs::Pair...)Create an IOContext for inline display.
IJulia.ipy_mime — ConstantSupported MIME types for inline display in IPython, in descending order of preference (descending "richness").
IJulia.ijulia_mime_types — ConstantA 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.
IJulia.ijulia_jsonmime_types — ConstantMIME 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).
IJulia.limitstringmime — Functionlimitstringmime(mime::MIME, x, forcetext=false)Convert x to a string of type mime, making sure to use an IOContext that tells the underlying show function to limit output.
IJulia.israwtext — FunctionNeed 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.
IJulia.display_dict — FunctionGenerate 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.
IJulia.display_mimejson — FunctionGenerate 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).
IJulia.display_mimestring — FunctionGenerate the preferred MIME representation of x.
Returns a tuple with the selected MIME type and the representation of the data using that MIME type.
IJulia.register_mime — Functionregister_mime(x::Union{MIME, Vector{MIME}})
register_mime(x::AbstractVector{<:MIME})Register a new MIME type.
IJulia.register_jsonmime — Functionregister_jsonmime(x::Union{MIME, Vector{MIME}})
register_jsonmime(x::AbstractVector{<:MIME})Register a new JSON MIME type.
Jupyter
IJulia.find_jupyter_subcommand — Functionfind_jupyter_subcommand(subcommand::AbstractString, port::Union{Nothing,Int}=nothing)Return a Cmd for the program subcommand. If the program is jupyter or jupyterlab it may prompt the user to install it.
IJulia.launch — Functionlaunch(cmd, dir, detached, verbose)Run cmd in dir. If detached is false it will not wait for the command to finish. If verbose is true then the stdout/stderr from the cmd process will be echoed to stdout/stderr.
Debugging
IJulia.set_verbose — Functionset_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.
Utility
IJulia.num_utf8_trailing — FunctionIf d ends with an incomplete UTF8-encoded character, return the number of trailing incomplete bytes. Otherwise, return 0.
IJulia.get_token — Functionget_token(code, pos)Given a string and a cursor position, find substring to request help on by:
- Searching backwards for the closest token (may be invalid)
- Keep searching backwards until we find an token before an unbalanced '(' a. If (1) is not valid, store the first valid token b. We assume a token before an unbalanced '(' is a function
- If we find a possible function token, return this token.
- Otherwise, return the last valid token
Important Note
Tokens are chosen following several empirical observations instead of rigorous rules. We assume that the first valid token before left-imbalanced (more '(' than ')') parentheses is the function "closest" to cursor. The following examples use '|' to denote cursor, showing observations on parentheses.
f()|has balanced parentheses with nothing within, thusfis the desired token.f(|)has imbalanced parentheses, thusfis the desired token.f(x|, y)gives tokensxandf.xhas balanced parentheses, whilefis left-imbalanced.fis desired.f(x)|returnsff(x, y)|returnsf.f((x|))returnsf, as expectedf(x, (|y))returnsf. This is a hack, as I deductcrossed_parentheseswhenever a separator is encountered, clamped to 0! Otherwise,xwould be returned.f(x, (y|)),f(x, (y)|), andf(x, (y))|all behave as above. Arbitrary nesting of tuples should not cause misbehavior.expr1 ; expr2, cursor inexpr2never causes search inexpr1
TODO: detect operators? More robust parsing using the Julia parser instead of string hacks?
IJulia.get_previous_token — Functionget_previous_token(code, pos, crossed_parentheses)Given a string and a cursor position, find substring corresponding to previous token. crossed_parentheses:Int keeps track of how many parentheses have been crossed. A pair of parentheses yields 0 crossing; a '(' add 1; a ')' subtracts 1.
Returns (startpos, endpos, crossed_parentheses, stop)
startposis the start position of the closest potential token beforepos.endposis end position if said token is can be valid identifier, or-1otherwisecrossed_parenthesesis the new count for parentheses.stopis true if ';' is hit, denoting the beginning of a clause.