rohith santosh
rohith santosh

Reputation: 40

How do i integrate my matplotlib animation graph with flask web ui

I am plotting data by reading the cell voltages and current and some other parameters using matplotlib in python. I am using Funcanimation to animate the plot. It plots in real-time. The data is being read every second. I can do it with matplotlib now, I want to add this matplotlib to the web application using flask which should have the same functionality as before. Any suggestion on how I should approach it. Any help would be appreciated. Thanks in advance.

here is the video of how my graph plots the data just to give an idea of what I am talking about.

This is the link to the video. https://drive.google.com/file/d/1XCw__YgY0MmrsQadQ5Z0at3Uk5H5vKC-/view?usp=sharing

Upvotes: 0

Views: 1250

Answers (1)

samkb420
samkb420

Reputation: 26

try this perhaps it will solve your problem

import base64
from io import BytesIO

from flask import Flask
from matplotlib.figure import Figure

app = Flask(__name__)


@app.route("/")
def hello():
    # Generate the figure **without using pyplot**.
    fig = Figure()
    ax = fig.subplots()
    ax.plot([1, 2])
    # Save it to a temporary buffer.
    buf = BytesIO()
    fig.savefig(buf, format="png")
    # Embed the result in the html output.
    data = base64.b64encode(buf.getbuffer()).decode("ascii")
    return f"<img src='data:image/png;base64,{data}'/>"

Upvotes: 1

Related Questions