Python. node red

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.

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.

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

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)

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

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.

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

What does the python script look like ?

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()

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