Python 如何使用Flask显示动画绘图

Python 如何使用Flask显示动画绘图,python,flask,Python,Flask,我想在浏览器中显示动画绘图(热图),如您所见。 传感器发送数据,服务器(代理)接收数据以绘制热图 我在使用(返回)时遇到问题,我不知道应该返回什么。我应该创建一个新的python文件并在那里复制live.py代码吗?我是烧瓶新手,有点困惑,我会感谢你的帮助 home.html <!DOCTYPE html> <html> <head> <title>Home</title> </head><body> <

我想在浏览器中显示动画绘图(热图),如您所见。 传感器发送数据,服务器(代理)接收数据以绘制热图 我在使用(返回)时遇到问题,我不知道应该返回什么。我应该创建一个新的python文件并在那里复制live.py代码吗?我是烧瓶新手,有点困惑,我会感谢你的帮助

home.html

<!DOCTYPE html>
<html>
<head>
  <title>Home</title>
</head><body>
<div class="container">
    <div class="row">
        <div class="col-lg-8  offset-lg-2">
          <h3 class="mt-5">heatmap</h3> 
            <img src=""="{{ url_for('video_feed') }}">
        </div>
    </div>
</div>
</body>
</html>

live.py


#Import necessary libraries
from flask import Flask, render_template, Response
import cv2
#Initialize the Flask app
app = Flask(__name__, template_folder='templates')

from PIL import Image
import cv2
import numpy as np
from shutil import copyfile
import shutil
import os, glob
import matplotlib.pyplot as plt
import math
from mqttsub import mqttsub
import seaborn as sb
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import matplotlib.animation as animation # to animate the visualization



def create_color(r, g, b):
    return [r/256, g/256, b/256]

def get_custom_color_palette():
    return LinearSegmentedColormap.from_list("", [
        create_color(61, 79, 195), create_color(61, 79, 195), create_color(61, 79, 195), create_color(103, 136, 238), create_color(119, 154, 247), create_color(163, 194, 254),create_color(192, 212, 245),
        create_color(218, 220, 223),
        create_color(229, 216, 209),create_color(247, 174, 144),create_color(237, 131, 103),create_color(213, 81, 67), create_color(181, 10, 39),create_color(181, 10, 39), create_color(255, 255, 255)
    ])
low, high = -1, 2
cmap = get_custom_color_palette()
empty=[ [ 2 for x in range(8) ] for y in range(8) ]


@app.route('/')
def index():
  return render_template('home.html')


def gen():

        try:
            #subscribe and get data
            subs2 = mqttsub("/node6/grideye/matrix")
            data2=eval(subs2.get_data())
            #normalize the data
            for value in range(8):
                norm = np.linalg.norm(data2[value])
                normal_array = data2[value]/norm
                data2[value]=normal_array

            data2[7][1]=2
            data2[7][2]=2
            data2[7][3]=2
            data2[7][0]=data2[6][0]

            #plot the heat map
            plt.subplot(3,3,6)
            heat_map6 = sb.heatmap(data2, cmap=cmap,vmin=0.3,cbar=False, vmax=0.45,square=True,xticklabels=False, yticklabels=False)
            plt.xlim(8,0)
        except:
            print()
           

        plt.subplots_adjust(wspace=0, hspace=0)

    fig=plt.figure(figsize=(9,9))

    ani = animation.FuncAnimation(fig, animate, interval=25)
    plt.show()


@app.route('/video_feed')
def video_feed():
    return Response(gen())

if __name__ == '__main__':
    app.run(debug=True)

#Import necessary libraries
from flask import Flask, render_template, Response
import cv2
#Initialize the Flask app
app = Flask(__name__, template_folder='templates')

from PIL import Image
import cv2
import numpy as np
from shutil import copyfile
import shutil
import os, glob
import matplotlib.pyplot as plt
import math
from mqttsub import mqttsub
import seaborn as sb
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import matplotlib.animation as animation # to animate the visualization



def create_color(r, g, b):
    return [r/256, g/256, b/256]

def get_custom_color_palette():
    return LinearSegmentedColormap.from_list("", [
        create_color(61, 79, 195), create_color(61, 79, 195), create_color(61, 79, 195), create_color(103, 136, 238), create_color(119, 154, 247), create_color(163, 194, 254),create_color(192, 212, 245),
        create_color(218, 220, 223),
        create_color(229, 216, 209),create_color(247, 174, 144),create_color(237, 131, 103),create_color(213, 81, 67), create_color(181, 10, 39),create_color(181, 10, 39), create_color(255, 255, 255)
    ])
low, high = -1, 2
cmap = get_custom_color_palette()
empty=[ [ 2 for x in range(8) ] for y in range(8) ]


@app.route('/')
def index():
  return render_template('home.html')


def gen():

        try:
            #subscribe and get data
            subs2 = mqttsub("/node6/grideye/matrix")
            data2=eval(subs2.get_data())
            #normalize the data
            for value in range(8):
                norm = np.linalg.norm(data2[value])
                normal_array = data2[value]/norm
                data2[value]=normal_array

            data2[7][1]=2
            data2[7][2]=2
            data2[7][3]=2
            data2[7][0]=data2[6][0]

            #plot the heat map
            plt.subplot(3,3,6)
            heat_map6 = sb.heatmap(data2, cmap=cmap,vmin=0.3,cbar=False, vmax=0.45,square=True,xticklabels=False, yticklabels=False)
            plt.xlim(8,0)
        except:
            print()
           

        plt.subplots_adjust(wspace=0, hspace=0)

    fig=plt.figure(figsize=(9,9))

    ani = animation.FuncAnimation(fig, animate, interval=25)
    plt.show()


@app.route('/video_feed')
def video_feed():
    return Response(gen())

if __name__ == '__main__':
    app.run(debug=True)