@marimo-team/islands 0.25.2-dev14 → 0.25.2-dev17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marimo-team/islands",
3
- "version": "0.25.2-dev14",
3
+ "version": "0.25.2-dev17",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -3,7 +3,7 @@
3
3
  import { loadPyodide } from "pyodide";
4
4
  import type { PyodideInterface } from "pyodide";
5
5
  import { beforeEach, describe, expect, it, vi } from "vitest";
6
- import { DefaultWasmController } from "../worker/bootstrap";
6
+ import { DefaultWasmController, requirementName } from "../worker/bootstrap";
7
7
 
8
8
  vi.mock("pyodide", () => ({ loadPyodide: vi.fn() }));
9
9
  vi.mock("../worker/getMarimoWheel", () => ({
@@ -61,3 +61,35 @@ describe("WASM runtime sources", () => {
61
61
  expect(controller.requirePyodide).toBe(pyodide);
62
62
  });
63
63
  });
64
+
65
+ describe("requirementName", () => {
66
+ it("returns the distribution name for a bare requirement", () => {
67
+ expect(requirementName("pandas")).toBe("pandas");
68
+ });
69
+
70
+ it.each([
71
+ ["pandas==2.1.0", "pandas"],
72
+ ["pandas>=2", "pandas"],
73
+ ["pandas<=2", "pandas"],
74
+ ["pandas~=2.1", "pandas"],
75
+ ["pandas!=2.0", "pandas"],
76
+ ["nltools===0.6.0.dev2", "nltools"],
77
+ ])("cuts the specifier off %s", (requirement, expected) => {
78
+ expect(requirementName(requirement)).toBe(expected);
79
+ });
80
+
81
+ it("cuts extras and markers", () => {
82
+ expect(requirementName("rich[jupyter]>=13")).toBe("rich");
83
+ expect(requirementName('cowsay==6.1; sys_platform == "emscripten"')).toBe(
84
+ "cowsay",
85
+ );
86
+ });
87
+
88
+ it("cuts a URL requirement at the name", () => {
89
+ expect(requirementName("pkg @ https://example.com/pkg.whl")).toBe("pkg");
90
+ });
91
+
92
+ it("tolerates surrounding whitespace", () => {
93
+ expect(requirementName(" pandas >= 2 ")).toBe("pandas");
94
+ });
95
+ });
@@ -30,6 +30,17 @@ type SessionResources = [
30
30
  // 3.b Install from micropip
31
31
  // 4. Initialize the notebook
32
32
 
33
+ /**
34
+ * The distribution name of a PEP 508 requirement: "pandas>=2" -> "pandas".
35
+ *
36
+ * Extras, markers and URL forms are all cut at their first delimiter, which is
37
+ * enough for the "already installed" lookups this is used for.
38
+ */
39
+ export function requirementName(requirement: string): string {
40
+ // Trim first: a leading space would otherwise split into an empty name.
41
+ return requirement.trim().split(/[[<>=!~;@\s]/)[0];
42
+ }
43
+
33
44
  export class DefaultWasmController implements WasmController {
34
45
  protected pyodide: PyodideInterface | null = null;
35
46
  private packageLoadQueue = Promise.resolve();
@@ -314,18 +325,26 @@ export class DefaultWasmController implements WasmController {
314
325
  });
315
326
  loadSpan.end();
316
327
 
317
- // Load from micropip
328
+ // Load from micropip.
329
+ //
330
+ // `imports` are PEP 508 requirements and may carry a version specifier
331
+ // ("pandas>=2", "nltools==0.6.0.dev2"). micropip wants them whole, but the
332
+ // "do we already have this?" checks below are keyed by name, so compare on
333
+ // the name and install the requirement.
318
334
  loadSpan = t.startSpan("micropip.install");
319
335
  const missingPackages = imports.filter(
320
- (pkg) => !pyodide.loadedPackages[pkg],
336
+ (pkg) => !pyodide.loadedPackages[requirementName(pkg)],
321
337
  );
322
338
  if (missingPackages.length > 0) {
339
+ const byName = missingPackages.map(
340
+ (pkg) => [requirementName(pkg), pkg] as const,
341
+ );
323
342
  await pyodide
324
343
  .runPythonAsync(`
325
344
  import micropip
326
345
  import sys
327
346
  # Filter out builtins
328
- missing = [p for p in ${JSON.stringify(missingPackages)} if p not in sys.modules]
347
+ missing = [req for name, req in ${JSON.stringify(byName)} if name not in sys.modules]
329
348
  if len(missing) > 0:
330
349
  print("Loading from micropip:", missing)
331
350
  await micropip.install(missing)