You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+89-22
Original file line number
Diff line number
Diff line change
@@ -23,25 +23,38 @@ npm test
23
23
24
24
## Documentation
25
25
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) throwerr;
33
+
console.log('finished');
34
+
});
35
+
```
36
+
37
+
If the script exits with a non-zero code, an error will be thrown.
38
+
26
39
### Running a Python script:
27
40
28
-
```js
29
-
varPythonShell=require('python-shell');
41
+
```typescript
42
+
import {PythonShell} from'python-shell';
30
43
31
44
PythonShell.run('my_script.py', function (err) {
32
45
if (err) throwerr;
33
46
console.log('finished');
34
47
});
35
48
```
36
49
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.
38
51
39
52
### Running a Python script with arguments and options:
40
53
41
-
```js
42
-
varPythonShell=require('python-shell');
54
+
```typescript
55
+
import {PythonShell} from'python-shell';
43
56
44
-
var options = {
57
+
let options = {
45
58
mode: 'text',
46
59
pythonPath: 'path/to/python',
47
60
pythonOptions: ['-u'], // get print results in real-time
@@ -58,9 +71,9 @@ PythonShell.run('my_script.py', options, function (err, results) {
58
71
59
72
### Exchanging data between Node and Python:
60
73
61
-
```js
62
-
varPythonShell=require('python-shell');
63
-
var pyshell =newPythonShell('my_script.py');
74
+
```typescript
75
+
import {PythonShell} from'python-shell';
76
+
let pyshell =newPythonShell('my_script.py');
64
77
65
78
// sends a message to the Python script via stdin
66
79
pyshell.send('hello');
@@ -92,9 +105,10 @@ For more details and examples including Python source code, take a look at the t
92
105
93
106
### Error Handling and extended stack traces
94
107
95
-
An error will be thrown if the process exits with a non-zero exit code or if data has been written to stderr. Additionally, if "stderr" contains a formatted Python traceback, the error is augmented with Python exception details including a concatenated stack trace.
108
+
An error will be thrown if the process exits with a non-zero exit code. Additionally, if "stderr" contains a formatted Python traceback, the error is augmented with Python exception details including a concatenated stack trace.
96
109
97
110
Sample error with traceback (from test/python/error.py):
traceback: 'Traceback (most recent call last):\n File "test/python/error.py", line 6, in <module>\n divide_by_zero()\n File "test/python/error.py", line 4, in divide_by_zero\n print 1/0\nZeroDivisionError: integer division or modulo by zero\n',
110
126
executable: 'python',
@@ -113,7 +129,9 @@ would result into the following error:
113
129
args: null,
114
130
exitCode: 1 }
115
131
```
132
+
116
133
and `err.stack` would look like this:
134
+
117
135
```
118
136
Error: ZeroDivisionError: integer division or modulo by zero
119
137
at PythonShell.parseError (python-shell/index.js:131:17)
@@ -141,6 +159,7 @@ Creates an instance of `PythonShell` and starts the Python process
141
159
*`binary`: data is streamed as-is through `stdout` and `stdin`
142
160
*`formatter`: each message to send is transformed using this method, then appended with "\n"
143
161
*`parser`: each line of data (ending with "\n") is parsed with this function and its result is emitted as a message
162
+
*`stderrParser`: each line of logs (ending with "\n") is parsed with this function and its result is emitted as a message
144
163
*`encoding`: the text encoding to apply on the child process streams (default: "utf8")
145
164
*`pythonPath`: The path where to locate the "python" executable. Default: "python"
146
165
*`pythonOptions`: Array of option switches to pass to "python"
@@ -154,23 +173,25 @@ PythonShell instances have the following properties:
154
173
*`command`: the full command arguments passed to the Python executable
155
174
*`stdin`: the Python stdin stream, used to send data to the child process
156
175
*`stdout`: the Python stdout stream, used for receiving data from the child process
157
-
*`stderr`: the Python stderr stream, used for communicating errors
176
+
*`stderr`: the Python stderr stream, used for communicating logs & errors
158
177
*`childProcess`: the process instance created via `child_process.spawn`
159
178
*`terminated`: boolean indicating whether the process has exited
160
179
*`exitCode`: the process exit code, available after the process has ended
161
180
162
181
Example:
163
-
```js
182
+
183
+
```typescript
164
184
// create a new instance
165
-
var shell =newPythonShell('script.py', options);
185
+
let shell =newPythonShell('script.py', options);
166
186
```
167
187
168
188
#### `#defaultOptions`
169
189
170
190
Configures default options for all new instances of PythonShell.
@@ -182,32 +203,53 @@ Runs the Python script and invokes `callback` with the results. The callback con
182
203
This method is also returning the `PythonShell` instance.
183
204
184
205
Example:
185
-
```js
206
+
207
+
```typescript
186
208
// run a simple script
187
209
PythonShell.run('script.py', function (err, results) {
188
210
// script finished
189
211
});
190
212
```
191
213
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
+
192
229
#### `.send(message)`
193
230
194
231
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.
195
232
196
233
Example:
197
-
```js
234
+
235
+
```typescript
198
236
// send a message in text mode
199
-
var shell =newPythonShell('script.py', { mode:'text '});
237
+
let shell =newPythonShell('script.py', { mode: 'text '});
200
238
shell.send('hello world!');
201
239
202
240
// send a message in JSON mode
203
-
var shell =newPythonShell('script.py', { mode:'json '});
241
+
let shell =newPythonShell('script.py', { mode: 'json '});
Parses incoming data from the Python script written via stdout and emits `message` events. This method is called automatically as data is being received from stdout.
210
248
249
+
#### `.receiveStderr(data)`
250
+
251
+
Parses incoming logs from the Python script written via stderr and emits `stderr` events. This method is called automatically as data is being received from stderr.
252
+
211
253
#### `.end(callback)`
212
254
213
255
Closes the stdin stream, allowing the Python script to finish and exit. The optional callback is invoked when the process is terminated.
@@ -216,25 +258,50 @@ Closes the stdin stream, allowing the Python script to finish and exit. The opti
216
258
217
259
Terminates the python script, the optional end callback is invoked if specified. A kill signal may be provided by `signal`, if `signal` is not specified SIGTERM is sent.
218
260
261
+
#### `checkSyntax(code:string)`
262
+
263
+
Checks the syntax of the code and returns a promise.
264
+
Promise is rejected if there is a syntax error.
265
+
266
+
#### `checkSyntaxFile(filePath:string)`
267
+
268
+
Checks the syntax of the file and returns a promise.
269
+
Promise is rejected if there is a syntax error.
270
+
219
271
#### event: `message`
220
272
221
273
Fires when a chunk of data is parsed from the stdout stream via the `receive` method. If a `parser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode.
222
274
223
275
Example:
224
-
```js
276
+
277
+
```typescript
225
278
// receive a message in text mode
226
-
var shell =newPythonShell('script.py', { mode:'text '});
279
+
let shell =newPythonShell('script.py', { mode: 'text '});
227
280
shell.on('message', function (message) {
228
281
// handle message (a line of text from stdout)
229
282
});
230
283
231
284
// receive a message in JSON mode
232
-
var shell =newPythonShell('script.py', { mode:'json '});
285
+
let shell =newPythonShell('script.py', { mode: 'json '});
233
286
shell.on('message', function (message) {
234
287
// handle message (a line of text from stdout, parsed as JSON)
235
288
});
236
289
```
237
290
291
+
#### event: `stderr`
292
+
293
+
Fires when a chunk of logs is parsed from the stderr stream via the `receiveStderr` method. If a `stderrParser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode.
294
+
295
+
Example:
296
+
297
+
```typescript
298
+
// receive a message in text mode
299
+
let shell =newPythonShell('script.py', { mode: 'text '});
300
+
shell.on('stderr', function (stderr) {
301
+
// handle stderr (a line of text from stderr)
302
+
});
303
+
```
304
+
238
305
#### event: `close`
239
306
240
307
Fires when the process has been terminated, with an error or not.
0 commit comments