انتقل إلى المحتوى

لم تُترجم هذه الصفحة بعد، لذا تُعرض بالإنجليزية.

Python elide module

The Python elide module: evaluate JavaScript with elide.js, share Python functions with elide.bind and @elide.poly(name=…), and reach telemetry controls.

في هذه الصفحة
Open in ChatGPTOpen in Claude

Python code running on Elide can import elide without installing anything.

NameEffect
elide.js(source)Evaluates a JavaScript string and returns its value. Needs --interop.
elide.bind(fn)Shares fn under its __name__; returns fn, so it works as a decorator.
elide.poly(name=…)Decorator that shares a function under name.
elide.telemetryTelemetry controls; see OpenTelemetry.

elide.js

# 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))
elide --interop python calls.py
hello-polyglot-world
3 a 2

JavaScript functions are callable, objects expose their properties as attributes, and undefined becomes None. Build a polyglot app lists every value conversion.

InputResult
Run without --interopValueError: polyglot language 'js' not found
JavaScript that throws or fails to parseRuntimeError: JSException: SyntaxError: <elide.js>:1:3 Expected an operand but found eof
A non-string argumentTypeError: elide.js() argument source must be str, not int

elide.bind and elide.poly

Both share a Python function by name. Python reads shared values with polyglot.import_value, and the REPL with /import NAME.

# shared.py
import polyglot
from elide import bind, poly

@bind
def add(a, b):
    return a + b

@poly(name="greet")
def say_hello(name):
    return f"hello {name}"

print(add(2, 3), say_hello("elide"))
print(polyglot.import_value("add")(2, 3))
print(polyglot.import_value("greet")("elide"))
elide python shared.py
5 hello elide
5
hello elide
  • Both decorators return the original function, so it stays callable from Python.
  • elide.poly(name=1) raises TypeError: elide.poly() argument name must be str, not int.
  • JavaScript files have no Polyglot global, so they cannot read shared values. To call Python from JavaScript, import the .py module instead; see Languages and interop.

See also

هل كانت هذه الصفحة مفيدة؟