# Python. node red

**URL:** https://discourse.nodered.org/t/python-node-red/32772
**Category:** General
**Created:** [12 September 2020 04:38 UTC](https://discourse.nodered.org/t/python-node-red/32772 "2020-09-12T04:38:46Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![HenryG](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@HenryG](https://discourse.nodered.org/u/HenryG)
#### Post date: [12 September 2020 04:38 UTC](https://discourse.nodered.org/t/python-node-red/32772/1 "2020-09-12T04:38:46Z")

</div>

Hey all,  
I'm struggling to pass an output of either "Rain" or "No Rain" from my python script. It predicts either those outputs by input the temp, humidity and wind speed. Currently I'm using python to communicate to node-red using MQTT and i think it works fine. I tried using the exec node but it keeps showing up an error where my py.file does not exist or it can't find the file even though i used a full path.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80382e5adbb430592d4aed0ea061560c70b07024.png)

Can someone help as i'm in a rush to try and finish my project. Please comment if i've missed any important details or a snip of my code.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [12 September 2020 05:23 UTC](https://discourse.nodered.org/t/python-node-red/32772/2 "2020-09-12T05:23:54Z")

</div>

If you are on windows i think you should use double backslashes. Can your python script not be done in node-red itself ? (ie not using python).

---

<div class="post-metadata">

### Author: ![HenryG](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@HenryG](https://discourse.nodered.org/u/HenryG)
#### Post date: [12 September 2020 05:38 UTC](https://discourse.nodered.org/t/python-node-red/32772/3 "2020-09-12T05:38:25Z")

</div>

I did many revisions. The last time I did by writing the whole python code inside, I think it had issues because if tensorflow (I was using the python3 function node at the time)

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [12 September 2020 06:03 UTC](https://discourse.nodered.org/t/python-node-red/32772/4 "2020-09-12T06:03:58Z")

</div>

I mean if your python code can be replaced by a node-red flow instead.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 September 2020 06:22 UTC](https://discourse.nodered.org/t/python-node-red/32772/5 "2020-09-12T06:22:03Z")

</div>

> [@HenryG](#):
>
> Currently I'm using python to communicate to node-red using MQTT and i think it works fine

In that case why use an exec node?

I can't look at your flow at the moment but you can always use forward slashes in file names, so don't use back slashes at all, it isn't necessary and adds confusion.

---

<div class="post-metadata">

### Author: ![HenryG](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@HenryG](https://discourse.nodered.org/u/HenryG)
#### Post date: [12 September 2020 07:03 UTC](https://discourse.nodered.org/t/python-node-red/32772/6 "2020-09-12T07:03:28Z")

</div>

How do I do that? I'm not very familiar with node red

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [12 September 2020 07:16 UTC](https://discourse.nodered.org/t/python-node-red/32772/7 "2020-09-12T07:16:11Z")

</div>

What does the python script look like ?

---

<div class="post-metadata">

### Author: ![HenryG](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@HenryG](https://discourse.nodered.org/u/HenryG)
#### Post date: [12 September 2020 07:23 UTC](https://discourse.nodered.org/t/python-node-red/32772/8 "2020-09-12T07:23:06Z")

</div>

from pandas import read\_csv  
from sklearn.preprocessing import OneHotEncoder  
from tensorflow.keras.models import load\_model  
import numpy as np  
import pickle  
import tkinter as tk  
from tkinter import messagebox

class predict\_gui():  
def predict\_all(self, inputVecs):  
testresult =   
for vec in inputVecs:  
pred = self.predict(vec)  
testresult.append(pred)  
return testresult

```
def predict(self, inputVec):
    inputVec = self.StdScales.transform([inputVec])
    pred = self.best_model.predict_classes(inputVec)
    pred = self.ohe.inverse_transform(pred)
    pred = list(np.reshape(pred, (1,)))
    return pred

def __init__ (self):
    with open('StdScales', 'rb') as f:
        self.StdScales = pickle.load(f)
    self.best_model = load_model('bestmodel.h5')

    self.ohe = OneHotEncoder(drop='first')
    self.ohe.fit([["No Rain"], ["Rain"]])
    self.MainMenu = tk.Tk()
    self.MainMenu.title("Main Menu") 
    self.MainMenu.protocol("WM_DELETE_WINDOW",self.quit_window)
    tk.Label(self.MainMenu, text="Temperature").grid(
        row=0,column=0,padx = 20 ,pady=20, sticky='W')
    tk.Label(self.MainMenu, text="Humidity").grid(
        row=1,column=0,padx = 20 ,pady=20, sticky='W')
    tk.Label(self.MainMenu, text="Wind Speed").grid(
        row=2,column=0,padx = 20 ,pady=20, sticky='W')
    self.TempEntry = tk.Entry(self.MainMenu, font = "Times 14", width = 12)
    self.TempEntry.grid(row=0,column=1,padx = 20 ,pady=20, sticky='W')
    self.HumidEntry = tk.Entry(self.MainMenu, font = "Times 14", width = 12)
    self.HumidEntry.grid(row=1,column=1,padx = 20 ,pady=20, sticky='W')
    self.WindSpdEntry = tk.Entry(self.MainMenu, font = "Times 14", width = 12)
    self.WindSpdEntry.grid(row=2,column=1,padx = 20 ,pady=20, sticky='W')
    tk.Label(self.MainMenu, text="Predict :", font = "Times 18 bold").grid(
        row=3,column=0,padx = 20 ,pady=20, sticky='W')
    self.PredictEntry = tk.Entry(self.MainMenu, font = "Times 18 bold", width = 8, justify='center')
    self.PredictEntry.grid(row=3,column=1,padx = 20 ,pady=20)
    self.PredictEntry.configure(state='disabled')
    tk.Button(self.MainMenu, text='Send', font = "Times 14", command=self.send_to_node_red, width = 10, height = 3).grid(
        row=3,column=2,padx=20, pady=20, sticky='W')
    
    self.MainMenu.mainloop()

def quit_window(self): # Exit confirmation window
    if messagebox.askokcancel("Quit", "Do you want to quit?"):
        self.MainMenu.destroy()
        self.ExitStatus = True

def send_to_node_red(self):
    Datasample = [self.TempEntry.get(), self.HumidEntry.get(), self.WindSpdEntry.get()]
    print(Datasample)
    result = self.predict(Datasample)
    print(result[0])
    self.PredictEntry.configure(state="normal")
    if self.PredictEntry.get() != "":
        self.PredictEntry.delete(0,"end")
    self.PredictEntry.insert(0, result[0])
    self.PredictEntry.configure(state="readonly")

    # Send result[0] to your node red

```

if **name** == " **main**":

```
GUI = predict_gui()
```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [11 November 2020 07:23 UTC](https://discourse.nodered.org/t/python-node-red/32772/9 "2020-11-11T07:23:07Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
