Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/jupyter/execute.ts
5907 views
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
24
import type { Request, Response } from "express";
25
26
import { execute } from "@cocalc/server/jupyter/execute";
27
import getAccountId from "lib/account/get-account";
28
import getParams from "lib/api/get-params";
29
import { getAnonymousID } from "lib/user-id";
30
31
export default async function handle(req: Request, res: Response) {
32
try {
33
const result = await doIt(req);
34
res.json({ ...result, success: true });
35
} catch (err) {
36
res.json({ error: `${err.message}` });
37
return;
38
}
39
}
40
41
async function doIt(req: Request) {
42
const { input, kernel, history, tag, noCache, hash, project_id, path } =
43
getParams(req);
44
const account_id = await getAccountId(req);
45
const anonymous_id = await getAnonymousID(req);
46
return await execute({
47
account_id,
48
project_id,
49
path,
50
anonymous_id,
51
input,
52
hash,
53
history,
54
kernel,
55
tag,
56
noCache,
57
});
58
}
59
60