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/frontend/client/jupyter.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import * as message from "@cocalc/util/message";
7
import { AsyncCall } from "./client";
8
import type { KernelSpec } from "@cocalc/jupyter/types";
9
10
export class JupyterClient {
11
private async_call: AsyncCall;
12
13
constructor(async_call: AsyncCall) {
14
this.async_call = async_call;
15
}
16
17
public async kernels(project_id?: string): Promise<KernelSpec[]> {
18
const resp = await this.async_call({
19
message: message.jupyter_kernels({ project_id }),
20
});
21
if (resp.error) {
22
throw Error(resp.error);
23
}
24
return resp.kernels;
25
}
26
27
public async execute({
28
input,
29
kernel,
30
history,
31
hash,
32
tag = "",
33
project_id,
34
path,
35
}: {
36
input?: string;
37
kernel?: string;
38
history?: string[];
39
hash?: string;
40
tag?: string;
41
project_id?: string;
42
path?: string;
43
}): Promise<{ output: object[]; time: Date; total_time_s: number } | null> {
44
const resp = await this.async_call({
45
message: message.jupyter_execute({
46
hash,
47
input,
48
kernel,
49
history,
50
tag,
51
project_id,
52
path,
53
}),
54
});
55
if (resp.error) {
56
throw Error(resp.error);
57
}
58
return resp;
59
}
60
}
61
62