Mini Racer

0.14.1 · active · verified Sun Apr 12

Mini Racer is a minimal, modern embedded V8 JavaScript engine for Python. It allows executing JavaScript code directly within Python applications. As of version 0.14.1, it provides a stable API for evaluating JS, calling functions, and managing execution contexts. The project has a relatively frequent release cadence, with minor updates and bug fixes appearing every few weeks or months.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `MiniRacer` context, evaluate JavaScript code, call a JavaScript function with Python arguments, and handle JavaScript execution errors using `MiniRacerEvalError`.

from py_mini_racer import MiniRacer, MiniRacerEvalError

# Initialize a MiniRacer context
mr = MiniRacer()

# Evaluate JavaScript code to define a function
mr.eval("function add(a, b) { return a + b; }")

# Call the JavaScript function with Python arguments
result = mr.call("add", 1, 2)
print(f"Result of add(1, 2): {result}")

# Evaluate a simple expression (e.g., getting V8 version)
js_version = mr.eval("process.versions.v8")
print(f"V8 version: {js_version}")

# Demonstrate error handling
try:
    mr.eval("throw new Error('Something went wrong!');")
excep MiniRacerEvalError as e:
    print(f"Caught MiniRacerEvalError: {e}")

view raw JSON →