Okay. And yes it does return the data as output I believe
Read the statement by Michael Teeuw here.
Latest posts made by beejay22
-
RE: Executing python in js - troublshooting/development
-
RE: Executing python in js - troublshooting/development
Did you try it with the pyscript? Or are you talking about it the core module, node_helper and config?
And okay
-
RE: Executing python in js - troublshooting/development
Sorry I copied the wrong node_helper.js file in my last post. This is how it looks like now:
//node_helper.js 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("python3", ["/home/pi/Desktop/realTester.py"]) process.stdout.on("data", (data)=>{ console.log(data.toString()) this.sendSocketNotification("HERE_IS_DATA", data.toString()) }) } })
I was trying to test if I can at least print something to magic mirror without a pyscript… node_helper looks like this:
//node_helper.js 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("python3", ["/home/pi/Desktop/test.py"]) process.stdout.on("data", (data)=>{ data= "Hello World"; console.log(data.toString()) this.sendSocketNotification("HERE_IS_DATA", data.toString()) }) } })
Hello World appears on MagicMirror but the test file that’s provided in the path (the test file just has: print(“this works”) does not so I’m not sure why I need to provide var process spawn if Hello World prints by itself… Anyways, I’m aware that there’s other ways to execute the child process however I’m not familiar with it. Please correct me if I’m wrong but can exec work in this situation as well? (pyscipt just prints out the latest titles when magic mirror restarts). I’m trying anything and everything at this point
And I added the default and start function to my core module although I don’t think I have to use it. This is how it looks like now:
/// MMM-Whitehouse.js - Core Module Module.register("MMM-Whitehouse", { defaults: {}, start: function () {}, getDom: function() { var e = document.createElement("div") e.id = "DISPLAY" return e }, notificationReceived: function(notification, payload, sender) { switch(notification) { case "DOM_OBJECTS_CREATED": var timer = setInterval(()=>{ this.sendSocketNotification("GIVE_ME_DATA") }, 1000) break } }, socketNotificationReceived: function(notification, payload) { switch(notification) { case "HERE_IS_DATA": var e = document.getElementById("DISPLAY") e.innerHTML = payload break } }, })
-
RE: Executing python in js - troublshooting/development
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
-
RE: Executing python in js - troublshooting/development
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
-
RE: Executing python in js - troublshooting/development
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
-
RE: Executing python in js - troublshooting/development
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 -
RE: Executing python in js - troublshooting/development
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)
-
Executing python in js - troublshooting/development
Hi. I’m very new to coding and I have a pyscript that simply prints out the latest titles from a website using BeautifulSoup. However, when I launch magic mirror it’s just a black screen. I deleted all other modules besides my own in the config file so that it’s just my module that runs. I’ve done ample research and can’t seem to figure out what’s going on (updated mm software, reinstalled dependencies in the css directory just in case it wasn’t installed correctly, checked to see node was up to date - v10.21.0, etc.). I’m starting to think it may be my pyscript but it runs correctly in the console (I can attach my pyscript if needed). I’m not sure what the problem is so I appreciate any and all help!
Core module: MMM-Whitehouse
Module.register("MMM-Whitehouse", { getDom: function() { var e = document.createElement("div") e.id = "DISPLAY" return e }, notificationReceived: function(notification, payload, sender) { switch(notification) { case "DOM_OBJECTS_CREATED": var timer = setInterval(()=>{ this.sendSocketNotification("GIVE_ME_DATA") }, 1000) break } }, socketNotificationReceived: function(notification, payload) { switch(notification) { case "HERE_IS_DATA": var e = document.getElementById("DISPLAY") e.innerHTML = payload break } }, })
node_helper.js
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("python", ["/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) }) } })
config.js
var config = { address: "localhost", // Address to listen on, can be: // - "localhost", "127.0.0.1", "::1" to listen on loopback interface // - another specific IPv4/6 to listen on a specific interface // - "0.0.0.0", "::" to listen on any interface // Default, when address config is left out or empty, is "localhost" port: 8080, ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"], // Set [] to allow all IP addresses // or add a specific IPv4 of 192.168.1.5 : // ["127.0.0.1", "::ffff:127.0.0.1", "::1", "::ffff:192.168.1.5"], // or IPv4 range of 192.168.3.0 --> 192.168.3.15 use CIDR format : // ["127.0.0.1", "::ffff:127.0.0.1", "::1", "::ffff:192.168.3.0/28"], useHttps: false, // Support HTTPS or not, default "false" will use HTTP httpsPrivateKey: "", // HTTPS private key path, only require when useHttps is true httpsCertificate: "", // HTTPS Certificate path, only require when useHttps is true language: "en", timeFormat: 24, units: "metric", // serverOnly: true/false/"local" , // local for armv6l processors, default // starts serveronly and then starts chrome browser // false, default for all NON-armv6l devices // true, force serveronly mode, because you want to.. no UI on this device modules: [ { module: "MMM-Whitehouse", position: "top_left", config: {} }, ] }; /*************** DO NOT EDIT THE LINE BELOW ***************/ if (typeof module !== "undefined") {module.exports = config;}
Error message (from dev mode):
Initializing MagicMirror. translator.js:202 Loading core translation file: translations/en.json translator.js:225 Loading core translation fallback file: translations/en.json loader.js:179 Load script: modules/MMM-Whitehouse//MMM-Whitehouse.js module.js:479 Module registered: MMM-Whitehouse loader.js:150 Bootstrapping module: MMM-Whitehouse loader.js:155 Scripts loaded for: MMM-Whitehouse loader.js:157 Styles loaded for: MMM-Whitehouse loader.js:159 Translations loaded for: MMM-Whitehouse loader.js:194 Load stylesheet: css/custom.css (index):1 Refused to apply style from 'http://localhost:8080/css/custom.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled. loader.js:203 Error on loading stylesheet: css/custom.css module.js:46 Starting module: MMM-Whitehouse main.js:474 All modules started! /home/pi/MagicMirror…rity-warnings.js:95 Electron Security Warning (Insecure Resources) This renderer process loads resources using insecure protocols.This exposes users of this app to unnecessary security risks. Consider loading the following resources over HTTPS or FTPS. - http://localhost:8080/css/main.css - http://localhost:8080/fonts/roboto.css - http://localhost:8080/socket.io/socket.io.js - http://localhost:8080/vendor/node_modules/nunjucks/browser/nunjucks.min.js - http://localhost:8080/js/defaults.js - http://localhost:8080/config/config.js - http://localhost:8080/vendor/vendor.js - http://localhost:8080/modules/default/defaultmodules.js - http://localhost:8080/js/logger.js - http://localhost:8080/translations/translations.js - http://localhost:8080/js/translator.js - http://localhost:8080/js/class.js - http://localhost:8080/js/module.js - http://localhost:8080/js/loader.js - http://localhost:8080/js/socketclient.js - http://localhost:8080/js/main.js - http://localhost:8080/fonts/node_modules/roboto-fontface/fonts/roboto-condensed/Roboto-Condensed-Regular.woff2 - http://localhost:8080/translations/en.json - http://localhost:8080/translations/en.json - http://localhost:8080/modules/MMM-Whitehouse//MMM-Whitehouse.js For more information and help, consult https://electronjs.org/docs/tutorial/security. This warning will not show up once the app is packaged. /home/pi/MagicMirror…ity-warnings.js:145 Electron Security Warning (Insecure Content-Security-Policy) This renderer process has either no Content Security Policy set or a policy with "unsafe-eval" enabled. This exposes users of this app to unnecessary security risks. For more information and help, consult https://electronjs.org/docs/tutorial/security. This warning will not show up once the app is packaged. (index):1 Refused to apply style from 'http://localhost:8080/css/custom.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.