|
| 1 | +<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.3/ace.js"></script> |
| 2 | +<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.3/ext-language_tools.js"></script> |
| 3 | +<script type="text/javascript"> |
| 4 | + RED.nodes.registerType('python-function',{ |
| 5 | + category: 'function', |
| 6 | + color: '#fdd0a2', |
| 7 | + defaults: { |
| 8 | + name: {value: ""}, |
| 9 | + func: {value: "\nreturn msg"}, |
| 10 | + outputs: {value: 1} // Ofuscated way to persist the number of outputs of the node |
| 11 | + }, |
| 12 | + inputs: 1, |
| 13 | + outputs: 1, |
| 14 | + icon: "function.png", |
| 15 | + label: function() { |
| 16 | + return this.name; |
| 17 | + }, |
| 18 | + oneditprepare: function() { |
| 19 | + $( "#node-input-outputs" ).spinner({ |
| 20 | + min:1 |
| 21 | + }); |
| 22 | + var langTools = ace.require('ace/ext/language_tools'); |
| 23 | + this.editor = ace.edit('node-input-func-editor'); |
| 24 | + this.editor.setTheme('ace/theme/tomorrow'); |
| 25 | + this.editor.getSession().setMode('ace/mode/python'); |
| 26 | + this.editor.setValue($("#node-input-func").val(), -1); |
| 27 | + this.editor.setOptions({ |
| 28 | + enableBasicAutocompletion: true, |
| 29 | + enableLiveAutocompletion: true, |
| 30 | + highlightSelectedWord: true, |
| 31 | + useSoftTabs: true, |
| 32 | + tabSize: 4, |
| 33 | + }); |
| 34 | + var noderedKeywords = [ |
| 35 | + 'msg', 'msg.payload', 'node', 'node.send', |
| 36 | + 'node.log', 'node.warn', 'node.error', 'node.status' |
| 37 | + ]; |
| 38 | + this.editor.completers.push({ |
| 39 | + getCompletions: function (state, session, pos, prefix, callback) { |
| 40 | + callback(null, noderedKeywords.map(function (word) { |
| 41 | + return { |
| 42 | + name: word, |
| 43 | + value: word, |
| 44 | + score: 0, |
| 45 | + meta: 'Node-RED' |
| 46 | + }; |
| 47 | + })); |
| 48 | + } |
| 49 | + }); |
| 50 | + this.editor.focus(); |
| 51 | + }, |
| 52 | + oneditsave: function() { |
| 53 | + var annot = this.editor.getSession().getAnnotations(); |
| 54 | + this.noerr = 0; |
| 55 | + $("#node-input-noerr").val(0); |
| 56 | + for (var k=0; k < annot.length; k++) { |
| 57 | + //console.log(annot[k].type,":",annot[k].text, "on line", annot[k].row); |
| 58 | + if (annot[k].type === "error") { |
| 59 | + $("#node-input-noerr").val(annot.length); |
| 60 | + this.noerr = annot.length; |
| 61 | + } |
| 62 | + } |
| 63 | + $("#node-input-func").val(this.editor.getValue()); |
| 64 | + delete this.editor; |
| 65 | + }, |
| 66 | + oneditresize: function(size) { |
| 67 | + var rows = $("#dialog-form>div:not(.node-text-editor-row)"); |
| 68 | + var height = $("#dialog-form").height(); |
| 69 | + for (var i=0;i<rows.size();i++) { |
| 70 | + height -= $(rows[i]).outerHeight(true); |
| 71 | + } |
| 72 | + var editorRow = $("#dialog-form>div.node-text-editor-row"); |
| 73 | + height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom"))); |
| 74 | + $(".node-text-editor").css("height",height+"px"); |
| 75 | + this.editor.resize(); |
| 76 | + } |
| 77 | + }); |
| 78 | +</script> |
| 79 | + |
| 80 | +<script type="text/x-red" data-template-name="python-function"> |
| 81 | + <div class="form-row"> |
| 82 | + <label for="node-input-name"><i class="fa fa-tag"></i> <span>Name</span></label> |
| 83 | + <input type="text" id="node-input-name" placeholder="Name"> |
| 84 | + </div> |
| 85 | + <div class="form-row" style="margin-bottom: 0px;"> |
| 86 | + <label for="node-input-func"><i class="fa fa-wrench"></i> <span>Function</span></label> |
| 87 | + <input type="hidden" id="node-input-func" autofocus="autofocus"> |
| 88 | + <input type="hidden" id="node-input-noerr"> |
| 89 | + </div> |
| 90 | + <div class="form-row node-text-editor-row"> |
| 91 | + <div style="height: 250px; min-height:150px;" class="node-text-editor" id="node-input-func-editor" ></div> |
| 92 | + </div> |
| 93 | + <div class="form-row"> |
| 94 | + <label for="node-input-outputs"><i class="fa fa-random"></i> <span>Outputs</span></label> |
| 95 | + <input id="node-input-outputs" style="width: 60px;" value="1"> |
| 96 | + </div> |
| 97 | + <div class="form-tips"><span>See the Info tab for help writing Python functions</span></div> |
| 98 | +</script> |
| 99 | + |
| 100 | +<script type="text/x-red" data-help-name="python-function"> |
| 101 | + <p>Just like the plain old Javascript function node, but writting Python!</p> |
| 102 | + <p>All functionality of the javascript function node is to be expected (now or in the near future, or in a parallel universe)</p> |
| 103 | + <p>Virtually all posible incoming messages are supported, available as a Python dictionary. However bear in mind that all data inside msg that is not a valid JSON data type will be dropped from it permanently</p> |
| 104 | + <p><pre>THIS IS NOT READY FOR PRODUCTION YET!</pre></p> |
| 105 | + |
| 106 | + <h2>Usage</h2> |
| 107 | + <p>A function block where you can write code to do more interesting things.</p> |
| 108 | + <p>The message is passed in as a Python dictionary called <code>msg</code>.</p> |
| 109 | + <p>By convention it will have a <code>msg['payload']</code> key containing |
| 110 | + the body of the message.</p> |
| 111 | + <h4>Logging and Error Handling</h4> |
| 112 | + <p>To log any information, or report an error, the following functions are available:</p> |
| 113 | + <ul> |
| 114 | + <li><code>node.log("Log")</code></li> |
| 115 | + <li><code>node.warn("Warning")</code></li> |
| 116 | + <li><code>node.error("Error")</code></li> |
| 117 | + </ul> |
| 118 | + </p> |
| 119 | + <p>The Catch node can also be used to handle errors. To invoke a Catch node, |
| 120 | + pass <code>msg</code> as a second argument to <code>node.error</code>:</p> |
| 121 | + <pre>node.error("Error msg", msg)</pre> |
| 122 | + <h4>Sending messages</h4> |
| 123 | + <p>The function can either return the messages it wants to pass on to the next nodes |
| 124 | + in the flow, or can call <code>node.send(messages)</code>.</p> |
| 125 | + <p>It can return/send:</p> |
| 126 | + <ul> |
| 127 | + <li>a single message dictionary - passed to nodes connected to the first output</li> |
| 128 | + <li>a list of message dictionaries - passed to nodes connected to the corresponding outputs</li> |
| 129 | + </ul> |
| 130 | + <p>If any element of the list is itself a list of messages, multiple |
| 131 | + messages are sent to the corresponding output.</p> |
| 132 | + <p>If null is returned, either by itself or as an element of the list, no |
| 133 | + message is passed on.</p> |
| 134 | + <p>See the <a target="_new" href="http://nodered.org/docs/writing-functions.html">online documentation</a> for more help.</p> |
| 135 | + |
| 136 | +</script> |
0 commit comments