Plotting real time monitoring with Flask and Vuejs.

Data and it’s visualization, it’s some of my favorite things to work on. In this tutorial I will talk about how to plot in real time data from the server side, this data could easily come from any database engine, but for the for the sake of simplicity it will come from a CSV.

I will be using Flask with Pandas on the server side, and Vuejs with charjs on client side.

So let’s start by generating our dummy data with Python with the help of Pandas library:

This dataframe will have 10000 rows and looks something like this:

Now we are all setted up and we can simply start coding, I will not be using vue cli, but I can later add the link to Github repository with it.

Client side implementation.

Let’s set up the HTML, let’s call this file client.html:
Note: I will not declare a Doctype or header for the sake of this tutorial, just exactly the things we need.

Our container with acceptable dimensions, and adding the line-chart component prepared to receive our dynamic property chartData:

Let’s import the libraries:

  1. Vuejs
  2. Charjs Library
  3. Our Chartjs wrapper for Vuejs
  4. Vue Resource

Now let’s start by import our line-chart component and it’s options:

Now it’s time for our main Vue instance:

Complete file here

PS: This could be done with web sockets as well, instead of AJAX.

For the first request the response will have 20 values for each dataset (data) and 20 labels (X axis).

It will look something like:

But for the next n requests the response will have just 1 value for each dataset and one label:

I did this way because we want to avoid send the same data we already have plotted, we want to get just one new row of data each second after the first one.

This logic it’s implemented directly in read_csv function below, I will explain it on the comments.

Server side implementation.

Since most of the work’s logic is being executed on the client side, the server side will be less confusing and more straightforward.

Let’s call this file and start importing the libraries we need:

  1. Flask
  2. Cors, in this case because our domains are diferrent we need to allow CORS
  3. Pandas

Let’s start by setting up our app:

Now our route and logic:

Complete file here

And we are all set:

Final result

Just run:

By default it should start the server on, if not just adjust the endpoint on the Ajax request in the client side.

Then double click on the client.html file, it should open a web browser.

Anyway, I know the code here can be a little confusing, so here it is the Github project.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Miguel Frazão

Full Stack Developer/Architect since 2013, working mostly on the Back end part of the Software recently.