How to execute a python code from coffee script and get the output

Viewed 57

i am developing a coffeescript which needs to execute a python program and get the responses from the python code. my coffee script is

module.exports = (robot) ->
    robot.respond /greetme/i, (msg) ->
        sender = msg.message.user.name.toLowerCase()
        @exec = require('child_process').exec
        command = "python3 ext-scripts/hello.py"
        @exec command, (out1) ->
            msg.send out1
        msg.send "Hello " + sender
        msg.finish()

and my python code is hello.py

print("hey indhu")
return "reached the python file"

i need to get the output "reached the python file" to 6th line in coffee script. to send out the message

i am getting error while doing this exec out function.

message: Error: Command failed: python3 ext-scripts/hello.py File "ext-scripts/hello.py", line 28 return "reached the python file" ^ SyntaxError: 'return' outside function

error: Response not OK: no_text

How to make it work. please help me . i am a python developer and new to coffeescript.

1 Answers

The easiest way is with a supprecess that reads the output and filters. The first version is good for Windows machines, the second for Linux systems.

import subprocess

process = subprocess.Popen(['echo', 'More output'],
                     stdout=subprocess.PIPE, 
                     stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
stdout, stderr

OR

import os

cmd = 'your command here'
terminal_output = os.system(cmd)

print(terminal_output)
Related