Skip to content

Commit ed3a518

Browse files
committed
Added doc jspython-interpreter
1 parent b37cf30 commit ed3a518

File tree

3 files changed

+119
-8
lines changed

3 files changed

+119
-8
lines changed

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const input = 'src/interpreter.ts';
77

88
export default [{
99
input,
10-
output: { file: pkg.main, name: 'jspython', format: 'umd', sourcemap: true, compact: true, globals: {} },
10+
output: { file: pkg.main, name: 'jspython', format: 'umd', sourcemap: true, compact: true },
1111
external: [],
1212
treeshake: true,
1313
plugins: [

website/docs/jspython-interpreter.md

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,112 @@ title: How to run JSPython interpreter
44
sidebar_label: JSPython interpreter
55
---
66

7+
## Install JS Python Interpreter
8+
9+
```
10+
npm i jspython-interpreter
11+
```
12+
713
## How to use JS Python Interpreter
814

15+
#### ECMAScript module
16+
17+
```js
18+
import { jsPython } from 'jspython-interpreter';
19+
```
20+
21+
#### CommonJS module
22+
23+
```js
24+
const jsPython = require('jspython-interpreter').jsPython;
25+
```
26+
27+
#### Append `<script>` in html
28+
29+
```html
30+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jspython-interpreter/dist/jspython-interpreter.min.js"></script>
31+
32+
<script>
33+
const jsPython = window.jspython.jsPython
34+
</script>
35+
```
36+
37+
## Create interpreter and evaluate script.
38+
39+
```js
40+
const script = `
41+
x = 2 + 3
42+
x
43+
`;
44+
const interpreter = jsPython();
45+
interpreter.evaluate(script).then(res => {
46+
consoe.log(res); // 5
47+
})
48+
```
49+
950
## Expose functions for JSPython import
1051

11-
### In browser app
52+
### Install needed modules localy using `npm install moduleName` command.
53+
54+
### Register package loader function to use js libraries in your script.
55+
#### In browser app
56+
```js
57+
import * as papaparse from 'papaparse';
58+
import * as dataPipe from 'datapipe-js';
59+
60+
const AVAILABLE_PACKAGES = {
61+
papaparse,
62+
'datapipe-js': dataPipe
63+
};
64+
65+
interpreter.registerPackagesLoader((packageName) => {
66+
return AVAILABLE_PACKAGES[packageName];
67+
});
68+
69+
```
70+
71+
#### In NodeJS
72+
```js
73+
interpreter.registerPackagesLoader((packageName) => {
74+
return require(packageName);
75+
});
76+
```
77+
### Import js libraries in script
78+
```js
79+
import axios // import library
80+
from fs import readFileSync as rfs, writeFileSync // import only methods from library
81+
import json5 as JSON5 // use as to provide appropriate name for lib.
82+
res = axios.get("data/url")
83+
```
84+
85+
## Register to the global scope
86+
#### Make custom function available in script.
87+
```js
88+
interpreter.addFunction('greeting', (name) => {
89+
return 'Hello ' + name + '!';
90+
});
1291

13-
### In NodeJS
92+
await interpreter.evaluate('greeting("John")') // Hello John.
93+
```
1494

15-
## Register functions to the global scope
95+
#### Make object availabel in script
96+
```js
97+
interpreter.assignGlobalContext({
98+
method1: () => 1,
99+
prop1: 2
100+
})
101+
const script = `
102+
v = method1() + prop1
103+
v
104+
`
105+
await interpreter.evaluate(script) // 3.
106+
```
16107

17108
## Provide evaluation context
109+
```js
110+
const script = `
111+
v = prop1 + prop2
112+
v
113+
`
114+
await interpreter.evaluate(script, {prop1: 3, prop2: -2}) // 1
115+
```

website/src/components/JSPythonEditor.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
import React from 'react';
22
import AceEditor from "react-ace";
3-
43
import '../../../src/assets/mode-jspython';
54
import 'ace-builds/src-noconflict/theme-github';
5+
import 'ace-builds/src-noconflict/theme-tomorrow_night';
66

77
class JSPythonEditor extends React.Component {
88
constructor(props) {
99
super(props);
1010
this.state = {
11-
value: this.props.value
11+
value: this.props.value,
12+
theme: this.theme
1213
}
13-
this.onChange = this.onChange.bind(this)
14+
this.onChange = this.onChange.bind(this);
15+
document.querySelector('.navbar .react-toggle').addEventListener('click', () => {
16+
setTimeout(() => {
17+
this.setState({
18+
theme: this.theme
19+
})
20+
});
21+
});
22+
}
23+
24+
get theme() {
25+
const appTheme = document.querySelector('html').getAttribute('data-theme');
26+
return appTheme === 'dark' ? 'tomorrow_night' : 'github'
1427
}
1528

1629
onChange(newValue) {
@@ -33,7 +46,7 @@ class JSPythonEditor extends React.Component {
3346
mode="python"
3447
height="100%"
3548
width="100%"
36-
theme="github"
49+
theme={this.state.theme}
3750
value={this.state.value}
3851
onChange={this.onChange}
3952
editorProps={{ $blockScrolling: true }}

0 commit comments

Comments
 (0)