Ir al contenido

Python en el navegador

import { Aside } from ‘@astrojs/starlight/components’;

Ejecutá Python real dentro de la página vía Pyodide, cargado bajo demanda. Accedé vía sdk.python. Corre cualquier Python — numpy, pandas, tu propia lógica — y alcanza el mundo exterior únicamente a través de funciones JS que inyectás explícitamente.

run<T>(code, options?): Promise<{ result: T; stdout: string; stderr: string }>
ready(options?): Promise<void> // preload the runtime
reset(): void // drop the cached runtime
readonly loaded: boolean
interface PythonRunOptions {
globals?: Record<string, unknown>; // JS values/functions exposed to Python
packages?: string[]; // e.g. ['numpy', 'pandas']
install?: string[]; // pure-Python wheels via micropip
onStdout?: (line: string) => void; // streamed print() output
onStderr?: (line: string) => void;
}
const { result } = await sdk.python.run(`
import numpy as np
float(np.mean([1, 2, 3, 4]))
`, { packages: ['numpy'] });
console.log(result); // 2.5

Python puede hacer await de funciones JS async que le pasás en globals:

await sdk.python.run(`
for wk in weeks:
await run_sql(TEMPLATE.replace("{W}", wk))
log(f"{wk} done")
`, {
globals: { weeks, TEMPLATE, run_sql: async (s) => fetchData(s), log: console.log },
});

No hace falta manifest — sdk.python no almacena datos del artifact. Mantené los jobs largos divididos en chunks y transmití el progreso vía onStdout, ya que el cómputo pesado bloquea el hilo principal.