|
| 1 | +# JSPython |
| 2 | +JSPython is a javascript implementation of Python language that runs within web browser or NodeJS environment. It does not transpile/compile your code into JavaScript, instead, it provides an interactive interpreter that reads Python code and carries out their instructions. With JSPython you should be able to safely use or interact any JavaScript libraries or API with a nice Python language. |
| 3 | + |
| 4 | +```py |
| 5 | +import Math |
| 6 | + |
| 7 | +arr = [4, 9, 16] |
| 8 | + |
| 9 | +def sqrt(a): |
| 10 | + return Math.sqrt(a) |
| 11 | + |
| 12 | +# use Array.map() with Python function |
| 13 | +roots = arr.map(sqrt).join(",") |
| 14 | + |
| 15 | +# use Array.map() or use arrow function |
| 16 | +roots = arr.map(i => Math.sqrt(i)).join(",") |
| 17 | + |
| 18 | +``` |
| 19 | +## Try out JSPython in a wild |
| 20 | +Interactive [Worksheet Systems JSPython editor](https://run.worksheet.systems/rest-client/jspython-editor) with an ability to query REST APIs and display results in Object Explorer, a configurable Excel-like data grid or just as a JSON or text. |
| 21 | + |
| 22 | +## Why would you use it? |
| 23 | +You can easily embed `JSPython` into your web app and your end users will benefit from a Python like scripting facility to: |
| 24 | + * build data transformation and data analysis tasks |
| 25 | + * allow users to configure a JS objects at run-time |
| 26 | + * run a comprehensive testing scenarios |
| 27 | + * experiment with your JS Libraries or features. |
| 28 | + |
| 29 | +## Features |
| 30 | +Our aim here is to provide a SAFE Python experience to Javascript or NodeJS users at run-time. So far, we implement a core set of Python feature which should be OK to start coding. |
| 31 | + |
| 32 | + * **Syntax and code flow** Same as Python, In `JSPython` we use indentation to indicate a block of code. All flow features like `if - else`, `for`, `while` loops - along with `break` and `continue` |
| 33 | + |
| 34 | + * **Objects, Arrays** `JSPython` allows you to work with JavaScript objects and arrays and you should be able to invoke their methods and properties as normal. So, all methods including prototype functions `push()`, `pop()`, `splice()` and [any more](ttps://www.w3schools.com/js/js_array_methods.asp) will work out of box. |
| 35 | + |
| 36 | + * **JSON** JSPython interpreter recognises [json5](https://json5.org/) style format. So, you can assign your variable a valid JSON as |
| 37 | + |
| 38 | + * **Functions** Functions def `def` `async def`, arrow functions `=>` - (including multiline arrow functions) |
| 39 | + |
| 40 | + * **Strings** Syntax and code flow `s = "Strings are double quoated only! For now."` represent a multiline string. A single or triple quotes are not supported yet. |
| 41 | + |
| 42 | + * **Date and Time** We have `dateTime()` function what returns JavaScript's Date object. So, you can use all Date [get](https://www.w3schools.com/js/js_date_methods.asp) and [set](https://www.w3schools.com/js/js_date_methods_set.asp) methods |
| 43 | + |
| 44 | + * **None, null** `null` and `None` are synonyms and can be used interchangeably |
| 45 | + |
| 46 | +## JSPython distinctive features |
| 47 | +Although we haven't implemented all the features available in Python yet. But, we already have a several usefull and distinctive features that are popular in other morden languages, but not in Python: |
| 48 | + - A single line arrow functions `=>` (no lambda keyword required) |
| 49 | + - A multiline arrow function `=>`. Particularly usefull when building data transformation pipelines |
| 50 | + - Null conditioning (null-coalescing) `myObj?.property?.subProperty or "N/A"`. |
| 51 | + |
| 52 | +## Quick start |
| 53 | + |
| 54 | +Zero install ! |
| 55 | +The most simple way to get started, without anything to install, is to use the distribution available online through jsDelivr. You can choose the latest stable release : |
| 56 | +``` |
| 57 | +<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/jspython.min.js"> |
| 58 | +</script> |
| 59 | +``` |
| 60 | + |
| 61 | +Or local install |
| 62 | +``` |
| 63 | +npm install jspython |
| 64 | +``` |
| 65 | +Run JS Python from your Javascript App or web page. |
| 66 | +### Basic |
| 67 | +```js |
| 68 | + jsPython() |
| 69 | + .evaluate('print("Hello Worls!")') |
| 70 | + .then( |
| 71 | + r => console.log("Result => ", r), |
| 72 | + e => console.log("Error => ", error) |
| 73 | + ) |
| 74 | +``` |
| 75 | +### Or with own data context and custom function: |
| 76 | +```js |
| 77 | + const script = ` |
| 78 | + x = [1, 2, 3] |
| 79 | + x.map(r => add(r, y)).join(",") |
| 80 | + `; |
| 81 | + const context = {y: 10} |
| 82 | + |
| 83 | + const result = await jsPython() |
| 84 | + .addFunction("add", (a, b) => a + b) |
| 85 | + .evaluate(script, context); |
| 86 | + // result will be a string "11,12,13" |
| 87 | +``` |
| 88 | +Also, you can provide entire JS Object or even a library. |
| 89 | + |
| 90 | + |
0 commit comments