# Crear una aplicación políglota

Esta página crea un programa que usa un paquete de npm desde JavaScript y un paquete de PyPI desde Python, en un solo
proceso.

| Requisito | Cómo |
| --- | --- |
| Python | `elide setup python`; `elide info` muestra `python` en `Languages` |
| Llamadas entre lenguajes | `--interop` en cada ejecución; no hay ningún ajuste en el manifiesto |
| Paquetes de npm | `dependencies.npm` en `elide.pkl` y después `elide install --ecosystems=npm` |
| Paquetes de PyPI | Un entorno virtual de Python 3.13, seleccionado con `VIRTUAL_ENV` |

## Configurar el proyecto

```pkl
// elide.pkl
amends "elide:project.pkl"

name = "poly"

dependencies {
  npm { packages { "pretty-ms@9.3.1" } }
  pypi { packages { "humanize==4.12.3" } }
}
```

```bash
elide install --ecosystems=npm
```

Un `elide install` sin opciones solo instala Maven. Elide no instala paquetes de PyPI: `elide install --ecosystems=pypi`
termina con el código `2` y el mensaje `pypi installs are not available in this build`. Conserve el bloque `pypi` como
registro e instale los paquetes con el `pip` de Python 3.13 en un entorno virtual:

```bash
python3.13 -m venv .venv
.venv/bin/pip install humanize==4.12.3
export VIRTUAL_ENV=$PWD/.venv
```

Elide carga los paquetes desde `$VIRTUAL_ENV/lib/python3.13/site-packages`. Los paquetes escritos solo en Python
funcionan; algunos módulos de extensión en C no se cargan (consulte [Python](/docs/python#known-limitations)).

## Escribir el programa

```python
# report.py
import humanize

def size(n):
    return humanize.naturalsize(n)

def summarize(files):
    total = sum(f.bytes for f in files)
    return {"count": len(files), "total": humanize.naturalsize(total)}
```

```javascript
// app.mjs
import prettyMs from "pretty-ms";
import report from "./report.py";

const files = [
  { name: "build.log", bytes: 1_500_000, ageMs: 172_800_000 },
  { name: "test.log", bytes: 32_000, ageMs: 5_400_000 },
];

for (const f of files) {
  console.log(`${f.name}: ${report.size(f.bytes)}, ${prettyMs(f.ageMs)} old`);
}

const summary = report.summarize(files);
console.log(`${summary.count} files, ${summary.total}`);
```

```bash
elide --interop run app.mjs
```

```text
build.log: 1.5 MB, 2d old
test.log: 32.0 kB, 1h 30m old
2 files, 1.5 MB
```

La exportación por defecto de un archivo de Python es su módulo; impórtelo desde un punto de entrada `.mjs`. Dos fallos
habituales:

| Falta | Error |
| --- | --- |
| `--interop` | `Error: Cannot import 'report.py': JavaScript entries run without Python. Re-run with --interop to use it.` |
| `VIRTUAL_ENV` | `ModuleNotFoundError: No module named 'humanize'` |

## Pasar valores

```python
# shapes.py
def label(order):
    return f"{order.item} x{order.qty}"

def doubled(prices):
    return [p * 2 for p in prices]

def config():
    return {"region": "eu", "retries": 3}

def apply(fn, value):
    return fn(value)

def fail():
    raise ValueError("bad input")
```

```javascript
// values.mjs
import shapes from "./shapes.py";

console.log(shapes.label({ item: "bolt", qty: 4 }));
console.log(shapes.doubled([1, 2.5, 3]).map((n) => n + 1));
const config = shapes.config();
console.log(config.region, config.get("retries"));
console.log(JSON.stringify(Object.fromEntries(config.items())));
console.log(shapes.apply((s) => s.toUpperCase(), "shout"));
console.log(shapes.apply((v) => v, null), shapes.apply((v) => v, undefined));
```

```bash
elide --interop run values.mjs
```

```text
bolt x4
(3)[3, 6, 7]
eu 3
{"region":"eu","retries":3}
SHOUT
null undefined
```

| Valor | De JavaScript a Python | De Python a JavaScript |
| --- | --- | --- |
| Números, cadenas, booleanos | Valores nativos | Valores nativos |
| `null`, `undefined` / `None` | `None`, y de vuelta sin cambios | `null` |
| Objeto / `dict` | Los campos se leen como atributos (`order.item`); `order["item"]` lanza `TypeError` | Las claves se leen como `d.key`, `d["key"]` o `d.get("key")`; `Object.keys`, `in` y `JSON.stringify` ven los métodos del dict, no sus claves. Conviértalo con `Object.fromEntries(d.items())`. |
| Array / `list` | Iterable e indexable; `len()` funciona | `Array.isArray` es `true`; `map`, la sintaxis spread y `Array.from` funcionan |
| Función | Invocable | Invocable |

## Llamar a JavaScript desde Python

El módulo `elide` evalúa JavaScript y devuelve su valor:

```python
# calls.py
from elide import js

slug = js("(s) => s.trim().toLowerCase().replaceAll(' ', '-')")
print(slug("  Hello Polyglot World "))

point = js("({ x: 1, y: 2, tags: ['a', 'b'] })")
print(point.x + point.y, point.tags[0], len(point.tags))
print(sorted(js("[3, 1, 2]")))

try:
    js("JSON.parse('{oops')")
except RuntimeError as e:
    print("caught:", e)

js("Promise.resolve(42)").then(lambda v: print("resolved:", v))
```

```bash
elide --interop python calls.py
```

```text
hello-polyglot-world
3 a 2
[1, 2, 3]
caught: JSException: SyntaxError: Unexpected token o in JSON at position 1
resolved: 42
```

## Manejar errores

Una excepción de Python llega a JavaScript como un `Error` cuyo `name` es la clase de la excepción; un error de
JavaScript llega a Python como `RuntimeError`. Un error no capturado imprime una sola pila que abarca ambos lenguajes:

```javascript
// errors.mjs
import shapes from "./shapes.py";

try {
  shapes.fail();
} catch (e) {
  console.log(e.name, "|", e.message);
}

shapes.apply((x) => {
  throw new RangeError(`bad value ${x}`);
}, 7);
```

```bash
elide --interop run errors.mjs
```

```text
ValueError | ValueError: bad input
RangeError: bad value 7
    at :=> (errors.mjs:10:9)
    at apply (shapes.py:11:12)
    at :module:eval (errors.mjs:9:1)
```

El programa termina con `1`.

## Llamar a código asíncrono

| Desde | Funciona | No funciona |
| --- | --- | --- |
| JavaScript que llama a Python | Llamar a un envoltorio síncrono que ejecuta `asyncio.run(...)` | `await` sobre una corrutina de Python devuelve la corrutina sin ejecutarla |
| Python con una promesa de JavaScript | `promise.then(callback)` | `await promise` lanza `TypeError: object polyglot.ForeignObject can't be used in 'await' expression` |

```python
# rates.py
import asyncio

async def fetch_rate(code):
    await asyncio.sleep(0.01)
    return {"EUR": 0.92, "GBP": 0.79}[code]

def rate(code):
    return asyncio.run(fetch_rate(code))

def when_ready(promise, done):
    promise.then(lambda value: done(f"python saw {value}"))
```

```javascript
// rates.mjs
import rates from "./rates.py";

console.log("rate:", rates.rate("EUR"));

const total = new Promise((resolve) => setTimeout(() => resolve(7), 10));
rates.when_ready(total, (msg) => console.log(msg));
await total;
```

```bash
elide --interop run rates.mjs
```

```text
rate: 0.92
python saw 7
```

`asyncio.run` bloquea el bucle de eventos de JavaScript hasta que termina la corrutina. Los hilos de Python no están
disponibles con `--interop`.

## Java y Kotlin

`elide run Hello.kt` ejecuta Kotlin por separado; JavaScript y Python no pueden llamarlo. Importarlo falla:

```javascript
import hello from "./Hello.kt";
```

```text
TypeError: Unsupported file extension: 'file:/path/to/Hello.kt'
```

Consulte también [Lenguajes e interoperabilidad](/docs/languages), [Python](/docs/python) e
[Instalador npm](/docs/toolchain/web/npm).

## Ejemplos

- [Python que llama a JavaScript](/docs/code-samples#python-calling-javascript)
- [JavaScript que importa Python](/docs/code-samples#javascript-importing-python)
