Skip to content

Commit dd1374e

Browse files
committed
adding runString to documentation and fixing old exit criteria
1 parent 7bc4968 commit dd1374e

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ npm test
2323

2424
## Documentation
2525

26+
### Running python code:
27+
28+
```typescript
29+
import {PythonShell} from 'python-shell';
30+
31+
PythonShell.runString('x=1+1;print(x)', function (err) {
32+
if (err) throw err;
33+
console.log('finished');
34+
});
35+
```
36+
37+
If the script exits with a non-zero code, an error will be thrown.
38+
2639
### Running a Python script:
2740

2841
```typescript
@@ -34,7 +47,7 @@ PythonShell.run('my_script.py', function (err) {
3447
});
3548
```
3649

37-
If the script writes to stderr or exits with a non-zero code, an error will be thrown.
50+
If the script exits with a non-zero code, an error will be thrown.
3851

3952
### Running a Python script with arguments and options:
4053

@@ -198,6 +211,21 @@ PythonShell.run('script.py', function (err, results) {
198211
});
199212
```
200213

214+
#### `#runString(code, options, callback)`
215+
216+
Runs the Python code and invokes `callback` with the results. The callback contains the execution error (if any) as well as an array of messages emitted from the Python script.
217+
218+
This method is also returning the `PythonShell` instance.
219+
220+
Example:
221+
222+
```typescript
223+
// run a simple script
224+
PythonShell.run('x=1;print(x)', function (err, results) {
225+
// script finished
226+
});
227+
```
228+
201229
#### `.send(message)`
202230

203231
Sends a message to the Python script via stdin. The data is formatted according to the selected mode (text or JSON), or through a custom function when `formatter` is specified.

0 commit comments

Comments
 (0)