UNPKG

@datalayer/core

Version:
121 lines (120 loc) 3.58 kB
/* * Copyright (c) 2023-2025 Datalayer, Inc. * Distributed under the terms of the Modified BSD License. */ import * as Python from './Python'; /** * Facade to interact with kernels by requesting code execution. */ export class RuntimeSnippetsFacade { static $language = new Map(); /** * Register a facade for a given language * * @param language Kernel language * @param API Kernel API */ static register(language, API) { this.$language.set(language, API); } /** * Whether there is an API for the given kernel language * * @param language Kernel language */ static supports(language) { return this.$language.has(language); } _language; /** * Create a kernel snippet generator for a given kernel * language. * * It will raise if the language is not available. * * @param language Kernel language */ constructor(language) { if (!RuntimeSnippetsFacade.$language.has(language)) { throw new Error(`${language} is not supported.`); } this._language = language; } /** * Kernel language */ get language() { return this._language; } /** * Code snippet to change the current working directory. * * The code snippet has no returned value. * * @param path New current directory * @returns The code snippet */ changeCurrentWorkingDirectory(path) { return RuntimeSnippetsFacade.$language .get(this.language) .changeCurrentWorkingDirectory(path); } /** * Code snippet to list the transferable kernel variables. * * The code snippet must return a serialized-JSON * dictionary (<variable name>, <variable type>) * * @returns The code snippet */ listVariables() { return RuntimeSnippetsFacade.$language.get(this.language).listVariables(); } /** * Code snippet to load kernel variables. * * The code snippet must return a serialized-JSON list * with successfully loaded variables. * * The format used to store the variable value is up to the kernel. * It should JSON-serializable and will be passed from {@link saveVariables}. * * @param variables Serialized-JSON dictionary (<variable name>, <variable value blob>) * @returns The code snippet */ loadVariables(variables) { return RuntimeSnippetsFacade.$language .get(this.language) .loadVariables(variables); } /** * Code snippet to save kernel variables. * * The code snippet must return a serialized-JSON dictionary * (<variable name>, <variable value blob>). * * The format used to store the variable value is up to the kernel. * It should JSON-serializable and will be passed to {@link loadVariables} * without modification. * * @param variables Variable names. * @returns The code snippet */ saveVariables(variables) { return RuntimeSnippetsFacade.$language .get(this.language) .saveVariables(variables); } /** * Extract output variables candidates by parsing the cell content. * * @param code Cell code source * @returns The output variable candidates */ getOutputCandidates(code) { return RuntimeSnippetsFacade.$language .get(this.language) .getOutputCandidates(code); } } RuntimeSnippetsFacade.register('python', Python);