JSPython Developer Guide
A core objective for JSPython is to bring Python-like scripting to the JavaScript infrastructure. So, any Python documentation should work within JSPython environment. We implemented most essential features in Python.
Variables and object and arrays#
We follow the Python way of declaring and using variables. However, we create JavaScript objects and run JavaScript engine lifecycles.
Create new object
or arrays
As a result, you can use all functions available in Array, Object
Working with dates#
We supply built-in functions dateTime([strDate]) that return JavaScript Date object. Along, with that, you can use any of their date functions.
Functions#
Functions can be defined with def and async def or with an arrow =>. All types of functions can return a value by specifying return keyword or as a last statement in a code block. You can have nested functions as well.
Functions defines as def func1():#
Defining a function, just the same way as in Python.
Important limitation: with that syntax you can't call methods what returns Promise. (e.g. http calls), but you can use it as a callback function.
Functions defines as async def func1:#
Same as one above, but in this function you can call functions what returns Promises (e.g. http calls).
Important message: When you pass this function as a callback function. Make sure this function support Promises. Otherwise it will lead to unexpected behaviours. Most of the functions do not accept Promises as a callback. Including Array functions like (map, filter).
Functions defines as => arrow functions#
This feature is not available in Python. Python has only a single line lambdas.
It is quite popular technique to use callback functions in JavaScript and Python. In JavaScript it is probably more popular. You can use anonymous functions defined with => . See JavaScript examples.
In this example we are using a single line and multiline array functions
or simpler syntax
By leveraging a standard JavaScript Array functions we transformed numbers array into array of objects
Recursion#
As in Python and most other languages. The functions can be invoked recursively.
Loops#
Same as in python, we do support for, while loops - along with break and continue keywords
for loop#
a Python's style for loop
while loop#
a Python's style while loop
Build-in functions and objects#
JSPython interpreter includes only a few functions and objects. However, you can easily extend it in your app.
Functions#
- print(args1, [moreArgs]) - a function that prints input to the console. Same as
console.login javascript - dateTime([strDate]) - a function that returns JavaScript
Dateobject - deleteProperty(obj, propertyName) - deletes property from the object
- jsPython() - returns a version of jsPython interpreter
- range(start[,stop, step]) - returns a range array. Same as python range function
Objects#
- Math - JavaScript's
Mathobject along with all functions. - Object - JavaScript's
Objectobject along with all functions. - Array - JavaScript's
Arrayobject along with all functions.
Importing libraries, functions#
You can import functions or objects from other libraries the same way as you would do it in Python. But, before, you would have to make sure your library is installed. When you are working server side (NodeJS) and running your JSPython scripts with jspython-cli then it is enough just to do npm install. But, a client side Web App external library has to be registered.
As an example, we registered dataPipe library and is ready to be imported in our Playground