I am currently developing a custom module for my magic mirror. I want this module to execute a python script. This python script fetches data from a web server and creates a .json file in the module folder with the data. I then want the module to import this datafile inside javascript and display it on screen.
However i cant get the magic mirror module to run the python script. I have very little javascript knowledge so any help is appreciated. This is the code i have so far
defaults: {
},
start: function () {
var timer = setInterval(()=>{
const spawn = require('child_process').spawn;
const childPython = spawn('python3', ['./modules/MMM-Test/bussavganger.py']);
this.updateDom()
}, 5000)
},
getDom: function() {
var element = document.createElement("div")
element.className = "myContent"
element.innerHTML = "Hello, everybody!"
return element
}
})
Currently i am just trying to run the module to see if the .json file is created. it is not. If i run the python script separately the file is created, so i know the .py file is not the problem.
You tried calling the python script via your module's js file, but instead you should use to call the python script via the node_helper.js file.
So use the socketNotification function of the MagicMirror to call the noder_helper and in return the node_helper then calls your python script, you can do something with it and at the end send back a socketNotification to your module's js file, e. g. the result of your python program or the exit code, etc.
In your
start: function()you could call the node_helper via this command, so that your python program is being started by the module helper later directly after booting up your module (doing that from now on every interval):Create a node_helper.js file in your module folder with the following:
You can read in your json file with fs in the node_helper.js as well and parse it there and send it back via the
sendSocketNotification. Be sure to have included the two beginning lines in the node_helper.js and (!) important use always absolute paths.