Read the statement by Michael Teeuw here.
Executing python in js - troublshooting/development
-
@beejay22 I copy/pasted your code, and only see a black screen cause there is nothing to display…
there is a bug in the node_helper
const spawn = require("child_process").spawn var NodeHelper = require("node_helper") module.exports = NodeHelper.create({ socketNotificationReceived: function(notification, payload) { switch(notification) { case "GIVE_ME_DATA": console.log("received notification") this.job() break } }, job: function() { // type A, you specify the pgm to execute your script HERE var process = spawn("python",["/home/sam/Desktop/realTester.py"]) // type B, you specify the pgm to execute your script IN the script. var process = spawn("/home/sam/Desktop/realTester.py") process.stdout.on("data", (data)=>{ console.log(data) var result = String.fromCharCode.apply(null, new Uint16Array((data))) // < --- you processed data to result this.sendSocketNotification("HERE_IS_DATA", data) // < ---- then sent data, which is still a 'buffer' }) } })
i fixed to this
onst spawn = require("child_process").spawn var NodeHelper = require("node_helper") module.exports = NodeHelper.create({ socketNotificationReceived: function(notification, payload) { switch(notification) { case "GIVE_ME_DATA": console.log("received notification") this.job() break } }, job: function() { // type A, you specify the pgm to execute your script HERE var process = spawn("python",["/home/sam/Desktop/realTester.py"]) // type B, you specify the pgm to execute your script IN the script. // I prefer this , before u needed two things, python and the script.. // but if u changed the script to bash, or java, or whatever, then the app would break.. var process = spawn("/home/sam/Desktop/realTester.py") process.stdout.on("data", (data)=>{ console.log(data.toString()) // debugging, log string //var result = String.fromCharCode.apply(null, new Uint16Array((data))) this.sendSocketNotification("HERE_IS_DATA", data.toString()) // pass the string, module expects string }) } })
the script for example is
#!/usr/bin/python3 print("hello");
-
Thanks for the insightful reply! I changed my node_helper to what you posted:
const spawn = require("child_process").spawn var NodeHelper = require("node_helper") module.exports = NodeHelper.create({ socketNotificationReceived: function(notification, payload) { switch(notification) { case "GIVE_ME_DATA": console.log("received notification") this.job() break } }, job: function(){ var process = spawn("/home/pi/Desktop/realTester.py") process.stdout.on("data", (data)=>{ console.log(data.toString()) this.sendSocketNotification("HERE_IS_DATA", data.toString()) }) } })
However, when I changed it I got an error message in the terminal which hasn’t happened before. Error message in terminal:
[2020-08-23 22:36:26.161] [ERROR] Error: spawn /home/pi/Desktop/testing.py EACCES at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19) at onErrorNT (internal/child_process.js:456:16) at processTicksAndRejections (internal/process/task_queues.js:84:9) { errno: 'EACCES', code: 'EACCES', syscall: 'spawn /home/pi/Desktop/realTester.py', path: '/home/pi/Desktop/realTester.py', spawnargs: [] } [2020-08-23 22:36:26.171] [LOG] MagicMirror will not quit, but it might be a good idea to check why this happened. Maybe no internet connection? [2020-08-23 22:36:26.172] [LOG] If you think this really is an issue, please open an issue on GitHub: https://github.com/MichMich/MagicMirror/issues [2020-08-23 22:36:27.069] [LOG] received notification [2020-08-23 22:36:27.094] [LOG] Whoops! There was an uncaught exception...
Still getting the same error log in the magic mirror using dev mode though… lastly this is my pyscript, realTester.py:
from bs4 import BeautifulSoup import requests url=requests.get("https://www.whitehouse.gov/presidential-actions/") src=url.content soup = BeautifulSoup(src, 'lxml') for h2_tag in soup.findAll('h2'): a_tag = h2_tag.find('a') print(a_tag.string)
-
@beejay22 sorry
2more things.
- u need to add the 1st line to the script to tell the system what processor to use
#!/usr/bin/python
- u need to make the script executable
chmod +x /home/pi/Desktop/realTester.py
-
Okay so now my pycript looks like this:
#!/usr/bin/python from bs4 import BeautifulSoup import requests url=requests.get("https://www.whitehouse.gov/presidential-actions/") src=url.content soup = BeautifulSoup(src, 'lxml') for h2_tag in soup.findAll('h2'): a_tag = h2_tag.find('a') print(a_tag.string)
If I’m my understanding correctly, adding this shebang line lets shell know that my src code is a script, the type of interpreter we want to use and the path of said interpreter…
Also am I suppose to execute
chmod +x /home/pi/Desktop/realTester.py
in shell? If so how and when… sorry I’m still trying to understand how exactly to do all of this -
@beejay22 yes, you need to execute the chmod command once, to mark the python script as executable. do this on a terminal window, or ssh session window
ANY file in Linux can be executable.
if you try to spawn/exec a file which does not have the execute permission set, u get the access error. it is not even opened.
once the bit is set, then it is searched for the shebang, if present the identified file is exec’d and the rest of the parms are passed on.
-
Hmmm I did this in the terminal:
$ chmod +x /home/pi/Desktop/realTester.pyNothing changed… still the same error log message… I think there’s something wrong with my custom’s css file since it says it’s not loading… in my css folder there’s only a main.css file there
-
@beejay22 so, can u run the script from the terminal like before
python /home/pi/Desktop/realTester.py
and then /home/pi/Desktop/realTester.pyboth should work, produce the same results
that $ wasn’t part of the chmod -
I didn’t add the $ to the command, I just said that in my post for clarity lol. I typed just this in the command line:
chmod +x /home/pi/Desktop/realTester.pyAnd yes I can run my script in the terminal with “python3 /home/pi/Desktop/realTester.py” but not with “/home/pi/Desktop/realTester.py” because the module I’m using in my script includes bs4 which is available with python3 I believe so I have to say python3
I also tried ‘touch ~/MagicMirror/css/custom.css’, still showing same error log
-
@beejay22 u can run python3 as the shebang. I was just giving example
your node_helper code was spawning python, not python3. I just copied what u did. u can put python3 in the spawn as well
custom.css, doesn’t matter, it’s empty so no harm
-
Tried this (all I changed was “python” to “python3”):
const spawn = require("child_process").spawn var NodeHelper = require("node_helper") module.exports = NodeHelper.create({ socketNotificationReceived: function(notification, payload) { switch(notification) { case "GIVE_ME_DATA": this.job() break } }, job: function() { var process = spawn("python3", ["/home/pi/Desktop/realTester.py"]) process.stdout.on("data", (data)=>{ console.log(data) var result = String.fromCharCode.apply(null, new Uint16Array((data))) this.sendSocketNotification("HERE_IS_DATA", data) }) } })
And also changed pyscript to this (just added python3 to the shebang):
#!/usr/bin/python3 from bs4 import BeautifulSoup import requests url=requests.get("https://www.whitehouse.gov/presidential-actions/") src=url.content soup = BeautifulSoup(src, 'lxml') for h2_tag in soup.findAll('h2'): a_tag = h2_tag.find('a') print(a_tag.string)
Also tested “python” again (not python3) in my node_helper along with the updated shebang as shown above
If custom.css file doesn’t matter I wonder why it’s giving this error… Have no clue what’s going on now lol