WebAssembly support for Gradio applications enabling Python code execution in the browser through Pyodide integration
npx @tessl/cli install tessl/npm-gradio--wasm@0.18.0@gradio/wasm provides WebAssembly support for Gradio applications by enabling Python code execution in the browser through Pyodide integration. It offers a WorkerProxy class that manages web workers for running Python code, WebAssembly-based server-sent events for real-time communication, and cross-origin worker support for enhanced security.
npm install @gradio/wasmimport { WorkerProxy, WasmWorkerEventSource } from "@gradio/wasm";Svelte integration:
import {
setWorkerProxyContext,
getWorkerProxyContext,
should_proxy_wasm_src,
resolve_wasm_src,
DownloadLink
} from "@gradio/wasm/svelte";Network utilities:
import { FAKE_LITE_HOST, is_self_host } from "@gradio/wasm/network";For CommonJS:
const { WorkerProxy, WasmWorkerEventSource } = require("@gradio/wasm");import { WorkerProxy } from "@gradio/wasm";
// Create a worker proxy for Python execution
const worker = new WorkerProxy({
gradioWheelUrl: "https://example.com/gradio.whl",
gradioClientWheelUrl: "https://example.com/gradio_client.whl",
files: {},
requirements: ["numpy", "pandas"],
sharedWorkerMode: false
});
// Wait for initialization
worker.addEventListener("initialization-completed", () => {
console.log("Worker ready!");
});
// Run Python code
await worker.runPythonCode(`
import numpy as np
data = np.array([1, 2, 3, 4, 5])
print(f"Mean: {np.mean(data)}")
`);
// Handle output
worker.addEventListener("stdout", (event) => {
console.log("Python output:", event.data);
});
// Clean up
worker.terminate();@gradio/wasm is built around several key components:
WorkerProxy class manages web workers (both dedicated and shared) for isolated Python executionCore functionality for managing WebAssembly workers that execute Python code through Pyodide. Handles initialization, code execution, and cleanup.
class WorkerProxy extends EventTarget {
constructor(options: WorkerProxyOptions);
runPythonCode(code: string): Promise<void>;
runPythonFile(path: string): Promise<void>;
postMessageAsync<T>(msg: InMessage): Promise<T>;
terminate(): void;
}
interface WorkerProxyOptions {
gradioWheelUrl: string;
gradioClientWheelUrl: string;
files: Record<string, EmscriptenFile | EmscriptenFileUrl>;
requirements: string[];
sharedWorkerMode: boolean;
}ASGI-compliant HTTP request handling through the WebAssembly worker. Enables web applications to make HTTP requests processed by Python code.
interface HttpRequest {
method: "GET" | "POST" | "PUT" | "DELETE";
path: string;
query_string: string;
headers: Record<string, string>;
body?: Uint8Array | ReadableStream<Uint8Array> | null;
}
interface HttpResponse {
status: number;
headers: Record<string, string>;
body: Uint8Array;
}WebAssembly-based server-sent events implementation for real-time communication between the browser and Python code.
class WasmWorkerEventSource {
constructor(workerProxy: WorkerProxy, url: URL);
readonly readyState: number;
readonly url: URL;
onopen: ((ev: Event) => any) | undefined;
onmessage: ((ev: MessageEvent) => any) | undefined;
onerror: ((ev: Event) => any) | undefined;
close(): void;
}Virtual file system operations within the worker environment for managing files and data.
writeFile(path: string, data: string | ArrayBufferView, opts?: Record<string, unknown>): Promise<void>;
renameFile(oldPath: string, newPath: string): Promise<void>;
unlink(path: string): Promise<void>;Python package installation and management within the WebAssembly environment.
install(requirements: string[]): Promise<void>;Python code completion functionality using Jedi for development environments.
interface CodeCompletionRequest {
code: string;
line: number;
column: number;
}
interface CodeCompletion {
label: string;
type: string;
docstring: string;
completion_prefix_length: number;
}
type CodeCompletionResponse = CodeCompletion[];Svelte framework integration with context management and specialized components for WebAssembly worker integration.
function setWorkerProxyContext(workerProxy: WorkerProxy): void;
function getWorkerProxyContext(): WorkerProxy | undefined;
function should_proxy_wasm_src(src: string | undefined | null): boolean;
function resolve_wasm_src(src: string | undefined | null): Promise<string | undefined | null>;Network-related utilities for host validation and URL processing in Gradio Lite applications.
const FAKE_LITE_HOST = "lite.local";
function is_self_host(url: URL): boolean;Core type definitions used across the package:
interface EmscriptenFile {
data: string | ArrayBufferView;
opts?: Record<string, unknown>;
}
interface EmscriptenFileUrl {
url: string;
opts?: Record<string, unknown>;
}
type ASGIScope = Record<string, unknown>;