• 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.

I don't know how to use node_helper.js.

Scheduled Pinned Locked Moved Solved Troubleshooting
6 Posts 2 Posters 2.3k 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.
  • E Offline
    emrhssla
    last edited by Apr 11, 2019, 12:11 PM

    I tried to use python-shell. So I successfully finished the npm install python-shell.
    But I saw it said that to use python-shell, you must use node_helper.js.
    So I tried to execute a simple Python code. The notification I_DID in node_helper.js was well communicated and executed. However, the console in node_helper.js is not output, nor is python-shell working.
    What is the problem?

    var Testpythons;
    Module.register("MMM-Testpython", {
    
        defaults: {},
        start: function (){
            Testpythons = this;
        },
      
      getDom: function() {
        var element = document.createElement("div")
        element.className = "myContent"
        element.id="divid"
        element.innerHTML = "Hello, World!!! " + this.config.foo
        var subElement = document.createElement("p")
        subElement.innerHTML = "Click" 
        subElement.id = "clickid"
        element.appendChild(subElement)
        return element
      },
      
      notificationReceived: function(notification, payload, sender) {
        switch(notification) {
          case "DOM_OBJECTS_CREATED":
          var elem = document.getElementById("clickid")
          elem.addEventListener("click", () => {
            Testpythons.sendSocketNotification("TESTPYTHON")
            console.log("hello~hello~hello~hello~hello~hello~hello~hello~hello~hello~")
            elem.innerHTML = "click success"
            
          }) 
            break
        }
      },
      socketNotificationReceived: function(notification, payload) {
        switch(notification) {
          case "I_DID":
            console.log("success")
            var elemk = document.getElementById("divid")
            elemk.innerHTML = "I_DID"
            break
        }
      },
    })
    

    node_helper.js

    var NodeHelper = require("node_helper");
    var PythonShell = require('python-shell');
    
    module.exports = NodeHelper.create({
      start: function() {
        console.log("Starting module: " + this.name);
      },
      
      socketNotificationReceived: function(notification, payload) {
        switch(notification) {
          case "TESTPYTHON":
            console.log("notification : " + notification)
            this.sendSocketNotification("I_DID")
            PythonShell.runString('print("Hello Python")', null, function (err, result) {
                if (err) throw err;
                console.log(result);  
                });
            break
        }
      },
    })  
    
    1 Reply Last reply Reply Quote 0
    • S Offline
      sdetweil
      last edited by sdetweil Apr 11, 2019, 12:28 PM Apr 11, 2019, 12:24 PM

      see https://ourcodeworld.com/articles/read/286/how-to-execute-a-python-script-and-retrieve-output-data-and-errors-in-node-js

      but your should have been ok… do you see the message
      console.log("Starting module: " + this.name);

      in the terminal window?

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      E 1 Reply Last reply Apr 12, 2019, 7:31 AM Reply Quote 0
      • E Offline
        emrhssla @sdetweil
        last edited by Apr 12, 2019, 7:31 AM

        This post is deleted!
        E 1 Reply Last reply Apr 12, 2019, 7:39 AM Reply Quote 0
        • E Offline
          emrhssla @emrhssla
          last edited by Apr 12, 2019, 7:39 AM

          @sdetweil

          var NodeHelper = require("node_helper");
          var {PythonShell} = require('python-shell');
          
          module.exports = NodeHelper.create({
            start: function() {
              console.log("Starting module: " + this.name);
            },
            
            socketNotificationReceived: function(notification, payload) {
              console.log("Starting module: " + this.name);
              switch(notification) {
                case "TESTPYTHON":
                  console.log("notification : " + notification)
                  this.sendSocketNotification("I_DID")
                  PythonShell.runString('print("Hello Python")', null, function (err, result) {
                      if (err) throw err;
                      console.log(result);  
                      });
                  break
              }
            },
          })  
          

          0_1555054288241_a49a1a4e-d8c3-4e5c-8463-81b9df38737c-image.png

          console doesnt show Starting module…

          S 1 Reply Last reply Apr 12, 2019, 12:05 PM Reply Quote 0
          • S Offline
            sdetweil @emrhssla
            last edited by Apr 12, 2019, 12:05 PM

            @emrhssla ok, there are two different output areas…

            the developers window, which u posted is from the module.js side…
            the node helper content comes out in the terminal window.

            start you mirror app,
            then start it with

            npm start &
            

            (note the & at the end)
            now you should be able to see any messages in the terminal window from the node_helper

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            E 1 Reply Last reply Apr 12, 2019, 1:29 PM Reply Quote 1
            • E Offline
              emrhssla @sdetweil
              last edited by Apr 12, 2019, 1:29 PM

              @sdetweil oh!!! 0_1555075761914_acdbfe01-9c20-45b9-a177-87d554b225e0-image.png
              thank you very very very much :)

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              1 / 1
              • First post
                1/6
                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