Reading HTML in Custom node

Greetings,

I have been creating a custom node that reads input and runs them in a Python script, I have a problem In capturing the HTML by JavaScript from my custom node, all I can get is the msg Object.

Regards,

Hi @abouslima, welcome to the forum.

What HTML are you trying to capture? Is that what is being returned by the python script?

How are you running the python script?

Can you share any more detail of what you are trying to do and what you have tried so far?

Hello @knolleary,

Thanks for your kind reply, I'm new into Node-Red, so it might seem simple but I#m having a hard time figuring it out.

So I have created a custom node, and I have these input, which I might increase later.

<script type="text/html" data-template-name="secondNode">
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    
    
	  <div class="form-row">
	      <label for="node-input-command"><i class="icon-tag"></i> Website</label>
	      <input type="text" id="node-input-command" placeholder="Website">
	  </div>
</script>

In my JavaScript file I can read the msg.payload but I don#t know how to read the HTML from my custom node that I will input, I don't mind adding them to the msg object, or creating a new object with them, just to read them.

I'm using a Spawn process, similar to this from the exec node, in the following JavaScript file it sends a random array from the function node.

module.exports = function(RED) {
    const child_process = require('child_process');
    function secondNode(config) {
     
        RED.nodes.createNode(this,config);
        // node-specific code goes here
        var node = this;
        node.file = __dirname + '/proj1.py'

        this.on('input', function(msg) {
         
            const child = child_process.spawn("python",[node.file, msg.payload]);
            
            child.stdout.on('data', (data) => {
                msg.payload = data.toString()
                this.send(msg)
            });

        });
    }
    RED.nodes.registerType("secondNode",secondNode);
};

my target is to send a CSV file through the msg.payload using the CSV node tp convert it to object and several other arguments that as well will be sent to Python but that should be input by the custom node I create.

the Python script is below:

import sys
import numpy as np
import re

num_list = re.findall(r'[0-9]+', sys.argv[1])
num_list = [int(n) for n in num_list]
mean = np.mean(num_list)
print(mean)

Thanks in advance for any input you can give me.

In your node's HTML file, you will have defined a set or properties the node can set in its defaults object - https://nodered.org/docs/creating-nodes/properties

So from the HTML you've shared, I would expect your defaults object to include entries for name and command.

At that point, when the node is deployed, the config object passed to your node's constructor function (which you have called secondNode) will contain those as properties:

function secondNode(config) {
   RED.nodes.createNode(this, config);
   var node = this;
   // Get the value of the 'command' property for this node
   var command = config.command;
   ...
1 Like

Thank you so much, I have applied it and it works.

Not wanting to discourage you from having a go at writing a custom node. However, are you aware that you could almost certainly do everything you want with existing nodes?

@TotallyInformation, Yes, but I just want to have a node customized with specific choices, as I want to add several multiple choice boxes for the user.

As I say, I didn't want to discourage you :smile:

Its just that we quite often get people reinventing the wheel because they didn't check whether something was already available. It can then be confusing for people since, if the node then gets published, the library gets cluttered with duplicates and nobody is quite sure which to use.

So if this is for personal/local use it is certainly not an issue of course. Have fun and welcome to the community. I warn you, it is addictive!

2 Likes

@TotallyInformation, Yes I totally understand, and I don't plan to publish the node, it's a learning project that I'm using locally and to get more experience in coding as I'm still new the field.

I'm already loving the community, I spent almost a week looking for an answer and I got it just few minutes after I posted, I really hope one day I can help people back.

Thanks again.

2 Likes