Coder Social home page Coder Social logo

pythonjs's Introduction

python.js

All python codes now are part of node.js

This project uses node napi and python capi to make python a part of node.js. This library only works on node.js (not browser)

API run is a good start when you use python.js, it accepts a python code and return exitCode when it done.

Helle world, example

const pyjs = require("./python.js");
console.log(pyjs.versions);

const pyCode = `
from time import time,ctime
print('Say "Hello World" at', ctime(time()))
`;

const pyExitCode = pyjs.run(pyCode);
console.log(pyExitCode);

this outputs Hello World with current local time.

Calling js code on python side is also supported. All nodejs codes are builtin to python nodejs module, so only need import nodejs. On javascript side, pass function as well as its name (used on python side) as second parameter of run. run accepts array of function infomations. The example is like

const pyjs = require("./python.js");
console.log(pyjs.versions);

const func = (a, b) => a.length * b;

const pyCode = `
from time import time,ctime
# import nodejs module for nodejs host context
import nodejs

print('Today is:', ctime(time()))
# call nodeMultiplication using nodejs.call
print('result of node call:', nodejs.call('nodeMultiplication', '42', 100))
`;

const context = [
    {
        func,
        name: "nodeMultiplication",
    },
];

const pyExitCode = pyjs.run(pyCode, context);

console.log(pyExitCode);

JavaScript calling also supports closure

const pyjs = require("./python.js");
console.log(pyjs.versions);

function closure() {
    let inside = 50;
    return (a, b) => (a.length * b) * inside;
}

const pyCode = `
from time import time,ctime
import nodejs

print('Today is:', ctime(time()))
print('result of node call:', nodejs.call('nodeMultiplication', '42', 42))
`;

const context = [
    {
        func: closure(),
        name: "nodeMultiplication",
    },
];

const pyExitCode = pyjs.run(pyCode, context);

console.log(pyExitCode);

call import and call can invoke seperate python function using javascript.

more api declaration is in file $/src/python.js.d.ts.

Have fun! All python codes now are part of node.js!

how to build

if you don't want to make a build from source, try demo on release section.

   git clone https://github.com/asanoic/pythonjs.git --recursive
   cmake [python-js-src-path] -DSEARCH_PATH=[gawk path]
   cmake --build .

gawk is for patching some files to make them work on mingw-w64, you can find it in MSYS2 on windows you might need copy python39.dll and lib [$/capi/python/lib] to same folder of python.js.node, if you do not have python installed. build work on Linux and Windows/MinGW-w64, other platforms or compilers might need change cmake file.

build requirement

  • C++ compiler supports C++14, recommand use this
  • Python 3.x, Python 3.9.1 is included in this repo for windows, on linux you can build from source based on your install.
  • napi headers which is included in this repo.

pythonjs's People

Contributors

asanoic avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.