jupyter

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jupyter serves the Jupyter messaging protocol over ZeroMQ for a kernel: the five channels a connection file names, the signing of every message, and the request kinds a front end sends, each answered by an Engine. The engine over the REPL session is in this package too.

Index

Constants

View Source
const ProtocolVersion = "5.3"

ProtocolVersion is the version of the Jupyter messaging protocol the kernel speaks.

Variables

View Source
var (
	ErrNoDelimiter  = errors.New("no <IDS|MSG> delimiter")
	ErrShortMessage = errors.New("fewer than four frames after the delimiter")
	ErrBadSignature = errors.New("signature does not verify")
)

Errors a wire message is refused for.

Functions

func Stderr

func Stderr() *log.Logger

Stderr is where the kernel logs when asked to; the launcher decides.

Types

type Completion

type Completion struct {
	Matches     []string
	CursorStart int
	CursorEnd   int
}

Completion is the answer to a completion request: the candidates, and the span of the code they replace, as rune offsets.

type ConnectionInfo

type ConnectionInfo struct {
	Transport       string `json:"transport"`
	IP              string `json:"ip"`
	ShellPort       int    `json:"shell_port"`
	IOPubPort       int    `json:"iopub_port"`
	StdinPort       int    `json:"stdin_port"`
	ControlPort     int    `json:"control_port"`
	HBPort          int    `json:"hb_port"`
	Key             string `json:"key"`
	SignatureScheme string `json:"signature_scheme"`
	KernelName      string `json:"kernel_name,omitempty"`
}

ConnectionInfo is what a Jupyter connection file holds: where each channel listens, and the key messages are signed with.

func ReadConnectionFile

func ReadConnectionFile(path string) (ConnectionInfo, error)

ReadConnectionFile reads the connection file a front end wrote for the kernel.

func (ConnectionInfo) Validate

func (c ConnectionInfo) Validate() error

Validate reports a connection the kernel cannot serve: a transport other than TCP or IPC, a channel without a port, or a signature scheme other than HMAC-SHA256 when a key is set.

type Engine

type Engine interface {
	// Execute runs one cell, writing what it produces to out, and reports a
	// failure as an *ExecError; out.Error is the kernel's to call.
	Execute(code string, out Output) error
	Complete(code string, cursor int) Completion
	IsComplete(code string) (IsCompleteStatus, string)
	Inspect(code string, cursor int, detail int) Inspection
	// Interrupt stops the cell running, if one is.
	Interrupt()
	// Shutdown ends the engine; restart says whether another is to follow.
	Shutdown(restart bool)
}

Engine is what the kernel asks of the language: it runs cells and answers what a front end asks about code. The kernel calls Execute, Complete, IsComplete and Inspect one at a time; Interrupt may be called while Execute runs, from another goroutine.

type ExecError

type ExecError struct {
	Name      string
	Value     string
	Traceback []string
}

ExecError is how an engine reports a cell that failed: the name of the error, what it says, and the traceback lines.

func (*ExecError) Error

func (e *ExecError) Error() string
type Header struct {
	MsgID    string `json:"msg_id"`
	Session  string `json:"session"`
	Username string `json:"username"`
	Date     string `json:"date"`
	MsgType  string `json:"msg_type"`
	Version  string `json:"version"`
}

Header identifies a message: who sent it, in which session, and what kind it is.

type HelpLink struct {
	Text string `json:"text"`
	URL  string `json:"url"`
}

HelpLink is one entry of a front end's help menu.

type Info

type Info struct {
	Implementation        string
	ImplementationVersion string
	Language              LanguageInfo
	Banner                string
	HelpLinks             []HelpLink
}

Info is what the kernel tells a front end about itself.

type Inspection

type Inspection struct {
	Found bool
	Data  MIMEBundle
}

Inspection is the answer to an inspection request: whether the cursor is on something known, and what to show about it.

type IsCompleteStatus

type IsCompleteStatus string

IsCompleteStatus is the answer to whether a cell is complete.

const (
	Complete   IsCompleteStatus = "complete"
	Incomplete IsCompleteStatus = "incomplete"
	Invalid    IsCompleteStatus = "invalid"
	Unknown    IsCompleteStatus = "unknown"
)

The answers the protocol admits.

type Kernel

type Kernel struct {
	// contains filtered or unexported fields
}

Kernel serves one connection: the five channels, each on its own socket, shell requests answered in order and control requests beside them.

func New

func New(info Info, conn ConnectionInfo, engine Engine, logger *log.Logger) *Kernel

New prepares a kernel over the connection, answering with the engine.

func (*Kernel) Interrupt

func (k *Kernel) Interrupt()

Interrupt stops the cell running, as a SIGINT from the front end does.

func (*Kernel) Listen

func (k *Kernel) Listen(ctx context.Context) (ConnectionInfo, error)

Listen binds the five channels and returns the connection they are bound on. A TCP channel whose port is 0 is bound on a free port, and the connection returned names the port taken; any other port is bound as given, so a port already in use fails here. ctx ending closes the sockets, whether or not Serve was called. A binding not yet served must be served before the kernel can listen again.

func (*Kernel) Run

func (k *Kernel) Run(ctx context.Context) (restart bool, err error)

Run is Listen then Serve: it binds the channels and serves them until a shutdown request arrives or ctx ends, reporting whether the front end asked for a restart.

func (*Kernel) Serve

func (k *Kernel) Serve() (restart bool, err error)

Serve answers on the channels Listen bound until a shutdown request arrives or the context given to Listen ends. It reports whether the front end asked for a restart.

type LanguageInfo

type LanguageInfo struct {
	Name           string `json:"name"`
	Version        string `json:"version"`
	MIMEType       string `json:"mimetype"`
	FileExtension  string `json:"file_extension"`
	PygmentsLexer  string `json:"pygments_lexer,omitempty"`
	CodeMirrorMode string `json:"codemirror_mode,omitempty"`
}

LanguageInfo is what the kernel tells a front end about its language.

type MIMEBundle

type MIMEBundle map[string]any

MIMEBundle is the data of one output, keyed by MIME type, each the representation a front end may pick. Text values are strings; a JSON value is any value that marshals to the JSON a front end expects.

type Message

type Message struct {
	// Identities are the routing frames a ROUTER socket prepends, echoed back on
	// the reply so it reaches the sender.
	Identities   [][]byte
	Header       Header
	ParentHeader Header
	Metadata     map[string]any
	Content      map[string]any
	Buffers      [][]byte
}

Message is one Jupyter message, decoded from or encoded to its wire frames.

type Output

type Output interface {
	// Stream writes text to a stream: "stdout" or "stderr".
	Stream(name, text string)
	// Display shows a rich output.
	Display(data MIMEBundle, metadata map[string]any)
	// Result shows the value of the cell: the last expression's.
	Result(data MIMEBundle)
	// Error reports a failure, with the lines a traceback shows for it.
	Error(name, value string, traceback []string)
}

Output is where an engine writes what a cell produces, in the order produced.

type REPLEngine

type REPLEngine struct {
	// contains filtered or unexported fields
}

REPLEngine runs cells against a REPL session: each cell is split into the statements the prompt would read from it, and each statement is submitted as the prompt submits it, so a notebook and a terminal session behave alike.

func NewREPLEngine

func NewREPLEngine(session *repl.Session) *REPLEngine

NewREPLEngine is an engine over the session.

func (*REPLEngine) Complete

func (e *REPLEngine) Complete(code string, cursor int) Completion

Complete answers completion at the cursor: the prompt completes a line, so the cursor's line is completed, and the span replaced is spelled back in the cell's rune offsets.

func (*REPLEngine) Execute

func (e *REPLEngine) Execute(code string, out Output) error

Execute runs the cell's statements in order and stops at the first that fails, as a cell of any other kernel does.

func (*REPLEngine) Inspect

func (e *REPLEngine) Inspect(code string, cursor int, detail int) Inspection

Inspect describes the name under the cursor as %print does.

func (*REPLEngine) Interrupt

func (e *REPLEngine) Interrupt()

Interrupt stops the statement running.

func (*REPLEngine) IsComplete

func (e *REPLEngine) IsComplete(code string) (IsCompleteStatus, string)

IsComplete reports whether the cell is ready to run: a declaration left open by a brace, parenthesis or bracket is not, and its continuation is indented.

func (*REPLEngine) Session

func (e *REPLEngine) Session() *repl.Session

Session is the session the engine runs.

func (*REPLEngine) Shutdown

func (e *REPLEngine) Shutdown(bool)

Shutdown has nothing to release: the session is the process's.

func (*REPLEngine) String

func (e *REPLEngine) String() string

String names the engine.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL