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/next/pages/api/v2/jupyter/execute.ts
Views: 687
1
/*
2
Evaluation of code using a Jupyter kernel as a service.
3
4
The INPUT parameters are:
5
6
- kernel: the name of the Jupyter kernel
7
- history: list of previous inputs as string (in order) that were sent to the kernel.
8
- input: a new input
9
10
ALTERNATIVELY, can just give:
11
12
- hash: hash of kernel/history/input
13
14
and if output is known it is returned. Otherwise, nothing happens.
15
We are trusting that there aren't hash collisions for this applications,
16
since we're using a sha1 hash.
17
18
The OUTPUT is:
19
20
- a list of messages that describe the output of the last code execution.
21
22
*/
23
import { execute } from "@cocalc/server/jupyter/execute";
24
import getAccountId from "lib/account/get-account";
25
import getParams from "lib/api/get-params";
26
import { analytics_cookie_name } from "@cocalc/util/misc";
27
28
export default async function handle(req, res) {
29
try {
30
const result = await doIt(req);
31
res.json({ ...result, success: true });
32
} catch (err) {
33
res.json({ error: `${err.message}` });
34
return;
35
}
36
}
37
38
async function doIt(req) {
39
const { input, kernel, history, tag, noCache, hash, project_id, path } =
40
getParams(req);
41
const account_id = await getAccountId(req);
42
const analytics_cookie = req.cookies[analytics_cookie_name];
43
return await execute({
44
account_id,
45
project_id,
46
path,
47
analytics_cookie,
48
input,
49
hash,
50
history,
51
kernel,
52
tag,
53
noCache,
54
});
55
}
56
57