Skip to content

gui-base ​

The base of Basthon's interfaces (console and notebook): loading the kernel, the content given by the URL (from, script, aux, module...), sharing, downloads and kernel iframes (see GUIBase).

Classes ​

GUIBase ​

The base of Basthon's frontends (console, notebook): loads the kernel with its loading screen, the content (shared link, checkpoints), the files and modules of the URL, the extensions, and handles sharing, downloads, backups, notifications and the kernel iframes. The frontends override its UI methods (info, error, confirm, select, content, setContent...).

Constructors ​

Constructor ​
ts
new GUIBase(options): GUIBase;

Start loading the kernel (with its loading screen) and the storages.

Parameters ​
ParameterTypeDescription
optionsGUIOptionsThe kernel's options, the frontend's name (for its storages) and whether to create the checkpoints manager.
Returns ​

GUIBase

Properties ​

PropertyModifierTypeDefault valueDescription
_contentFilenameprotectedstring"content.txt"Default name of the file when the user downloads the content.
_stateStorageprotectedStorage<boolean>undefined-
_urlKeyprotectedstring"content"-
copyToClipboardpublic static(text) => void...Copy a text to the clipboard (with the old execCommand API if needed).

Accessors ​

kernel ​
Get Signature ​
ts
get kernel(): any;

The kernel, once created (undefined before).

Returns ​

any

kernelLoader ​
Get Signature ​
ts
get kernelLoader(): KernelLoader;

The kernel loader.

Returns ​

KernelLoader

kernelSafe ​
Get Signature ​
ts
get kernelSafe(): any;

The kernel if it is ready, null otherwise.

Returns ​

any

language ​
Get Signature ​
ts
get language(): string;

The kernel's language.

Returns ​

string

Methods ​

_init() ​
ts
protected _init(options?): Promise<void>;

Initialize the frontend: redirect the errors to notifications, load the extensions, set up the UI, load the content (a failure is notified without stopping), wait for the kernel, then load the URL's files and modules and hide the loading screen.

Parameters ​
ParameterTypeDefault valueDescription
options?anynullThe frontend's options.
Returns ​

Promise<void>

_openFile() ​
ts
protected _openFile(callbacks): Promise<void>;

Let the user open files: a file whose extension has a callback is given to it (e.g. load it in the editor or install it as a module, the user choosing), the other ones are put in the kernel (see putFSRessource).

Parameters ​
ParameterTypeDescription
callbacks{ [key: string]: (_) => Promise<void>; }The callbacks by extension.
Returns ​

Promise<void>

backup() ​
ts
backup(approved?): Promise<any>;

Back up the content in the checkpoints.

Parameters ​
ParameterTypeDescription
approved?booleanIs the backup approved? By default, only if no evaluation is running (a page closed during an infinite loop then proposes recovery).
Returns ​

Promise<any>

confirm() ​
ts
confirm(
   title, 
   message, 
   text, 
   callback, 
   textCancel, 
   callbackCancel
): void;

Ask the user to confirm or cancel (to be overridden).

Parameters ​
ParameterTypeDescription
titlestringThe dialog's title.
messagestringThe dialog's message (HTML).
textstringThe confirmation button's text.
callback() => voidCalled if the user confirms.
textCancelstringThe cancellation button's text.
callbackCancel() => voidCalled if the user cancels.
Returns ​

void

content() ​
ts
content(): string;

The content (script or notebook), to be overridden.

Returns ​

string

deflate() ​
ts
deflate(content): Promise<string>;

Compress a string to a URL-safe string (zlib, base64).

Parameters ​
ParameterTypeDescription
contentstringThe string.
Returns ​

Promise<string>

The compressed string.

download() ​
ts
download(filename?): void;

Download the content (backed up first).

Parameters ​
ParameterTypeDescription
filename?stringThe file's name (the frontend's default otherwise).
Returns ​

void

error() ​
ts
error(title, message): void;

Notify the user of an error (to be overridden).

Parameters ​
ParameterTypeDescription
titlestringThe notification's title.
messagestringThe notification's message (HTML).
Returns ​

void

getState() ​
ts
protected getState(state, def?): Promise<any>;

Read a state of the frontend (e.g. the theme) from its storage.

Parameters ​
ParameterTypeDescription
statestringThe state's name.
def?anyThe default value.
Returns ​

Promise<any>

The state's value.

handleDynamicIFrame() ​
ts
handleDynamicIFrame(iframe): Promise<void>;

Connect an iframe displayed by the code (e.g. PythonTutor) to the frontend: it gets a port to ask for its size, and is asked to refresh when first visible.

Parameters ​
ParameterTypeDescription
iframeHTMLIFrameElementThe iframe.
Returns ​

Promise<void>

handleKernelIFrame() ​
ts
handleKernelIFrame(iframe, port): Promise<void>;

Connect a kernel iframe (from an eval.display event): add its fullscreen button, then once loaded, transfer it the kernel's port and a port to the frontend (gui-port, see iframeResizeChannel).

Parameters ​
ParameterTypeDescription
iframeHTMLIFrameElementThe iframe.
portMessagePortThe kernel's port (content.port of the event).
Returns ​

Promise<void>

inflate() ​
ts
inflate(content): Promise<string>;

Decompress a string compressed by deflate.

Parameters ​
ParameterTypeDescription
contentstringThe compressed string.
Returns ​

Promise<string>

The string.

Throws ​

If it is not a compressed string.

info() ​
ts
info(title, message): void;

Notify the user (to be overridden).

Parameters ​
ParameterTypeDescription
titlestringThe notification's title.
messagestringThe notification's message (HTML).
Returns ​

void

init() ​
ts
init(options?): Promise<void>;

Initialize the frontend (see _init); loaded waits for it.

Parameters ​
ParameterTypeDefault valueDescription
options?anynullThe frontend's options (given to setupUI and loadInitialContent).
Returns ​

Promise<void>

initCaller() ​
ts
initCaller(
   func, 
   message, 
   catchError
): Promise<any>;

Run a step of the initialization, showing a message on the loading screen.

Parameters ​
ParameterTypeDescription
func() => Promise<any>The step.
messagestringThe loading screen's message.
catchErrorbooleanNotify the step's error and go on (otherwise, the error is thrown).
Returns ​

Promise<any>

The step's result.

kernelRestart() ​
ts
kernelRestart(): Promise<void>;

Restart the kernel (a pending input is answered first).

Returns ​

Promise<void>

lastBackupValid() ​
ts
lastBackupValid(): Promise<any>;

Is the last backup approved (undefined without checkpoints)?

Returns ​

Promise<any>

loaded() ​
ts
loaded(): Promise<void>;

Wait for the frontend to be initialized (see init).

Returns ​

Promise<void>

Throws ​

The initialization's error.

loadFromQS() ​
ts
loadFromQS(key): Promise<string | null>;

Load the content given by the URL: compressed in a parameter (e.g. script or ipynb, see sharingURL) or downloaded from the from parameter (empty: an empty content).

Parameters ​
ParameterTypeDescription
keystringThe parameter of the content (script, ipynb...).
Returns ​

Promise<string | null>

The content, null if the URL gives none.

Throws ​

If the from file can't be downloaded.

loadFromStorage() ​
ts
loadFromStorage(setContent?): Promise<string | null>;

Load the last backup. If it was not approved (the page was closed while code was running, e.g. an infinite loop), the user chooses a backup.

Parameters ​
ParameterTypeDefault valueDescription
setContent?booleantrueSet it as the content.
Returns ​

Promise<string | null>

The backup (null if there is none or the user cancels).

loadInitialContent() ​
ts
protected loadInitialContent(options): Promise<void>;

Load the content at startup: from the URL, otherwise from the last backup.

Parameters ​
ParameterTypeDescription
optionsanyThe frontend's options.
Returns ​

Promise<void>

loadURLAux() ​
ts
loadURLAux(): Promise<void>;

Load the auxiliary files of the URL (aux parameter).

Returns ​

Promise<void>

loadURLModules() ​
ts
loadURLModules(): Promise<void>;

Load the modules of the URL (module parameter).

Returns ​

Promise<void>

notifyError() ​
ts
notifyError(error): void;

Notify the user of an error (all the page's errors and console.error calls are redirected here), and hide the loading screen. The error is also logged to the browser's console.

Parameters ​
ParameterTypeDescription
errorError | ErrorEvent | PromiseRejectionEventThe error (its message is shown, HTML escaped).
Returns ​

void

open() ​
ts
open(file): Promise<void>;

Load a file as the content.

Parameters ​
ParameterTypeDescription
fileFile | BlobThe file.
Returns ​

Promise<void>

pageLoad() ​
ts
pageLoad(): Promise<void>;

Wait for the page to be loaded (document.body available).

Returns ​

Promise<void>

putFSRessource() ​
ts
putFSRessource(file): Promise<void>;

Put a file of the user in the kernel, as a module or an auxiliary file (see KernelBase.putRessource), and notify the user.

Parameters ​
ParameterTypeDescription
fileFileThe file.
Returns ​

Promise<void>

registerExtension() ​
ts
registerExtension(name, callback): void;

Register an extension, loaded when the URL's extensions parameter names it. Frontends register theirs in their constructor (e.g. the notebook's sequenced or linenumbers).

Parameters ​
ParameterTypeDescription
namestringThe extension's name.
callback() => Promise<void>Load the extension.
Returns ​

void

select() ​
ts
select(
   title, 
   message, 
   choices, 
   textCancel, 
   callbackCancel
): void;

Ask the user to choose (to be overridden).

Parameters ​
ParameterTypeDescription
titlestringThe dialog's title.
messagestringThe dialog's message (HTML).
choices{ handler: () => void; text: string; }[]The choices: their text and what to do.
textCancelstringThe cancellation button's text.
callbackCancel() => voidCalled if the user cancels.
Returns ​

void

selectCheckpoint() ​
ts
selectCheckpoint(): Promise<string | null>;

Let the user choose a backup.

Returns ​

Promise<string | null>

The backup's content (null if the user cancels).

setCheckpointsManager() ​
ts
setCheckpointsManager(checkpoints): void;

Set the checkpoints manager (when created by the frontend, see GUIOptions.noCheckpointsInit).

Parameters ​
ParameterTypeDescription
checkpointsCheckpointsManager<string>The checkpoints manager.
Returns ​

void

setContent() ​
ts
setContent(content): void;

Set the content (script or notebook), to be overridden.

Parameters ​
ParameterTypeDescription
contentstringThe content.
Returns ​

void

setState() ​
ts
protected setState(state, value): Promise<void>;

Write a state of the frontend in its storage.

Parameters ​
ParameterTypeDescription
statestringThe state's name.
valueanyThe value.
Returns ​

Promise<void>

setupUI() ​
ts
protected setupUI(options): Promise<void>;

Set up the UI once the kernel is available: backups at each evaluation, evaluations counting, extensions. To be extended by the frontends.

Parameters ​
ParameterTypeDescription
optionsanyThe frontend's options.
Returns ​

Promise<void>

share() ​
ts
share(): Promise<void>;

Share the content: create its link and let the user copy or test it.

Returns ​

Promise<void>

sharingURL() ​
ts
sharingURL(key): Promise<string>;

The sharing link of the content: the page's URL with the content compressed in a parameter (see deflate).

Parameters ​
ParameterTypeDescription
keystringThe parameter (script, ipynb...).
Returns ​

Promise<string>

The link.

theme() ​
ts
theme(): Promise<"dark" | "light" | undefined>;

The current theme (to be overridden).

Returns ​

Promise<"dark" | "light" | undefined>

validateBackup() ​
ts
validateBackup(): Promise<void>;

Tag the last backup as approved.

Returns ​

Promise<void>

openURL() ​
ts
static openURL(url, download?): void;

Open a URL in a new tab, or download it.

Parameters ​
ParameterTypeDescription
urlstringThe URL.
download?stringThe name of the file to download (open the URL otherwise).
Returns ​

void

Interfaces ​

GUIOptions ​

Options of a frontend.

Properties ​

PropertyTypeDescription
kernelOptionsKernelLoaderOptionsThe kernel's language and options.
noCheckpointsInit?booleanDon't create the checkpoints manager (the frontend sets its own, see setCheckpointsManager).
uiName?stringThe frontend's name (e.g. "console"), naming its storages.