Run Python scripts from a Blueprint application
npm install @onehilltech/blueprint-python --save
You run a Python script by first injecting the python
service into a Blueprint component,
such as a controller, action, service, or policy. After injecting the service, call the
run (args, options)
method on the service. This is an asynchronous method. The return
value is the standard output (i.e., stdout) of the script.
const { Action, Controller, service } = require ('@onehilltech/blueprint');
module.exports = Controller.extend ({
__invoke () {
return Action.extend ({
/// Inject the Python service into the action.
python: service (),
async execute (req, res) {
const result = await this.python.run (script);
return res.status (200, { result } );
}
});
}
});
You can pass arguments to the python script by passing an array as the first
parameter of the run (args, options)
method.
const { Action, Controller, service } = require ('@onehilltech/blueprint');
module.exports = Controller.extend ({
__invoke () {
return Action.extend ({
/// Inject the Python service into the action.
python: service (),
async execute (req, res) {
const result = await this.python.run ([script, arg1, arg2]);
return res.status (200, { result } );
}
});
}
});
The last parameter of run (args, options)
are options for configuring the execution
process that runs the Python script. The options supported are the same as the options
from process.spawn
.
Happy Coding!