Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/jupyter/stateless-api/execute.ts
Views: 687
/*1~/cocalc/src/packages/project$ node2Welcome to Node.js v16.19.1.3Type ".help" for more information.4> e = require('./dist/jupyter/stateless-api/kernel').default; z = await e.getFromPool('python3'); await z.execute("2+3")5[ { data: { 'text/plain': '5' } } ]6>7*/89import { jupyter_execute_response } from "@cocalc/util/message";10import Kernel from "./kernel";11import getLogger from "@cocalc/backend/logger";12const log = getLogger("jupyter:stateless-api:execute");1314export default async function jupyterExecute(socket, mesg) {15log.debug(mesg);16let kernel: undefined | Kernel = undefined;17try {18kernel = await Kernel.getFromPool(mesg.kernel, mesg.pool);19const outputs: object[] = [];2021if (mesg.path != null) {22try {23await kernel.chdir(mesg.path);24log.debug("successful chdir");25} catch (err) {26outputs.push({ name: "stderr", text: `${err}` });27log.debug("chdir failed", err);28}29}3031if (mesg.history != null && mesg.history.length > 0) {32// just execute this directly, since we will ignore the output33log.debug("evaluating history");34await kernel.execute(mesg.history.join("\n"), mesg.limits);35}3637// append the output of running mesg.input to outputs:38for (const output of await kernel.execute(mesg.input, mesg.limits)) {39outputs.push(output);40}41socket.write_mesg(42"json",43jupyter_execute_response({ id: mesg.id, output: outputs })44);45} finally {46if (kernel) {47await kernel.close();48}49}50}515253