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
new GUIBase(options): GUIBase;Start loading the kernel (with its loading screen) and the storages.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | GUIOptions | The kernel's options, the frontend's name (for its storages) and whether to create the checkpoints manager. |
Returns
Properties
Accessors
kernel
Get Signature
get kernel(): any;The kernel, once created (undefined before).
Returns
any
kernelLoader
Get Signature
get kernelLoader(): KernelLoader;The kernel loader.
Returns
KernelLoader
kernelSafe
Get Signature
get kernelSafe(): any;The kernel if it is ready, null otherwise.
Returns
any
language
Get Signature
get language(): string;The kernel's language.
Returns
string
Methods
_init()
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
options? | any | null | The frontend's options. |
Returns
Promise<void>
_openFile()
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
| Parameter | Type | Description |
|---|---|---|
callbacks | { [key: string]: (_) => Promise<void>; } | The callbacks by extension. |
Returns
Promise<void>
backup()
backup(approved?): Promise<any>;Back up the content in the checkpoints.
Parameters
| Parameter | Type | Description |
|---|---|---|
approved? | boolean | Is 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()
confirm(
title,
message,
text,
callback,
textCancel,
callbackCancel
): void;Ask the user to confirm or cancel (to be overridden).
Parameters
| Parameter | Type | Description |
|---|---|---|
title | string | The dialog's title. |
message | string | The dialog's message (HTML). |
text | string | The confirmation button's text. |
callback | () => void | Called if the user confirms. |
textCancel | string | The cancellation button's text. |
callbackCancel | () => void | Called if the user cancels. |
Returns
void
content()
content(): string;The content (script or notebook), to be overridden.
Returns
string
deflate()
deflate(content): Promise<string>;Compress a string to a URL-safe string (zlib, base64).
Parameters
| Parameter | Type | Description |
|---|---|---|
content | string | The string. |
Returns
Promise<string>
The compressed string.
download()
download(filename?): void;Download the content (backed up first).
Parameters
| Parameter | Type | Description |
|---|---|---|
filename? | string | The file's name (the frontend's default otherwise). |
Returns
void
error()
error(title, message): void;Notify the user of an error (to be overridden).
Parameters
| Parameter | Type | Description |
|---|---|---|
title | string | The notification's title. |
message | string | The notification's message (HTML). |
Returns
void
getState()
protected getState(state, def?): Promise<any>;Read a state of the frontend (e.g. the theme) from its storage.
Parameters
| Parameter | Type | Description |
|---|---|---|
state | string | The state's name. |
def? | any | The default value. |
Returns
Promise<any>
The state's value.
handleDynamicIFrame()
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
| Parameter | Type | Description |
|---|---|---|
iframe | HTMLIFrameElement | The iframe. |
Returns
Promise<void>
handleKernelIFrame()
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
| Parameter | Type | Description |
|---|---|---|
iframe | HTMLIFrameElement | The iframe. |
port | MessagePort | The kernel's port (content.port of the event). |
Returns
Promise<void>
inflate()
inflate(content): Promise<string>;Decompress a string compressed by deflate.
Parameters
| Parameter | Type | Description |
|---|---|---|
content | string | The compressed string. |
Returns
Promise<string>
The string.
Throws
If it is not a compressed string.
info()
info(title, message): void;Notify the user (to be overridden).
Parameters
| Parameter | Type | Description |
|---|---|---|
title | string | The notification's title. |
message | string | The notification's message (HTML). |
Returns
void
init()
init(options?): Promise<void>;Initialize the frontend (see _init); loaded waits for it.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
options? | any | null | The frontend's options (given to setupUI and loadInitialContent). |
Returns
Promise<void>
initCaller()
initCaller(
func,
message,
catchError
): Promise<any>;Run a step of the initialization, showing a message on the loading screen.
Parameters
| Parameter | Type | Description |
|---|---|---|
func | () => Promise<any> | The step. |
message | string | The loading screen's message. |
catchError | boolean | Notify the step's error and go on (otherwise, the error is thrown). |
Returns
Promise<any>
The step's result.
kernelRestart()
kernelRestart(): Promise<void>;Restart the kernel (a pending input is answered first).
Returns
Promise<void>
lastBackupValid()
lastBackupValid(): Promise<any>;Is the last backup approved (undefined without checkpoints)?
Returns
Promise<any>
loaded()
loaded(): Promise<void>;Wait for the frontend to be initialized (see init).
Returns
Promise<void>
Throws
The initialization's error.
loadFromQS()
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
| Parameter | Type | Description |
|---|---|---|
key | string | The 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()
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
setContent? | boolean | true | Set it as the content. |
Returns
Promise<string | null>
The backup (null if there is none or the user cancels).
loadInitialContent()
protected loadInitialContent(options): Promise<void>;Load the content at startup: from the URL, otherwise from the last backup.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | any | The frontend's options. |
Returns
Promise<void>
loadURLAux()
loadURLAux(): Promise<void>;Load the auxiliary files of the URL (aux parameter).
Returns
Promise<void>
loadURLModules()
loadURLModules(): Promise<void>;Load the modules of the URL (module parameter).
Returns
Promise<void>
notifyError()
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
| Parameter | Type | Description |
|---|---|---|
error | Error | ErrorEvent | PromiseRejectionEvent | The error (its message is shown, HTML escaped). |
Returns
void
open()
open(file): Promise<void>;Load a file as the content.
Parameters
| Parameter | Type | Description |
|---|---|---|
file | File | Blob | The file. |
Returns
Promise<void>
pageLoad()
pageLoad(): Promise<void>;Wait for the page to be loaded (document.body available).
Returns
Promise<void>
putFSRessource()
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
| Parameter | Type | Description |
|---|---|---|
file | File | The file. |
Returns
Promise<void>
registerExtension()
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
| Parameter | Type | Description |
|---|---|---|
name | string | The extension's name. |
callback | () => Promise<void> | Load the extension. |
Returns
void
select()
select(
title,
message,
choices,
textCancel,
callbackCancel
): void;Ask the user to choose (to be overridden).
Parameters
| Parameter | Type | Description |
|---|---|---|
title | string | The dialog's title. |
message | string | The dialog's message (HTML). |
choices | { handler: () => void; text: string; }[] | The choices: their text and what to do. |
textCancel | string | The cancellation button's text. |
callbackCancel | () => void | Called if the user cancels. |
Returns
void
selectCheckpoint()
selectCheckpoint(): Promise<string | null>;Let the user choose a backup.
Returns
Promise<string | null>
The backup's content (null if the user cancels).
setCheckpointsManager()
setCheckpointsManager(checkpoints): void;Set the checkpoints manager (when created by the frontend, see GUIOptions.noCheckpointsInit).
Parameters
| Parameter | Type | Description |
|---|---|---|
checkpoints | CheckpointsManager<string> | The checkpoints manager. |
Returns
void
setContent()
setContent(content): void;Set the content (script or notebook), to be overridden.
Parameters
| Parameter | Type | Description |
|---|---|---|
content | string | The content. |
Returns
void
setState()
protected setState(state, value): Promise<void>;Write a state of the frontend in its storage.
Parameters
| Parameter | Type | Description |
|---|---|---|
state | string | The state's name. |
value | any | The value. |
Returns
Promise<void>
setupUI()
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
| Parameter | Type | Description |
|---|---|---|
options | any | The frontend's options. |
Returns
Promise<void>
share()
share(): Promise<void>;Share the content: create its link and let the user copy or test it.
Returns
Promise<void>
sharingURL()
sharingURL(key): Promise<string>;The sharing link of the content: the page's URL with the content compressed in a parameter (see deflate).
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The parameter (script, ipynb...). |
Returns
Promise<string>
The link.
theme()
theme(): Promise<"dark" | "light" | undefined>;The current theme (to be overridden).
Returns
Promise<"dark" | "light" | undefined>
validateBackup()
validateBackup(): Promise<void>;Tag the last backup as approved.
Returns
Promise<void>
openURL()
static openURL(url, download?): void;Open a URL in a new tab, or download it.
Parameters
| Parameter | Type | Description |
|---|---|---|
url | string | The URL. |
download? | string | The name of the file to download (open the URL otherwise). |
Returns
void
Interfaces
GUIOptions
Options of a frontend.