DEV Community

Pavlo Paska
Pavlo Paska

Posted on • Originally published at worksheet.systems

Why we introduced our new coding language - JSPython?

I’ve worked with a dozen programming languages during my career in software development. Anything from Basic, Pascal, C* to Python, JavaScript, and more. And here we are, fast forward 25 years and I had no other option than to invent another coding language. With all questions asking, ‘why?’

Do we want to learn another programming language? - No, Python is the most popular language in the world

Do we have time to rebuild libraries for all the needs? - No, JavaScript has huge communities and a huge EcoSystem around it.

We had a requirement to allow users to define custom logic at runtime safely. We tried a JSON-based configuration, but, as complexities grew, that configuration became unmaintainable. Even UI-based drag’n drop interfaces become messy for bigger and more complex logic. And after a few failed attempts I decided to build my interpreter and introduce a new coding language to the world. And syntax-wise I choose to start with Python as it is the most familiar syntax among our target audience.

JSPython is a python-like syntax interpreter implemented with javascript that runs entirely in the web browser and/or in the NodeJS environment. It does not transpile/compile your code into JavaScript, instead, it provides an interactive interpreter that reads Python-like code and carries out its instructions. With JSPython you should be able to safely interact with any JavaScript libraries or API’s with the popular Python-like syntax.

With JSPython you can use any JavaScript library with Python-like syntax. You can use standard Javascript objects, arrays, callback functions, as well as build your custom logic with JavaScript and expose it to JSPython users

Main advantages of JSPython

  • Safe scripting environment. You have full control over what functions and libraries you expose to JSPython and allow users to use.
  • Works in a browser as well as offline on the server-side. You can integrate it easily with your web app in the browser and/or run jspy files offline with jspython-cli in the NodeJS environment
  • The python-like syntax. Most of the language features (syntax, indentation, and expressions) are mirrored from Python. Also, added a few extras like Multiline lambdas, Null coalescing, single line conditions, etc
  • Utilize the entire JavaScript ecosystem. Leverage a massive JavaScript ecosystem with Python scripting. You should be able to import and invoke any JS function with the Python syntax
  • Rapid Application Development. The seamless interaction between Python and JavaScript allows developers to enjoy the best of two worlds. And deliver business solutions much quicker and more reliably.

Where to start?

JSPython is an open source JavaScript library with a permissive BSD 3-Clause License. Published to npm as jspython-interpreter

Run JSPython in your JavaScript App

npm install jspython-interpreter

jsPython()
  .evaluate('print("Hello World!")')            
  .then(
    r => console.log("Result => ", r),            
    e => console.log("Error => ", error)            
  )
Enter fullscreen mode Exit fullscreen mode

Run JSPython in NodeJS with JSPython-CLI

Use JSPython-cli command line tool what allows you to run JSPython in NodeJS environment

jspython --file=path/to/jspython/file --param1=value --param

Any similar projects around?

Obviously, before committing to such a big endeavour I’ve looked around for existing libraries. The closest library is Skulpt but their API isn’t flexible enough and we couldn’t utilize our JavaScript libraries

Performance

Many people are asking about performance and maturity of the project. How does it compare to Python performance-wise?

Well, it is very comparable to CPython, mainly because CPython is quite slow :), while Google’s V8 JavaScript engine is extremely optimized and performant. You will find that NodeJS will significantly outperform any CPython project. So, I can confidently say that JSPython is fast enough to serve most of your custom workflow and behaviour logic. And is already used in several performance critical production systems.

Coding playground

Check out our basic playground https://jspython.dev/playground

Or more advanced examples build with WORKSHEETS Data Studio

sample code

# initial array
nums = [1, 2]
""" 
You can use all standard JavaScript functions 
(https://www.w3schools.com/jsref/jsref_obj_array.asp). 
"""

# add more numbers
for i in range(3,10):
    nums.push(i)

# `filter` will leave only odd numbers
# `map` will create an object
# y will be an array of objects transformed from odd numbers
y = nums
    .filter(n => n % 2)
    .map((n, i) =>
        newItem = {
            index: i,
            element: n,
            square: Math.pow(n, 2),
            cube: Math.pow(n, 3)
        }
        return newItem
    )     


print('Prints to console =>', y)

# last statement will be a result
return y
Enter fullscreen mode Exit fullscreen mode

Worksheet Systems

JSPython was build to power our low code data management platform - Worksheet Systems. Which allows anybody to build database applications with ease, control and confidence. JSPython is used as a primary low code language to define UI behaviours, validations, build data transformational pipelines and much more.

However, it is open source project with a permissive BSD 3-Clause License. So, feel free to adopt it into your own projects.

Discussion (0)