Running python script from MagicMirror module

1.1k Views Asked by At

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.

1

There are 1 best solutions below

0
Kevko741 On

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):

var self = this;
setInterval(function() {
            self.sendSocketNotification('DO_PYTHON', <transmit data for your node_helper here>);
            self.updateDom();
        }, 5000);

Create a node_helper.js file in your module folder with the following:

var NodeHelper = require("node_helper");
const spawn = require("child_process").spawn;

module.exports = NodeHelper.create({

    init() {
    },

    start() {
    },

    stop() {      
    },

    // If notification of the main.js file is received, the node_helper will do this here:
    socketNotificationReceived(notification, payload) {
        if (notification === "DO_PYTHON") {
            // this.config = payload;
            this.yourOwnMethod();
        } else {
            // ...
        }
    },

 yourOwnMethod() {
    var self = this;
    var process = spawn("python3", ["/absolute/path/to/modules/MMM-Test/bussavganger.py"]);
    // do something else here
    this.sendSocketNotification("PYTHON_DONE", <e. g. exit state as your payload>)
 },

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.