• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Python in module

Scheduled Pinned Locked Moved Development
3 Posts 2 Posters 405 Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    UjwalReddy267
    last edited by Jul 6, 2020, 6:20 PM

    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
    		}
    })
    
    
    1 Reply Last reply Reply Quote 0
    • ? Offline
      A Former User
      last edited by Jul 6, 2020, 6:31 PM

      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

      U 1 Reply Last reply Jul 7, 2020, 5:55 AM Reply Quote 0
      • U Offline
        UjwalReddy267 @Guest
        last edited by Jul 7, 2020, 5:55 AM

        @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

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        1 / 1
        • First post
          1/3
          Last post
        Enjoying MagicMirror? Please consider a donation!
        MagicMirror created by Michael Teeuw.
        Forum managed by Sam, technical setup by Karsten.
        This forum is using NodeBB as its core | Contributors
        Contact | Privacy Policy