-1

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

  • 1
    Can you provide the full function that is not working? Also, what is the value of `indices`? – nonDucor Jun 29 '22 at 07:06
  • @nonDucor I am getting data from ethernet cable, i have also updated the indices part. Dynamically array value is getting updated in python code , so i dont think there is an error in the function where i am updating the data of array. The main issue is i am not able to pass dynamic value of array to HTML. Tho, the data is passed when i give static value to array in the same code. – Sameer Kumar Shrivastava Jun 29 '22 at 08:05
  • Your second code snippet is not inside any function. It should be inside the `dashboard` function. Can you provide the complete function, otherwise we do not have enough information to help you. – nonDucor Jun 29 '22 at 08:12
  • @nonDucor for your reference i have given the ethernet function, but still i want to mention that ethernet function in working perfectly fine, the global variables, azi, ran and dopp are getting dynamically updated constantly in the python file (app.py). But when i try to use these array in HTML Script (Code snippet 2), then i get an empty array in the javascript (verifed in google chrome console) – Sameer Kumar Shrivastava Jun 29 '22 at 09:36
  • In Flask, your HTML response will only be re-generated when the `dashboard` function is executed. If it is not being executed, the HTML will not be updated. – nonDucor Jun 29 '22 at 09:51
  • @nonDucor I want the array to be updated every second, so how can i do that? As far as I know dashboard function will be executed when i visit './dashboard' then how can i execute dashboard function every second if i want the data to be updated every second? – Sameer Kumar Shrivastava Jun 29 '22 at 10:00
  • Does [this](https://stackoverflow.com/questions/2787679/how-to-reload-page-every-5-seconds) answer your question? – nonDucor Jun 29 '22 at 10:43
  • @nonDucor I tried [this](https://stackoverflow.com/questions/2787679/how-to-reload-page-every-5-seconds), now the page is refreshing every 5 second but the array is still not updating. So my issue is still unsolved. – Sameer Kumar Shrivastava Jun 29 '22 at 11:13
  • Are you sure these variables are global? Nowhere in the code you provided they are declared as `global`. Particularly, they are not global in your definition of `ethernet()`. You may want to search how globals work in Python and how to declare them. – nonDucor Jun 29 '22 at 12:13

1 Answers1

0
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)
Amit punia
  • 16
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 01 '22 at 12:41