DukPy

0.5.1 · active · verified Wed Apr 15

DukPy is a simple, lightweight JavaScript interpreter for Python, embedding the Duktape JavaScript engine. It allows executing JavaScript code directly from Python applications. As of version 0.5.1, it supports modern Python versions (up to 3.14) and maintains a steady release cadence, often aligning with new Python releases or Duktape engine updates.

Warnings

Install

Imports

Quickstart

Demonstrates basic JavaScript evaluation, passing data from Python to JavaScript, and calling Python functions from within JavaScript.

import dukpy

# Basic JavaScript evaluation
result = dukpy.evaljs("1 + 2")
print(f"1 + 2 = {result}")

# Passing data to JavaScript
result = dukpy.evaljs("a + b", data={'a': 10, 'b': 20})
print(f"a + b (with data) = {result}")

# Calling Python functions from JavaScript
def my_python_sum(x, y):
    return x + y

js_code = "var sum = my_python_sum(5, 7); sum;"
result = dukpy.evaljs(js_code, python_functions={'my_python_sum': my_python_sum})
print(f"JS calling Python function = {result}")

view raw JSON →