1.0.11 • Published 2 years ago

pyes v1.0.11

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

pyes

pyes is an ES module that allows you to run python from JavaScript

Example

import { pythonFunction, PythonInterpreter } from "pyes"

const addBody = `
def add_impl(a, b):
  return a+b

def add(args):
  return add_impl(args[0], args[1])
`

const onReady = () => {console.log("Now the interpreter is ready")}
const interpreter = new PythonInterpreter(onReady);

const add = pythonFunction(interpreter, "add", addBody)
console.log(await add([1,2]));
interpreter.stop()

How it works

Every time you define a new Python function, pyes spins up a new web worker where a python interpreter is run. It feeds the function definition to the interpreter (which is run in interactive mode). When you call the JS function that you get back as the first element of the return value of pythonFunction, pyes encodes the argument you have passed as JSON and it then feeds some code to the python interpreter which:

  • parses the JSONified argument back into a python value using json.loads
  • calls your function with said parsed value
  • gets the output of your function and it JSONify it and prints it

From JS, we then read the stdout and parse the JSON result and return it as the result of the function.

1.0.11

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago