MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    Python in module

    Development
    2
    3
    135
    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
      UjwalReddy267 last edited by

      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
      • ?
        A Former User last edited by

        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 Reply Quote 0
        • U
          UjwalReddy267 @Guest last edited by

          @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
          • First post
            Last post
          Enjoying MagicMirror? Please consider a donation!
          MagicMirror created by Michael Teeuw.
          Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
          This forum is using NodeBB as its core | Contributors
          Contact | Privacy Policy