kernel-base
The base of all Basthon kernels, used by the frontends: a kernel runs the user's code (in a worker, or in the main thread for the legacy Python kernel), receives its input and dispatches its outputs as events (see KernelBase).
Classes
DownloadError
A kernel's start failed because a file it needs could not be downloaded (e.g. the network cut the download). Its name survives the transfer from the worker (unlike instanceof): the frontends can test it to show a network problem rather than an outdated browser.
Extends
Error
Constructors
Constructor
new DownloadError(message): DownloadError;Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | What could not be downloaded, and why. |
Returns
Overrides
Error.constructor
KernelBase
The API of all Basthon's kernels, used by the frontends (console, notebook...). A kernel is created (usually by a KernelLoader), started with init, then runs code with execute and reports through events:
| Event | Dispatched when | Data (besides the request's data) |
|---|---|---|
eval.output | the code prints | stream ("stdout" or "stderr"), content: the text |
eval.display | the code displays something | display_type and content (see below) |
eval.clear-output | the code clears its output | content.wait |
eval.input | the code waits for an input | content.prompt, content.password, resolve(answer) |
eval.input-cancel | the kernel gave up waiting for an input | the eval.input data |
eval.finished | the evaluation succeeded | execution_count, result (a MIME bundle, if any) |
eval.error | the evaluation failed | execution_count, error (a KernelError) |
validation.log | a self-assessment validation ran (Python) | the validation's log |
The data of an eval.display event is either a MIME bundle (display_type "multiple", content maps MIME types such as "text/html" or "image/png" to their content) or a kernel iframe (display_type "kernel-iframe", content.html is the iframe's page and content.port the MessagePort to transfer to it, e.g. p5 sketches and interactive matplotlib figures).
Each kernel has its own events, so several kernels can run side by side: listen to them with addEventListener.
Extended by
Constructors
Constructor
new KernelBase(options): KernelBase;Parameters
| Parameter | Type | Description |
|---|---|---|
options | KernelOptions | The kernel's options (copied). |
Returns
Properties
| Property | Modifier | Type | Default value | Description |
|---|---|---|---|---|
_execution_count | protected | number | 0 | - |
_files | protected | Map<string, ArrayBuffer> | ... | - |
_modules | protected | Map<string, ArrayBuffer> | ... | - |
_options | protected | KernelOptions | undefined | - |
_pendingInput? | protected | PromiseDelegate<InputAnswer> | undefined | - |
_runningData | protected | any | {} | - |
beforeEval | public | (data) => Promise<void> | ... | Called by execute before each evaluation, that waits for it. Frontends set it to do what has to be done first (e.g. a backup of the code). |
confirmCamera | public | (data) => Promise<boolean> | ... | Ask the user to let the running code use the camera, at each request (on top of the browser's permission, granted once for all the code run on the page). Frontends set their own dialog, window.confirm is the default. |
Accessors
execution_count
Get Signature
get execution_count(): number;Number of evaluations since the kernel started (reset at each restart), as Jupyter's execution count.
Returns
number
isReady
Get Signature
get isReady(): boolean;Is the kernel started and ready to run code?
Returns
boolean
Methods
_init()
protected _init(): Promise<void>;The kernel's own initialization, run once by init (to be overridden).
Returns
Promise<void>
_start()
protected _start(): Promise<void>;The kernel's own start, run by start (to be overridden).
Returns
Promise<void>
_stop()
protected _stop(): Promise<void>;The kernel's own (asynchronous) stop, run by stop (to be overridden).
Returns
Promise<void>
addEventListener()
Call Signature
addEventListener<K>(eventName, callback): void;Listen to an event of this kernel (see the events of KernelBase).
Type Parameters
| Type Parameter |
|---|
K extends keyof KernelEvents |
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | K | The event's name (e.g. "eval.output"). |
callback | (data) => void | Called with the event's data. |
Returns
void
Call Signature
addEventListener(eventName, callback): void;Listen to an event of this kernel (see the events of KernelBase).
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | string | The event's name (e.g. "eval.output"). |
callback | (_) => void | Called with the event's data. |
Returns
void
assetsURL()
assetsURL(absolute?): string;URL of the assets directory (see KernelOptions.rootPath).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
absolute? | boolean | false | Return an absolute URL. |
Returns
string
banner()
banner(): string;The message displayed by consoles at startup.
Returns
string
basthonRoot()
basthonRoot(absolute?): string;URL of the kernel's files: <assets>/<version>/<language>.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
absolute? | boolean | false | Return an absolute URL. |
Returns
string
cancelPendingInput()
cancelPendingInput(): void;Cancel a pending input on the kernel side (e.g. the kernel gave up waiting for the answer): the frontend is notified by an eval.input-cancel event (with the eval.input data) to close its input field.
Returns
void
clone()
clone(obj): any;Copy a JSON-serializable object.
Parameters
| Parameter | Type | Description |
|---|---|---|
obj | any | The object to copy. |
Returns
any
Its copy.
complete()
complete(code): Promise<[string[], number] | []>;Completions of the code at its end (e.g. the names starting with the last word).
Parameters
| Parameter | Type | Description |
|---|---|---|
code | string | The code before the cursor. |
Returns
Promise<[string[], number] | []>
[completions, start]: the completions and the position in code where they start, or [] if there is none.
dispatchEvent()
protected dispatchEvent(eventName, data): void;Dispatch an event of this kernel (see the events of KernelBase).
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | string | The event's name (e.g. "eval.output"). |
data | any | The event's data. |
Returns
void
download()
download(data, filename): void;Download a file (the browser saves it, or asks where to).
Parameters
| Parameter | Type | Description |
|---|---|---|
data | string | Uint8Array<ArrayBuffer> | The file's content: bytes or a data URL. |
filename | string | The name of the saved file. |
Returns
void
endPendingInput()
endPendingInput(): void;End a pending input: there is nothing more to read (EOFError in Python), e.g. the program's stdin is closed.
Returns
void
evalAsync()
evalAsync(
code,
outCallback,
errCallback,
data?
): Promise<any>;Run code (prefer execute, that dispatches the evaluation's events).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
code | string | undefined | The code to run. |
outCallback | (_) => void | undefined | Called with the text printed on stdout. |
errCallback | (_) => void | undefined | Called with the text printed on stderr. |
data? | any | null | The request's data, passed with the events dispatched during the evaluation. |
Returns
Promise<any>
[result, executionCount]: the result (a MIME bundle, or undefined when the code has no value) and the execution count.
Throws
KernelError when the code fails.
execute()
execute(data): Promise<void>;Run code in this kernel and dispatch the evaluation's events: eval.output while it runs, then eval.finished or eval.error.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | EvalRequest | The request's data, with the code to run (its other fields are copied to the evaluation's events). |
Returns
Promise<void>
Resolved at the end of the evaluation, even if it failed (see eval.error).
getFile()
getFile(path): Promise<Uint8Array<ArrayBufferLike>>;Read a file of the kernel's (virtual) filesystem.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file's path (relative to the working directory or absolute). |
Returns
Promise<Uint8Array<ArrayBufferLike>>
The file's content.
getUserModuleFile()
getUserModuleFile(filename): Promise<Uint8Array<ArrayBufferLike>>;Read a module installed by putModule.
Parameters
| Parameter | Type | Description |
|---|---|---|
filename | string | The module's file name. |
Returns
Promise<Uint8Array<ArrayBufferLike>>
The module's content.
init()
init(): Promise<void>;Initialize the kernel (once) and start it. Wait for ready before running code.
Returns
Promise<void>
Throws
The initialization's error (the kernel can't be used).
inputAsync()
inputAsync(
prompt,
password?,
data?
): Promise<InputAnswer>;Ask the frontend for an input: dispatch an eval.input event and wait for its answer (given to the event's resolve).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
prompt | string | null | undefined | undefined | The text displayed before the input. |
password? | boolean | false | Hide the typed text. |
data? | any | undefined | The evaluation's data (sent with the event). |
Returns
Promise<InputAnswer>
The answer (see InputAnswer).
interruptPendingInput()
interruptPendingInput(): void;Interrupt a pending input: the running program stops (KeyboardInterrupt in Python) instead of getting an answer.
Returns
void
language()
language(): string;The kernel's language, as used in URLs and file paths (lower case, e.g. "python3", "ocaml").
Returns
string
languageName()
languageName(): string;The kernel's language as displayed to the user (e.g. "Python 3").
Returns
string
moduleExts()
moduleExts(): string[];Extensions of the files installed as modules by putRessource (e.g. ["py"]), without the dot.
Returns
string[]
more()
more(source): Promise<boolean>;Does the code need more lines to be complete (e.g. after def f(): in Python)? Used by consoles to ask for continuation lines.
Parameters
| Parameter | Type | Description |
|---|---|---|
source | string | The lines typed so far. |
Returns
Promise<boolean>
true if the code is incomplete.
pendingInput()
pendingInput(): boolean;Is the running code waiting for an input?
Returns
boolean
postIFrameMessage()
postIFrameMessage(key, message): void;Relay a kernel iframe's message to the worker waiting for it (e.g. the answer to a tkinter dialog): frontends call it for the iframes' {type: "iframe-message", key, message} messages.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key of the message, given by the worker. |
message | string | The message. |
Returns
void
ps1()
ps1(): string;The console's prompt for a new instruction.
Returns
string
ps2()
ps2(): string;The console's prompt for a continuation line (see more).
Returns
string
putFile()
putFile(filename, content): Promise<void>;Put a file in the kernel's (virtual) filesystem, in its working directory (an auxiliary file). The file is put again after a restart.
Parameters
| Parameter | Type | Description |
|---|---|---|
filename | string | The file's name. |
content | ArrayBuffer | The file's content. |
Returns
Promise<void>
putModule()
putModule(filename, content): Promise<void>;Install a module for the user's code (e.g. a Python module, importable with import). The module is installed again after a restart.
Parameters
| Parameter | Type | Description |
|---|---|---|
filename | string | The module's file name (its extension is one of moduleExts). |
content | ArrayBuffer | The module's content. |
Returns
Promise<void>
putRessource()
putRessource(filename, content): Promise<void>;Put a file as a module (putModule) if its extension is one of moduleExts, as an auxiliary file (putFile) otherwise.
Parameters
| Parameter | Type | Description |
|---|---|---|
filename | string | The file's name. |
content | ArrayBuffer | The file's content. |
Returns
Promise<void>
ready()
ready(): Promise<void>;Wait for the kernel to be ready (started).
Returns
Promise<void>
Throws
The error of the kernel's initialization or start.
removeEventListener()
Call Signature
removeEventListener<K>(eventName, callback): void;Stop listening to an event of this kernel.
Type Parameters
| Type Parameter |
|---|
K extends keyof KernelEvents |
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | K | The event's name. |
callback | (data) => void | The callback given to addEventListener. |
Returns
void
Call Signature
removeEventListener(eventName, callback): void;Stop listening to an event of this kernel.
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | string | The event's name. |
callback | (_) => void | The callback given to addEventListener. |
Returns
void
resolvePendingInput()
resolvePendingInput(): void;Answer a pending input with an empty text.
Returns
void
restart()
restart(): Promise<void>;Restart the kernel (stop then start): the user's variables are lost, the files and modules are kept.
Returns
Promise<void>
safeKernel()
safeKernel(): boolean;Does the kernel run in a worker (isolated kernel), isolated from the page?
Returns
boolean
false for the main thread's kernels (legacy kernel).
setRunningData()
setRunningData(data): Promise<void>;Data given to the running code, kept across restarts (e.g. the user's random seed in Capytale). Merged with the previous data.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The data to add (a JSON-serializable object). |
Returns
Promise<void>
start()
start(): Promise<void>;Start the kernel (after init or stop): reset the execution count and put the files and modules again.
Returns
Promise<void>
stop()
stop(): void;Stop the kernel: it is no longer ready once the call returns (the end of its stop is awaited by the next start).
Returns
void
userModules()
userModules(): Promise<string[]>;The modules installed by putModule.
Returns
Promise<string[]>
Their file names.
version()
version(): string;The kernel's version (Basthon's version, e.g. "0.87.0").
Returns
string
KernelError
An error raised by the evaluation of code (e.g. a Python exception), given by the eval.error event. name, value and traceback follow Jupyter's ename, evalue and traceback.
Extends
Error
Constructors
Constructor
new KernelError(
name,
value,
traceback?,
displayed?
): KernelError;Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
name | string | undefined | The error's type (e.g. "ZeroDivisionError"). |
value | string | undefined | The error's message. |
traceback? | string | undefined | The error's traceback. |
displayed? | boolean | false | Was the error already shown to the user? |
Returns
Overrides
Error.constructor
Properties
Interfaces
KernelEvents
The events of a kernel and their data (see KernelBase).
Properties
KernelOptions
Options given to a kernel's constructor, common to all the kernels (some kernels have their own ones, e.g. Python3KernelOptions).
Properties
Type Aliases
EvalRequest
type EvalRequest = EvalData & {
code: string;
};What KernelBase.execute takes: the code to run and the evaluation's data.
Type Declaration
| Name | Type |
|---|---|
code | string |
InputAnswer
type InputAnswer = string | null | typeof END_OF_INPUT;The answer to an input: the text typed by the user, null if the input was interrupted (see KernelBase.interruptPendingInput) or END_OF_INPUT at the end of the input (see KernelBase.endPendingInput).
Variables
END_OF_INPUT
const END_OF_INPUT: {
eof: true;
};The answer to an input at the end of the input, e.g. when the program's standard input is closed (see KernelBase.endPendingInput): the program gets an EOFError in Python, EOF in C.
Type Declaration
| Name | Type | Default value |
|---|---|---|
eof | true | true |
VERSION
VERSION: any;