Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

1.7 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import { createModuleWorker } from "@/utils/worker";
import { getMarimoVersion } from "../meta/globals";
import workerUrl from "./worker/worker.tsx?worker&url";

/**
 * Interface for creating Web Workers for islands
 */
export interface WorkerFactory {
  /**
   * Creates a new worker instance
   */
  create(): Worker;
}

/**
 * Configuration for the default worker factory
 */
export interface DefaultWorkerFactoryConfig {
  /**
   * The URL to the worker script
   * Defaults to the bundled worker
   */
  workerUrl?: string;

  /**
   * The name to give the worker (shows in DevTools)
   * Defaults to the marimo version
   */
  workerName?: string;
}

/**
 * Default implementation of WorkerFactory that creates Pyodide workers
 * for islands mode.
 */
export class DefaultWorkerFactory implements WorkerFactory {
  private readonly url: string;
  private readonly name: string;

  public constructor(config: DefaultWorkerFactoryConfig = {}) {
    this.url = config.workerUrl || this.getDefaultWorkerUrl();
    this.name = config.workerName || getMarimoVersion();
  }

  /**
   * Creates a new Pyodide worker
   */
  public create(): Worker {
    return createModuleWorker(new URL(this.url, import.meta.url), {
      name: this.name,
    });
  }

  /**
   * Gets the default worker URL based on environment
   */
  private getDefaultWorkerUrl(): string {
    const url = import.meta.env.DEV
      ? workerUrl
      : makeRelativeWorkerUrl(workerUrl);
    return url;
  }
}

/**
 * Makes worker URLs relative for production builds
 */
function makeRelativeWorkerUrl(url: string): string {
  return url.startsWith("./")
    ? url
    : url.startsWith("/")
      ? `.${url}`
      : `./${url}`;
}