-
-
Notifications
You must be signed in to change notification settings - Fork 787
/
api.ts
515 lines (488 loc) · 15.8 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
declare var Module: any;
declare var Hiwire: any;
declare var API: any;
import "./module.ts";
import { loadPackage, loadedPackages } from "./load-package";
import { isPyProxy, PyBuffer, PyProxy, TypedArray } from "./pyproxy.gen";
import { PythonError } from "./error_handling.gen";
import { loadBinaryFile } from "./compat";
export { loadPackage, loadedPackages, isPyProxy };
import "./error_handling.gen.js";
API.loadBinaryFile = loadBinaryFile;
/**
* An alias to the Python :py:mod:`pyodide` package.
*
* You can use this to call functions defined in the Pyodide Python package
* from JavaScript.
*/
export let pyodide_py: PyProxy; // actually defined in loadPyodide (see pyodide.js)
/**
*
* An alias to the global Python namespace.
*
* For example, to access a variable called ``foo`` in the Python global
* scope, use ``pyodide.globals.get("foo")``
*/
export let globals: PyProxy; // actually defined in loadPyodide (see pyodide.js)
/**
*
* The Pyodide version.
*
* It can be either the exact release version (e.g. ``0.1.0``), or
* the latest release version followed by the number of commits since, and
* the git hash of the current commit (e.g. ``0.1.0-1-bd84646``).
*/
export let version: string = ""; // actually defined in loadPyodide (see pyodide.js)
/**
* Just like `runPython` except uses a different globals dict and gets
* `eval_code` from `_pyodide` so that it can work before `pyodide` is imported.
* @private
*/
API.runPythonInternal = function (code: string): any {
// API.runPythonInternal_dict is initialized in finalizeBootstrap
return API._pyodide._base.eval_code(code, API.runPythonInternal_dict);
};
/**
* Runs a string of Python code from JavaScript, using :any:`pyodide.code.eval_code`
* to evaluate the code. If the last statement in the Python code is an
* expression (and the code doesn't end with a semicolon), the value of the
* expression is returned.
*
* .. admonition:: Positional globals argument
* :class: warning
*
* In Pyodide v0.19, this function took the globals parameter as a positional
* argument rather than as a named argument. In v0.20 this will still work
* but it is deprecated. It will be removed in v0.21.
*
* @param code Python code to evaluate
* @param options
* @param options.globals An optional Python dictionary to use as the globals.
* Defaults to :any:`pyodide.globals`.
* @returns The result of the Python code translated to JavaScript. See the
* documentation for :any:`pyodide.code.eval_code` for more info.
*/
export function runPython(
code: string,
options: { globals?: PyProxy } = {}
): any {
if (!options.globals) {
options.globals = API.globals;
}
return API.pyodide_code.eval_code(code, options.globals);
}
API.runPython = runPython;
/**
* Inspect a Python code chunk and use :js:func:`pyodide.loadPackage` to install
* any known packages that the code chunk imports. Uses the Python API
* :func:`pyodide.code.find\_imports` to inspect the code.
*
* For example, given the following code as input
*
* .. code-block:: python
*
* import numpy as np x = np.array([1, 2, 3])
*
* :js:func:`loadPackagesFromImports` will call
* ``pyodide.loadPackage(['numpy'])``.
*
* @param code The code to inspect.
* @param messageCallback The ``messageCallback`` argument of
* :any:`pyodide.loadPackage` (optional).
* @param errorCallback The ``errorCallback`` argument of
* :any:`pyodide.loadPackage` (optional).
* @async
*/
export async function loadPackagesFromImports(
code: string,
messageCallback?: (msg: string) => void,
errorCallback?: (err: string) => void
) {
let pyimports = API.pyodide_code.find_imports(code);
let imports;
try {
imports = pyimports.toJs();
} finally {
pyimports.destroy();
}
if (imports.length === 0) {
return;
}
let packageNames = API._import_name_to_package_name;
let packages: Set<string> = new Set();
for (let name of imports) {
if (packageNames.has(name)) {
packages.add(packageNames.get(name));
}
}
if (packages.size) {
await loadPackage(Array.from(packages), messageCallback, errorCallback);
}
}
/**
* Run a Python code string with top level await using
* :any:`pyodide.code.eval_code_async` to evaluate the code. Returns a promise which
* resolves when execution completes. If the last statement in the Python code
* is an expression (and the code doesn't end with a semicolon), the returned
* promise will resolve to the value of this expression.
*
* For example:
*
* .. code-block:: pyodide
*
* let result = await pyodide.runPythonAsync(`
* from js import fetch
* response = await fetch("./repodata.json")
* packages = await response.json()
* # If final statement is an expression, its value is returned to JavaScript
* len(packages.packages.object_keys())
* `);
* console.log(result); // 79
*
* .. admonition:: Python imports
* :class: warning
*
* Since pyodide 0.18.0, you must call :js:func:`loadPackagesFromImports` to
* import any python packages referenced via `import` statements in your
* code. This function will no longer do it for you.
*
* .. admonition:: Positional globals argument
* :class: warning
*
* In Pyodide v0.19, this function took the globals parameter as a
* positional argument rather than as a named argument. In v0.20 this will
* still work but it is deprecated. It will be removed in v0.21.
*
* @param code Python code to evaluate
* @param options
* @param options.globals An optional Python dictionary to use as the globals.
* Defaults to :any:`pyodide.globals`.
* @returns The result of the Python code translated to JavaScript.
* @async
*/
export async function runPythonAsync(
code: string,
options: { globals?: PyProxy } = {}
): Promise<any> {
if (!options.globals) {
options.globals = API.globals;
}
return await API.pyodide_code.eval_code_async(code, options.globals);
}
API.runPythonAsync = runPythonAsync;
/**
* Registers the JavaScript object ``module`` as a JavaScript module named
* ``name``. This module can then be imported from Python using the standard
* Python import system. If another module by the same name has already been
* imported, this won't have much effect unless you also delete the imported
* module from ``sys.modules``. This calls the {any}`pyodide_py` API
* :func:`pyodide.register_js_module`.
*
* @param name Name of the JavaScript module to add
* @param module JavaScript object backing the module
*/
export function registerJsModule(name: string, module: object) {
API.pyodide_ffi.register_js_module(name, module);
}
/**
* Tell Pyodide about Comlink.
* Necessary to enable importing Comlink proxies into Python.
*/
export function registerComlink(Comlink: any) {
API._Comlink = Comlink;
}
/**
* Unregisters a JavaScript module with given name that has been previously
* registered with :js:func:`pyodide.registerJsModule` or
* :func:`pyodide.register_js_module`. If a JavaScript module with that name
* does not already exist, will throw an error. Note that if the module has
* already been imported, this won't have much effect unless you also delete
* the imported module from ``sys.modules``. This calls the :any:`pyodide_py` API
* :func:`pyodide.unregister_js_module`.
*
* @param name Name of the JavaScript module to remove
*/
export function unregisterJsModule(name: string) {
API.pyodide_ffi.unregister_js_module(name);
}
/**
* Convert the JavaScript object to a Python object as best as possible.
*
* This is similar to :any:`JsProxy.to_py` but for use from JavaScript. If the
* object is immutable or a :any:`PyProxy`, it will be returned unchanged. If
* the object cannot be converted into Python, it will be returned unchanged.
*
* See :ref:`type-translations-jsproxy-to-py` for more information.
*
* @param obj
* @param options
* @returns The object converted to Python.
*/
export function toPy(
obj: any,
{
depth,
defaultConverter,
}: {
/**
* Optional argument to limit the depth of the conversion.
*/
depth: number;
/**
* Optional argument to convert objects with no default conversion. See the
* documentation of :any:`JsProxy.to_py`.
*/
defaultConverter?: (
value: any,
converter: (value: any) => any,
cacheConversion: (input: any, output: any) => any
) => any;
} = { depth: -1 }
): any {
// No point in converting these, it'd be dumb to proxy them so they'd just
// get converted back by `js2python` at the end
switch (typeof obj) {
case "string":
case "number":
case "boolean":
case "bigint":
case "undefined":
return obj;
}
if (!obj || API.isPyProxy(obj)) {
return obj;
}
let obj_id = 0;
let py_result = 0;
let result = 0;
try {
obj_id = Hiwire.new_value(obj);
try {
py_result = Module.js2python_convert(obj_id, { depth, defaultConverter });
} catch (e) {
if (e instanceof Module._PropagatePythonError) {
Module._pythonexc2js();
}
throw e;
}
if (Module._JsProxy_Check(py_result)) {
// Oops, just created a JsProxy. Return the original object.
return obj;
// return Module.pyproxy_new(py_result);
}
result = Module._python2js(py_result);
if (result === 0) {
Module._pythonexc2js();
}
} finally {
Hiwire.decref(obj_id);
Module._Py_DecRef(py_result);
}
return Hiwire.pop_value(result);
}
/**
* Imports a module and returns it.
*
* .. admonition:: Warning
* :class: warning
*
* This function has a completely different behavior than the old removed pyimport function!
*
* ``pyimport`` is roughly equivalent to:
*
* .. code-block:: js
*
* pyodide.runPython(`import ${pkgname}; ${pkgname}`);
*
* except that the global namespace will not change.
*
* Example:
*
* .. code-block:: js
*
* let sysmodule = pyodide.pyimport("sys");
* let recursionLimit = sysmodule.getrecursionlimit();
*
* @param mod_name The name of the module to import
* @returns A PyProxy for the imported module
*/
export function pyimport(mod_name: string): PyProxy {
return API.importlib.import_module(mod_name);
}
/**
* Unpack an archive into a target directory.
*
* .. admonition:: Positional globals argument :class: warning
*
* In Pyodide v0.19, this function took the extract_dir parameter as a
* positional argument rather than as a named argument. In v0.20 this will
* still work but it is deprecated. It will be removed in v0.21.
*
* @param buffer The archive as an ArrayBuffer or TypedArray.
* @param format The format of the archive. Should be one of the formats
* recognized by `shutil.unpack_archive`. By default the options are 'bztar',
* 'gztar', 'tar', 'zip', and 'wheel'. Several synonyms are accepted for each
* format, e.g., for 'gztar' any of '.gztar', '.tar.gz', '.tgz', 'tar.gz' or
* 'tgz' are considered to be synonyms.
*
* @param options
* @param options.extractDir The directory to unpack the archive into. Defaults
* to the working directory.
*/
export function unpackArchive(
buffer: TypedArray | ArrayBuffer,
format: string,
options: {
extractDir?: string;
} = {}
) {
if (
!ArrayBuffer.isView(buffer) &&
Object.prototype.toString.call(buffer) !== "[object ArrayBuffer]"
) {
throw new TypeError(
`Expected argument 'buffer' to be an ArrayBuffer or an ArrayBuffer view`
);
}
API.typedArrayAsUint8Array(buffer);
let extract_dir = options.extractDir;
API.package_loader.unpack_buffer.callKwargs({
buffer,
format,
extract_dir,
installer: "pyodide.unpackArchive",
});
}
/**
* @private
*/
API.saveState = () => API.pyodide_py._state.save_state();
/**
* @private
*/
API.restoreState = (state: any) => API.pyodide_py._state.restore_state(state);
/**
* Sets the interrupt buffer to be ``interrupt_buffer``. This is only useful
* when Pyodide is used in a webworker. The buffer should be a
* ``SharedArrayBuffer`` shared with the main browser thread (or another
* worker). In that case, signal ``signum`` may be sent by writing ``signum``
* into the interrupt buffer. If ``signum`` does not satisfy 0 < ``signum`` <
* ``NSIG`` it will be silently ignored. NSIG is 65 (internally signals are
* indicated by a bitflag).
*
* You can disable interrupts by calling `setInterruptBuffer(undefined)`.
*
* If you wish to trigger a ``KeyboardInterrupt``, write ``SIGINT`` (a 2), into
* the interrupt buffer.
*
* By default ``SIGINT`` raises a ``KeyboardInterrupt`` and all other signals
* are ignored. You can install custom signal handlers with the signal module.
* Even signals that normally have special meaning and can't be overridden like
* ``SIGKILL`` and ``SIGSEGV`` are ignored by default and can be used for any
* purpose you like.
*/
export function setInterruptBuffer(interrupt_buffer: TypedArray) {
Module.HEAP8[Module._Py_EMSCRIPTEN_SIGNAL_HANDLING] = !!interrupt_buffer;
Module.Py_EmscriptenSignalBuffer = interrupt_buffer;
}
/**
* Throws a KeyboardInterrupt error if a KeyboardInterrupt has been requested
* via the interrupt buffer.
*
* This can be used to enable keyboard interrupts during execution of JavaScript
* code, just as ``PyErr_CheckSignals`` is used to enable keyboard interrupts
* during execution of C code.
*/
export function checkInterrupt() {
if (Module.__PyErr_CheckSignals()) {
Module._pythonexc2js();
}
}
export type PyodideInterface = {
globals: typeof globals;
FS: typeof FS;
PATH: typeof PATH;
ERRNO_CODES: typeof ERRNO_CODES;
pyodide_py: typeof pyodide_py;
version: typeof version;
loadPackage: typeof loadPackage;
loadPackagesFromImports: typeof loadPackagesFromImports;
loadedPackages: typeof loadedPackages;
isPyProxy: typeof isPyProxy;
runPython: typeof runPython;
runPythonAsync: typeof runPythonAsync;
registerJsModule: typeof registerJsModule;
unregisterJsModule: typeof unregisterJsModule;
setInterruptBuffer: typeof setInterruptBuffer;
checkInterrupt: typeof checkInterrupt;
toPy: typeof toPy;
pyimport: typeof pyimport;
unpackArchive: typeof unpackArchive;
registerComlink: typeof registerComlink;
PythonError: typeof PythonError;
PyBuffer: typeof PyBuffer;
};
/**
* An alias to the `Emscripten File System API
* <https://emscripten.org/docs/api_reference/Filesystem-API.html>`_.
*
* This provides a wide range of POSIX-`like` file/device operations, including
* `mount
* <https://emscripten.org/docs/api_reference/Filesystem-API.html#FS.mount>`_
* which can be used to extend the in-memory filesystem with features like `persistence
* <https://emscripten.org/docs/api_reference/Filesystem-API.html#persistent-data>`_.
*
* While all the file systems implementations are enabled, only the default
* ``MEMFS`` is guaranteed to work in all runtime settings. The implementations
* are available as members of ``FS.filesystems``:
* ``IDBFS``, ``NODEFS``, ``PROXYFS``, ``WORKERFS``.
*/
export let FS: any;
/**
* An alias to the `Emscripten Path API
* <https://github.com/emscripten-core/emscripten/blob/main/src/library_path.js>`_.
*
* This provides a variety of operations for working with file system paths, such as
* ``dirname``, ``normalize``, and ``splitPath``.
*/
export let PATH: any;
/**
* An alias to the Emscripten ERRNO_CODES map of standard error codes.
*/
export let ERRNO_CODES: any;
/**
* @private
*/
API.makePublicAPI = function (): PyodideInterface {
FS = Module.FS;
PATH = Module.PATH;
ERRNO_CODES = Module.ERRNO_CODES;
let namespace = {
globals,
FS,
PATH,
ERRNO_CODES,
pyodide_py,
version,
loadPackage,
loadPackagesFromImports,
loadedPackages,
isPyProxy,
runPython,
runPythonAsync,
registerJsModule,
unregisterJsModule,
setInterruptBuffer,
checkInterrupt,
toPy,
pyimport,
unpackArchive,
registerComlink,
PythonError,
PyBuffer,
_module: Module,
_api: API,
};
API.public_api = namespace;
return namespace;
};