Python node js module

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

🌉 . Bridge to interoperate Node.js and Python

License

extremeheat/JSPyBridge

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Interoperate Node.js and Python. You can run Python from Node.js, or run Node.js from Python. Work in progress.

Requires Node.js 14 and Python 3.8 or newer.

  • Ability to call async and sync functions and get object properties with a native feel
  • Built-in garbage collection
  • Bidirectional callbacks with arbitrary arguments
  • Iteration and exception handling support
  • Object inspection allows you to easily console.log or print() any foreign objects
  • (Bridge to call Python from JS) Python class extension and inheritance. See pytorch and tensorflow examples.
  • (Bridge to call JS from Python) Native decorator-based event emitter support
  • (Bridge to call JS from Python) First-class Jupyter Notebook/Google Colab support. See some Google Colab uses below.

See some examples here. See documentation below and in here.

Access JavaScript from Python

from javascript import require, globalThis chalk, fs = require("chalk"), require("fs") print("Hello", chalk.red("world!"), "it's", globalThis.Date().toLocaleString()) fs.writeFileSync("HelloWorld.txt", "hi!")

Access Python from JavaScript

Make sure to have the dependencies installed before hand!

import  python > from 'pythonia' // Import tkinter const tk = await python('tkinter') // All Python API access must be prefixed with await const root = await tk.Tk() // A function call with a $ suffix will treat the last argument as a kwarg dict const a = await tk.Label$(root,  text: 'Hello World' >) await a.pack() await root.mainloop() python.exit() // Make sure to exit Python in the end to allow node to exit. You can also use process.exit.

Check out some cool examples below! Try them on Gitpod! Click the Open in Gitpod link above, and then open the examples folder.

Bridge feature comparison

Unlike other bridges, you may notice you’re not just writing Python code in JavaScript, or vice-versa. You can operate on objects on the other side of the bridge as if the objects existed on your side. This is achieved through real interop support: you can call callbacks, and do loss-less function calls with any arguments you like (with the exception of floating points percision of course).

python(ia) bridge javascript bridge npm:python-bridge
Garbage collection
Class extension support Not built-in (rare use case), can be manually done with custom proxy
Passthrough stdin ❌ (Standard input is not piped to bridge processes. Instead, listen to standard input then expose an API on the other side of the bridge recieve the data.)
Passthrough stdout, stderr
Long-running sync calls
Long-running async calls ❌ (need to manually create new thread) ✔ (AsyncTask) ❌ (need to manually create new thread)
Callbacks
Call classes
Iterators
Inline eval
Dependency Management
Local File Imports
Error Management
Object inspection

You can import the bridge module with

from javascript import require

This will import the require function which you can use just like in Node.js. This is a slightly modified require function which does dependency management for you. The first paramater is the name or location of the file to import. Internally, this calls the ES6 dynamic import() function. Which supports both CommonJS and ES6 modules.

If you are passing a module name (does not start with / or include a .) such as ‘chalk’, it will search for the dependency in the internal node_module folder and if not found, install it automatically. This install will only happen once, it won’t impact startup afterwards.

The second paramater to the built-in require function is the version of the package you want, for example require(‘chalk’, ‘^3’) to get a version greater than major version 3. Just like you would if you were using npm install . It’s reccomended to only use the major version as the name and version will be internally treated as a unique package, for example ‘chalk—^3’. If you leave this empty, we will install latest version instead, or use the version that may already be installed globally.

  • All function calls to JavaScript are thread synchronous
  • ES6 classes can be constructed without new
  • ES5 classes can be constructed with the .new psuedo method
  • Use @On decorator when binding event listeners. Use off() to disable it.
  • All callbacks run on a dedicated callback thread. DO NOT BLOCK in a callback or all other events will be blocked. Instead:
  • Use the @AsyncTask decorator when you need to spawn a new thread for an async JS task.

Let’s say we have a file in JS like this called time.js .

function whatTimeIsIt()  return (new Date()).toLocaleString() > module.exports =  whatTimeIsIt >

Then we can call it from Python !

from javascript import require time = require('./time.js') print(time.whatTimeIsIt())

You must use the provided On, Once, decorator and off function over the normal dot methods.

const  EventEmitter > = require('events') class MyEmitter extends EventEmitter  counter = 0 inc()  this.emit('increment', ++this.counter) > > module.exports =  MyEmitter >
from javascript import require, On, off MyEmitter = require('./emitter.js') # New class instance myEmitter = MyEmitter() # Decorator usage @On(myEmitter, 'increment') def handleIncrement(this, counter): print("Incremented", counter) # Stop listening. `this` is the this variable in JS. off(myEmitter, 'increment', handleIncrement) # Trigger the event handler myEmitter.inc()
function MyClass(num)  this.getNum = () => num > module.exports =  MyClass >
MyEmitter = require('./es5.js') myClass = MyClass.new(3) print(myClass.getNum())
module.exports =  items: [5, 6, 7, 8] >
items = require('./items.js') for item in items: print(item)
export function method(cb, salt)  cb(42 + salt) >
method = require('./callback').method # Example with a lambda, but you can also pass a function ref method(lambda v: print(v), 2) # Prints 44
Оцените статью