CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/jupyter/test/common.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// Use clean in-memory blob store for tests.
7
process.env.JUPYTER_BLOBS_DB_FILE = "memory";
8
9
import { kernel as jupyter_kernel } from "../jupyter";
10
11
import { JupyterKernelInterface } from "@cocalc/frontend/jupyter/project-interface";
12
export type JupyterKernel = JupyterKernelInterface;
13
14
import json from "json-stable-stringify";
15
16
const DEBUG = !!process.env["DEBUG"];
17
if (DEBUG) {
18
console.log("DEBUG =", DEBUG);
19
}
20
21
// We use custom kernels for testing, since faster to start.
22
// For example, we don't use matplotlib inline for testing (much) and
23
// using it greatly slows down startup.
24
export function custom_kernel_path() {
25
process.env.JUPYTER_PATH = `${__dirname}/jupyter`;
26
}
27
custom_kernel_path();
28
29
export function default_kernel_path() {
30
process.env.JUPYTER_PATH = "/ext/jupyter";
31
}
32
33
export function kernel(name: string, path?: string): JupyterKernelInterface {
34
if (path == null) {
35
path = "";
36
}
37
return jupyter_kernel({ name, verbose: DEBUG, path });
38
}
39
40
export async function exec(k: JupyterKernel, code: string): Promise<string> {
41
return output(await k.execute_code_now({ code: code }));
42
}
43
44
// String summary of key aspect of output, which is useful for testing.
45
export function output(v: any[]): string {
46
let s = "";
47
let x: any;
48
for (x of v) {
49
if (x.content == null) continue;
50
if (x.content.data != null) {
51
return json(x.content.data);
52
}
53
if (x.content.text != null) {
54
s += x.content.text;
55
}
56
if (x.content.ename != null) {
57
return json(x.content);
58
}
59
}
60
return s;
61
}
62
63