Search code examples
pythonflaskjinja2flask-restfulflask-socketio

Data Array in HTML Script is not updating in flask when dynamic data is sent


I am using python Flask to build an application. When i give static data to array [] ran, azi it gets updated in the HTML Script console log, but when i dynamically update this array the data of the array in Javascript of HTML is not updated in the console.

initial case when static data is given (in this case the data is updated successfully in HTML Script and console)

 azi=[0,30,45,60,90]
 ran=[200,500,800,1000,1500]
 dopp=[]    
 @app.route('/dashboard.html')
 def dashboard():  
    return render_template('dashboard.html',azi=azi,ran=ran,dopp=dopp)

Supporting HTML Script Code

<script>
 var Range = {{ran}};
 var Angle = {{azi}};
 console.log("Range Array = {{ran}}");
 console.log("Azimuth Array = {{azi}}");
</script>

Code snippet, when i try to update the array dynamically, but it doesn't get updated in HTML Script Console. (azi, ran and dopp are global variables) i am getting the data through a ethernet cable, the value of azi,ran and dopp array [] is getting updated in the python code (app.py), but the same dynamic array value when passed to HTML is not getting updated in the script. This the snip of google chrome console where i am getting a blank array, but the array is dynamically updating in python

   #azi,ran and dopp are global variables
        azi=[]
        ran=[]
        dopp=[]
                
  def ethernet():    
    print("Initialising....\n")
    time.sleep(1)
    s = socket.socket()
    shost = socket.gethostname()
    ip = socket.gethostbyname(shost)
    host = input(str("Enter server address: "))
    name = input(str("\nEnter your Command: "))
    port = 10
    print("\nTrying to connect to ", host, "(", port, ")\n")
    time.sleep(1)
    s.connect((host, port))
    print("Connected...\n")

    start_time = time.time()
    while(1):
        ran=[]
        azi=[]
        dopp=[]        
        if(time.time() - start_time >= 0.5):
            start_time = time.time()
            s.send(name.encode())
            s_name = s.recv(1024)
            s_name = s_name.decode()
            print(s_name, "Received\n")
            char = "x"
            indices = [i.start() for i in re.finditer(char, s_name)]
            a=s_name            
            NumOfCent = (int(a[0 : indices[0]]))
            MessageType = (int(a[indices[0] + 1 : indices[1]]))
            DwellNo = (int(a[indices[1] + 1 : indices[2]]))
            
            for i in range(2, len(indices) - 3, 3):
                Range = (int(a[indices[i] + 1 : indices[i + 1]]))
                Range /= 8.0
                ran.append(Range)         
                print(Range)
                print(ran)    
          
                Azimuth = (int(a[indices[i + 1] + 1 : indices[i + 2]]))
                Azimuth /= 8.0
                azi.append(Azimuth)
                print(Azimuth)

                Doppler = (int(a[indices[i + 2] + 1 : indices[i + 3]]))
                print(Doppler)
                print('')
                dopp.append(Doppler)
                x = np.multiply(Range,np.sin(Azimuth*3.14/180))
                y = np.multiply(Range,np.cos(Azimuth*3.14/180))
               

I am new to python Flask, Thanks For Your Help :)


Solution

  • def ethernet():    
        print("Initialising....\n")
        time.sleep(1)
        s = socket.socket()
        shost = socket.gethostname()
        ip = socket.gethostbyname(shost)
        host = input(str("Enter server address: "))
        name = input(str("\nEnter your Command: "))
        port = 10
        print("\nTrying to connect to ", host, "(", port, ")\n")
        time.sleep(1)
        s.connect((host, port))
        print("Connected...\n")
        ran=[]
        azi=[]
        dopp=[] 
        start_time = time.time()
        while(1):
            if(time.time() - start_time >= 0.5):
                start_time = time.time()
                s.send(name.encode())
                s_name = s.recv(1024)
                s_name = s_name.decode()
                print(s_name, "Received\n")
                char = "x"
                indices = [i.start() for i in re.finditer(char, s_name)]
                a=s_name            
                NumOfCent = (int(a[0 : indices[0]]))
                MessageType = (int(a[indices[0] + 1 : indices[1]]))
                DwellNo = (int(a[indices[1] + 1 : indices[2]]))
                
                for i in range(2, len(indices) - 3, 3):
                    Range = (int(a[indices[i] + 1 : indices[i + 1]]))
                    Range /= 8.0
                    ran.append(Range)         
                    print(Range)
                    print(ran)    
              
                    Azimuth = (int(a[indices[i + 1] + 1 : indices[i + 2]]))
                    Azimuth /= 8.0
                    azi.append(Azimuth)
                    print(Azimuth)
    
                    Doppler = (int(a[indices[i + 2] + 1 : indices[i + 3]]))
                    print(Doppler)
                    print('')
                    dopp.append(Doppler)
                    x = np.multiply(Range,np.sin(Azimuth*3.14/180))
                    y = np.multiply(Range,np.cos(Azimuth*3.14/180))
        
        return ran,azi,dopp
    
    
    @app.route('/dashboard.html')
    def dashboard():
        ran,azi,dopp = ethernet()
        return render_template('dashboard.html',azi=azi,ran=ran,dopp=dopp)