簡體   English   中英

發送動態數據時,HTML 腳本中的數據數組未在燒瓶中更新

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

我正在使用 python Flask 來構建應用程序。 當我將靜態數據提供給數組 [] 運行時,azi 它會在 HTML 腳本控制台日志中更新,但是當我動態更新此數組時,HTML 的 Javascript 中的數組數據不會在控制台中更新。

給出靜態數據的初始情況(在這種情況下,數據在 HTML 腳本和控制台中成功更新)

 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)

支持 HTML 腳本代碼

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

代碼片段,當我嘗試動態更新數組時,它沒有在 HTML 腳本控制台中更新。 (azi、ran 和 dopp 是全局變量)我通過以太網電纜獲取數據,azi,ran 和 dopp 數組 [] 的值在 python 代碼(app.py)中得到更新,但相同的動態數組值傳遞給 HTML 時不會在腳本中更新。 這是我得到一個空白數組的谷歌瀏覽器控制台的片段,但數組在 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))
               

我是 python Flask 的新手,感謝您的幫助:)

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM