MagicMirror² v2.13.0 is available! For more information about this release, check out this topic.

Python in module



  • I am trying to run a python code from my node helper file. But, it does not seem to be working
    Core file:

    Module.register("MMM-Rediscover",{
                    default: {loc: 'modules/MMM-Rediscover/images'},
    		count : 0,
    		files:[],
    		start : function(){
    				var self = this
    				self.sendSocketNotification("GET_FILES",self.config.loc)
    				setInterval(function() {self.updateDom()}, 3000);
    		},
    		getDom : function (){
                            var wrapper = document.createElement('div')
    			wrapper.id = 'GPHOTO'
    			var img = document.createElement('div')
    			var i = this.count
                            img.style.backgroundImage = 'url(' + this.files[i] + ')'
    			img.id = 'photo'
            		var blr = document.createElement('div')
    			blr.id = 'backgrnd'
    			blr.style.backgroundImage = 'url(' + this.files[i] + ')'
    			wrapper.appendChild(blr)
    			wrapper.appendChild(img)
    			len = this.files.length
    			this.count = (this.count == len-1) ? 0 : this.count+1
                            return wrapper
                    },
    		getStyles : function (){
    			return ['MMM-Rediscover.css']
    		},
    		socketNotificationReceived : function(notification , payload){
    			if(notification == "OBTAINED"){
    				this.files = payload
    			}
    		}
    })
    
    

    My node_helper file:

    var NodeHelper = require("node_helper")
    var file = require("fs")
    const spawn = require("child_process").spawn
    module.exports = NodeHelper.create({
    		start : function(){
    			var process = spawn("python", ["modules/MMM-Rediscover/images/Photos.py"])
    			process.stdout.on("data", (data)=>{console.log(data)})
    		},
    		socketNotificationReceived: function(notification, payload){
    			if(notification == "GET_FILES"){
    				var payload = this.getFiles(payload)
    				this.sendSocketNotification("OBTAINED",payload)
    			}
    		},
    		getFiles : function(loc){
    			const contents = file.readdirSync(loc)
    			ret = []
    			for ( var i = 0; i < contents.length; i++){
    				extension = contents[i].split('.').pop().toLowerCase()
    				if (extension == 'jpg'){
    					var currentItem = loc + '/' + contents[i]
    					ret.push(currentItem)
    				}
    			}
    			return ret
    		}
    })
    
    

  • Module Developer

    hi,

    var process = spawn(“python”, [“modules/MMM-Rediscover/images/Photos.py”])

    you must give the full path to your python script

    This is an example there



  • @Bugsounet I tried giving the full path too, but it did not work
    The python script does work on its own, there are no errors in that file


Log in to reply