Skip to content

Commit d4cfc6d

Browse files
committed
--- Initial commit ---
0 parents  commit d4cfc6d

21 files changed

+3875
-0
lines changed

.gitignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# compiled output
4+
/dist
5+
/tmp
6+
/out-tsc
7+
/release
8+
/lib
9+
10+
# dependencies
11+
/node_modules
12+
13+
# IDEs and editors
14+
/.idea
15+
.project
16+
.classpath
17+
.c9/
18+
*.launch
19+
.settings/
20+
*.sublime-workspace
21+
22+
# IDE - VSCode
23+
.vscode/*
24+
!.vscode/settings.json
25+
!.vscode/tasks.json
26+
!.vscode/launch.json
27+
!.vscode/extensions.json
28+
29+
# misc
30+
/.sass-cache
31+
/connect.lock
32+
/coverage
33+
/libpeerconnection.log
34+
npm-debug.log
35+
yarn-error.log
36+
testem.log
37+
/typings
38+
39+
# System Files
40+
.DS_Store
41+
Thumbs.db
42+
43+
package-lock.json
44+
*.tgz

.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/src
2+
/node_modules
3+
.gitignore
4+
index.html
5+
rollup.config.js
6+
rollup.config.dev.js
7+
jest.config.js
8+
*.tgz
9+
*.ts

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "chrome",
9+
"request": "launch",
10+
"name": "Launch Chrome against localhost",
11+
"url": "http://localhost:10001",
12+
"webRoot": "${workspaceFolder}"
13+
}
14+
]
15+
}

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules\\typescript\\lib"
3+
}

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+

index.html

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<html>
2+
3+
<head>
4+
<title>JSPython dev</title>
5+
<script src="./node_modules/json5/dist/index.min.js"></script>
6+
<script src="./node_modules/ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
7+
<script src="./lib/jspython.js"></script>
8+
<script src="./lib/assets/mode-jspython.js"></script>
9+
<style>
10+
.container {
11+
height: 100%;
12+
min-width: 200px;
13+
width: 90%;
14+
margin: 10px auto;
15+
}
16+
17+
#editor,#result {
18+
height: 40%;
19+
display: block;
20+
min-height: 20%;
21+
width: 100%;
22+
margin-top: 10px;
23+
margin-bottom: 10px;
24+
}
25+
</style>
26+
</head>
27+
28+
<body>
29+
<div class="container">
30+
<h4>PScript development console</h4>
31+
<div id="editor">
32+
x = [1,2,3,4,5,6,7,8,9]
33+
x
34+
.filter(i => i > 5)
35+
.join(",")
36+
</div>
37+
<button onclick="runInterpreter()">Run</button>
38+
<textarea id="result"></textarea>
39+
</div>
40+
<script>
41+
42+
const editor = ace.edit("editor");
43+
editor.setTheme("ace/theme/monokai");
44+
editor.session.setMode("ace/mode/python");
45+
46+
const jsPython = JSPython.jsPython;
47+
48+
async function runInterpreter() {
49+
50+
const scripts = editor.getValue();
51+
try {
52+
const result = await jsPython()
53+
.addFunction('returnsPromise', a1 => new Promise((s, f) => { setTimeout(() => s(a1), 10) }))
54+
.addFunction('nullValue', () => { console.log(' ** invoked!!!'); return null })
55+
.evaluate(scripts);
56+
document.getElementById('result').value = typeof result === 'object' ? JSON.stringify(result) : result
57+
console.log('Result => ', result);
58+
} catch (err) {
59+
document.getElementById('result').value = err;
60+
console.error(err);
61+
}
62+
}
63+
</script>
64+
</body>
65+
66+
</html>

0 commit comments

Comments
 (0)