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/redux/handle-nbconvert-change.ts
Views: 687
/*1Client sets this:2{type:'nbconvert', args:[...], state:'start'}34Then:51. All clients show status bar that export is happening.62. Commands to export are disabled during export.73. Unless timeout exceeded.89The code in this file implements what the project does.1011- Project sees export entry in table. If currently exporting, does nothing.12If not exporting, starts exporting and sets:1314{type:'nbconvert', args:[...], state:'run', start:[time in ms]}1516- When done, project sets1718{type:'nbconvert', args:[...], state:'done'}1920- If error, project stores the error in the key:value store and sets:2122{type:'nbconvert', args:[...], state:'done', error:'message' or {key:'xlkjdf'}}23*/2425import type { JupyterActions } from "./project-actions";26import { is_array, trunc } from "@cocalc/util/misc";2728import { getLogger } from "@cocalc/backend/logger";29const log = getLogger("jupyter:handle-nbconvert-change");3031// nbconvert can output arbitrarily large errors, so we truncate.32// But don't truncate too small! https://github.com/sagemathinc/cocalc/issues/587833const MAX_ERROR_LENGTH = 100000;3435interface Value {36state: string;37args: string[];38}3940export default async function handleChange(41actions: JupyterActions,42oldVal?: Value,43newVal?: Value44): Promise<void> {45log.debug("got a change:", oldVal, newVal);46if (newVal == null) {47log.debug("delete nbconvert; no op");48return;49}50if (newVal.state != "start") {51log.debug(52`nothing to do -- requesting to change state to '${newVal.state}'.`53);54return;55}56const { args } = newVal;57if (!is_array(args)) {58log.debug("invalid args -- must be an array");59actions.syncdb.set({60type: "nbconvert",61state: "done",62error: "args must be an array",63});64actions.syncdb.commit();65return;66}6768log.debug("tell client that we started running");69let error: any = null;70actions.syncdb.set({71type: "nbconvert",72state: "run",73start: new Date().getTime(),74error,75});76actions.syncdb.commit();77actions.ensure_backend_kernel_setup();7879try {80log.debug(81"saving file to disk first, since some nbconvert functionality uses that file is on disk."82);83await actions.save_ipynb_file();8485log.debug(86"now actually run nbconvert command (which may or may not actually use upstream nbconvert...)"87);88if (actions.jupyter_kernel == null) {89throw Error("no kernel, so can't run nbconvert");90}9192await actions.jupyter_kernel.nbconvert(args);93log.debug("success");94} catch (err) {95error = trunc(`${err}`, MAX_ERROR_LENGTH);96log.debug("error", error);97}98actions.syncdb.set({99type: "nbconvert",100state: "done",101error,102time: new Date().getTime(),103});104actions.syncdb.commit();105}106107108