Brain Aboze Technology Business Analyst | Data Scientist | Technical Writer

Build interactive charts with Flask and D3.js

10 min read 2843

The influence of data analysis on various sectors has grown over the years, as it has become critical to decision-making for companies. Data analysis techniques can reveal trends, patterns, and metrics to proffer insights and optimization. This is why it is important for developers to understand how to build programs that allow for easy visualization of data.

In this article, we will use Flask and D3.js to create a simple, interactive data dashboard in order to understand some factors related to hypothetical customer churn rate.

Flask is a Python web framework that provides tools, libraries, and technologies to build web applications. D3.js is a JavaScript library that manipulates DOM elements using data to render visual components.

Project setup and environment

It is essential to create an isolated local environment to specify the various packages and versions installed that only apply to this project in order to prevent global installations and package collision.

We will begin by creating a virtual Python environment. Install the virtualenv package using pip like so:

pip install virtualenv

Navigate to the project root folder and create the virtual environment:

virtualenv flask

The virtual environment must be activated before we can install packages. In the project root folder, execute:

source flask/bin/activate

Next, we install the packages required for this project. This can be done via pip installing all the packages, or via the requirements.txt file found on the project’s GitHub repository:

We made a custom demo for .
No really. Click here to check it out.

pip install -r requirements.txt

Upon successfully installing the required Python packages, we move to set the project file structure and required files:

├── flask
├── static
│   ├── Review.gif
│   ├── css
│   ├── data
│   ├── js
│   └── logo.jpeg
└── templates
    └── index.html

Overview of project workflow

diagram of project workflow

The customer churn data will be served to the Flask app, where a data wrangling operation will be carried out with Python. The Flask app will serve the data dashboard, and D3.js will render the respective charts with JavaScript.

Anatomy of the Flask web app

The Python script is a Flask instance that contains the entry point, routing, and endpoint. Python’s Pandas and NumPy libraries are used for data wrangling operations. The preprocessed data is serialized to JSON format to be served to the index.html, and the analysis covers contract and tenure features.

The contract feature describes customers’ contract terms with the example company and has three classes: month-to-month, one year, and two years. Tenure is a continuous feature that describes the number of months the customer has stayed with the company:

from flask import Flask, jsonify, render_template
import pandas as pd
import numpy as np

app = Flask(__name__)

#Reading data
data_df = pd.read_csv("static/data/Churn_data.csv")
churn_df = data_df[(data_df['Churn']=="Yes").notnull()]

def index():
   return render_template('index.html')

def calculate_percentage(val, total):
   """Calculates the percentage of a value over a total"""
   percent = np.round((np.divide(val, total) * 100), 2)
   return percent

def data_creation(data, percent, class_labels, group=None):
   for index, item in enumerate(percent):
       data_instance = {}
       data_instance['category'] = class_labels[index]
       data_instance['value'] = item
       data_instance['group'] = group

def get_piechart_data():
   contract_labels = ['Month-to-month', 'One year', 'Two year']
   _ = churn_df.groupby('Contract').size().values
   class_percent = calculate_percentage(_, np.sum(_)) #Getting the value counts and total

   piechart_data= []
   data_creation(piechart_data, class_percent, contract_labels)
   return jsonify(piechart_data)

def get_barchart_data():
   tenure_labels = ['0-9', '10-19', '20-29', '30-39', '40-49', '50-59', '60-69', '70-79']
   churn_df['tenure_group'] = pd.cut(churn_df.tenure, range(0, 81, 10), labels=tenure_labels)
   select_df = churn_df[['tenure_group','Contract']]
   contract_month = select_df[select_df['Contract']=='Month-to-month']
   contract_one = select_df[select_df['Contract']=='One year']
   contract_two =  select_df[select_df['Contract']=='Two year']
   _ = contract_month.groupby('tenure_group').size().values
   mon_percent = calculate_percentage(_, np.sum(_))
   _ = contract_one.groupby('tenure_group').size().values
   one_percent = calculate_percentage(_, np.sum(_))
   _ = contract_two.groupby('tenure_group').size().values
   two_percent = calculate_percentage(_, np.sum(_))
   _ = select_df.groupby('tenure_group').size().values
   all_percent = calculate_percentage(_, np.sum(_))

   barchart_data = []
   data_creation(barchart_data,all_percent, tenure_labels, "All")
   data_creation(barchart_data,mon_percent, tenure_labels, "Month-to-month")
   data_creation(barchart_data,one_percent, tenure_labels, "One year")
   data_creation(barchart_data,two_percent, tenure_labels, "Two year")
   return jsonify(barchart_data)

if __name__ == '__main__':

The entry point has an index.html template file that consists of the data dashboard layout. The index.html template consists of two containers: the writeup section and the visualization section.

The template files contain access points to the script files and a CDN to link D3.js to the project, along with the CSS stylesheet styles.css. The scripts include pieChart.js, barChart.js, updateBarChart.js, and index.js, which do the following:

  • Render the pie chart and the default bar chart
  • Update the bar chart based on a selection of the pie chart
  • Include the main script that runs the chart’s functions to render on the dashboard

The index.html template also fetches the JSON response data via the routing URLs with two variables: pieChartDataUrl and barChartDataUrl.

><!DOCTYPE html>
<html lang="en">

   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   <meta name="description" content="Data Dashboard">
   <meta name="author" content="Aboze Brain">
   <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
   <title>Data Dashboard</title>
   <link href="{{ url_for('static', filename='css/styles.css') }}" rel="stylesheet">
       <div class="about">
         <h1>Data Dashboard</h1>
         <h2>Project: Interactive charts for frontend data visualization using flask and D3js</h2>
         <h2>Author: Aboze Brain John</h2>
         <p>Bio: Aboze Brain John is a Technology Business Analyst.
            He has experience in Data Science and Analytics, Software Engineering, Product Research, and Technical Writing.</p>
         <p>Project Overview: The project is focused on the analysis of churned customers.
           This analysis is achieved using Python's Flask library to serve the data and Javascript D3.js library to visualize the analysis.
           The use case is the Telco Customer Churn found on Kaggle <a href="">here</a>
         <h4>The code can be found on Github <a href="">here</a></h4>
         <h4>The article can be found on Logrocket blog <a href="#">here</a></h4>
       <img src="{{ url_for('static', filename='logo.jpeg')}}" alt="Logrocket logo">

       <div class="visualization">
         <div id="pieChart"></div>
         <div id="barChart"></div>

   <script src=""></script>
       const pieChartDataUrl = "{{ url_for('get_piechart_data') }}";
       const barChartDataUrl = "{{ url_for('get_barchart_data') }}";
   <script src="{{ url_for('static', filename='js/pieChart.js') }}"></script>
   <script src="{{ url_for('static', filename='js/barChart.js') }}"></script>
   <script src="{{ url_for('static', filename='js/updateBarChart.js') }}"></script>
   <script src="{{ url_for('static', filename='js/index.js') }}"></script>

The JavaScript scripts utilize a functional programming paradigm with various functions to create components executed in index.js. The index.js file uses promises to handle asynchronous operations and represents the eventual completion (or failure) and resulting values.

const urls = [pieChartDataUrl, barChartDataUrl];

Promise.all( => d3.json(url))).then(run);

function run(dataset) {
   d3PieChart(dataset[0], dataset[1]);

Creating pie chart and bar chart functions

Next, we have two functions to create d3PieChart and d3BarChart in the pieChart.js and barChart.js static files, respectively. We will be utilizing SVG elements, because they provide different shapes and offer more flexibility and power.

The d3PieChart function accepts two arguments: the pie chart data, and the dataset to update the bar chart upon selection of a slice of the pie chart. The pieChart.js file contains the following:

function d3PieChart(dataset, datasetBarChart){
   // Set up SVG dimensions and properties
   const margin = {top:20, right:20, bottom:20, left:20};
   const width = 350 - margin.left - margin.right,
   height = 350 - - margin.bottom,
   outerRadius = Math.min(width, height) / 2,
   innerRadius = outerRadius * .5,
   color = d3.scaleOrdinal(d3.schemeAccent); //color scheme

   // Selecting the div with id pieChart on the index.html template file
   const visualization ='#pieChart')
       .append("svg")      //Injecting an SVG element
       .data([dataset])    //Binding the pie chart data
       .attr("width", width)
       .attr("height", height)
       .append("g")        //Grouping the various SVG components  
       .attr("transform", "translate(" + outerRadius + "," + outerRadius + ")"); //Piechart tranformation and transition upon page loading

   const data = d3.pie()   //Creating the data object that will develop the various segment of the pie chart.
       .value(function(d){return d.value;})(dataset);    // Retrieve the pie chart data values from our Flask app, the pie chart where tied to a 'value' key of a JSON object.

   // Generate an arc generator that produces the circular chart (outer circle)
   const arc = d3.arc()   

    // Generate an arc generator that produces the circular chart (inner circle)
   const innerArc = d3.arc()

   // Create pie chart slices based on the data object created
   const arcs = visualization.selectAll("g.slice")
       .enter()    // creates the initial join of data to elements                      
       .attr("class", "slice")
       .on("click", click);

   arcs.append("svg:path")     // create path element
       .attr("fill", function(d, i) { return color(i); } )     //Add color to slice
       .attr("d", arc)     // creates actual SVG path with associated data and the arc drawing function
       .append("svg:title")        // Add title to each piechart slice
       .text(function(d) { return + ": " +"%"; });          

   d3.selectAll("g.slice")     // select slices in the group SVG element (pirchart)
       .transition()           //Set piechart transition on loading
       .attr("d", innerArc);

   arcs.filter(function(d) { return d.endAngle - d.startAngle > .1; })     //Define slice labels at certain angles
       .append("svg:text")     //Insert text area in SVG
       .attr("dy", "0.20em")      //shift along the y-axis on the position of text content
       .attr("text-anchor", "middle")      //Position slice labels
       .attr("transform", function(d) { return "translate(" + innerArc.centroid(d) + ")"; }) //Positioning upon transition and transform
       .text(function(d) { return; }); // Append category name on slices

   visualization.append("svg:text") //Append the title of chart in the middle of the pie chart
       .attr("dy", ".20em")
       .attr("text-anchor", "middle")
       .text("churned customers")

   // Function to update barchart when a piechart slice is clicked
   function click(d, i) {
       updateBarChart(, color(i), datasetBarChart);

The d3BarChart function defines the default group to be visualized when the page is loaded, and no specific contract class is selected. The default group is the distribution of the tenure of churned customers.

d3BarChart accepts just one argument: the served bar chart data. barChart.js contains the following:

//Set up SVG dimensions and properties
const margin = {top: 20, right: 10, bottom: 20, left: 20},
width = 350 - margin.left - margin.right,
height = 350 - - margin.bottom,
barPadding = 5,
graph_misc = {ylabel: 4, xlabelH : 5, title:9};

// Setting the default group
const group = "All";

// Function to get the percentage values  for a specific selected group from the whole dataset.
function get_percentage(group, datasetBarChart){
   const _ = [];
   for (instance in datasetBarChart){
       if (datasetBarChart[instance].group==group){
   } return _;

function d3BarChart(datasetBarChart){
   defaultBarChart = get_percentage(group, datasetBarChart);

   const xScale = d3.scaleLinear()     // Barchart X axis scale
       .domain([0, defaultBarChart.length]) // Scale range from 0 to the length of data object
       .range([0, width]);

   const yScale = d3.scaleLinear() // Barchart y axis scale
       .domain([0, d3.max(defaultBarChart, function(d) { return d.value; })])    //Scale range from 0 to the maximum value of the default bar chart data
       .range([height, 0]);

   // // Selecting the div with id barChart on the index.html template file
   const bar ='#barChart')
       .attr('width', width + margin.left + margin.right)
       .attr('height', height + + margin.bottom)
       .attr('id', 'barChartPlot');

   //Adding barchart title
       .attr('x', (width + margin.left + margin.right)/2)
       .attr('y', graph_misc.title)
       .attr('text-anchor', 'middle')
       .text('Tenure group for churned customers');

   const visualization = bar.append('g')
       .attr("transform", "translate(" + margin.left + "," + ( + graph_misc.ylabel) + ")");

       .attr("x", function(d, i) {
           return xScale(i);
       .attr("width", width / defaultBarChart.length - barPadding)  
       .attr("y", function(d) {
           return yScale(d.value);
       .attr("height", function(d) {
           return height-yScale(d.value);
       .attr("fill", "#757077");

   //Adding  barchart labels
       .text(function(d) {
               return d.value+"%";
       .attr("text-anchor", "middle")

       .attr("x", function(d, i) {
               return (i * (width / defaultBarChart.length)) + ((width / defaultBarChart.length - barPadding) / 2);
       .attr("y", function(d) {
               return (yScale(d.value) - graph_misc.ylabel); //Setting the Y axis to represent the value in the served JSON data
       .attr("class", "yAxis");

   const xLabels = bar
       .attr("transform", "translate(" + margin.left + "," + ( + height + graph_misc.xlabelH)  + ")");

       .text(function(d) { return d.category;})
       .attr("text-anchor", "middle")
       .attr("x", function(d, i) {
           return (i * (width / defaultBarChart.length)) + ((width / defaultBarChart.length - barPadding) / 2);
       .attr("y", 15)
       .attr("class", "xAxis");           

Making the charts interactive

We have successfully created the default bar chart, but to make our project interactive, we will update the bar chart with new values upon selecting the pie chart slices.

The updateBarChart.js script will enable this functionality. It accepts three arguments: the group selected on the pie chart, the color of the pie chart slice, and the updated bar chart data:

function updateBarChart(group, color, datasetBarChart){
   const currentBarChart = get_percentage(group, datasetBarChart);

   //Defining chart scale, same as the default bar chart
   const xScale = d3.scaleLinear()
       .domain([0, currentBarChart.length])
       .range([0, width]);

   const yScale = d3.scaleLinear()
       .domain([0, d3.max(currentBarChart, function(d) { return d.value; })])

   const bar ='#barChart svg');  //Selecting the div containing bar chart ID and creating an SVG element

   // Add title to Barchart
       .attr("x", (width + margin.left + margin.right)/2)
       .attr("y", graph_misc.title)
       .attr("text-anchor", "middle")
       .text("Tenure group for churned customers "+group);

   const visualization ='barChartPlot')
       .datum(currentBarChart);    //binding data to multiple SVG elements

       .attr('x',  (width + margin.left + margin.right)/2)
       .attr('y', graph_misc.title)
       .attr('class', 'title')
       .attr('text-anchor', 'middle')
       .text('Tenure group for churned customers '+group);

   const plot ='#barChartPlot')
       .datum(currentBarChart);        //binding data to multiple SVG elements

       .transition()       //Setting bar chart change transition
       .attr('x', function(d,i){
           return xScale(i);
       .attr('width', width/currentBarChart.length - barPadding)
       .attr('y', function(d){
           return yScale(d.value)
       .attr("height", function(d) {
           return height-yScale(d.value);
       .attr("fill", color);

       .attr("text-anchor", "middle")
       .attr("x", function(d, i) {
           return (i * (width / currentBarChart.length)) + ((width / currentBarChart.length - barPadding) / 2);})
       .attr("y", function(d) {
           return yScale(d.value) - graph_misc.ylabel;})
       .text(function(d) {
       return d.value+'%';})
       .attr("class", "yAxis");

Adding styling

Lastly, let’s add some styling to our HTML template. The stylesheet should be linked to the index.html file and contain the following styles in the styles.css static file:

/* Reset default browser settings */

/* Box sizing rules */
*::after {
 box-sizing: border-box;

/* Remove default padding and margin */
* {
 padding: 0;
 margin: 0;

/* Set core body defaults */
body {
 position: fixed;
 display: flex;
 background: #fdfdfd;
 scroll-behavior: smooth;
 text-rendering: optimizeSpeed;
 font-family: "Roboto Mono", monospace;
 font-weight: bold;
 -webkit-font-smoothing: antialiased;
 overflow-x: hidden;

/* Make images easier to work with */
img {
 max-width: 100%;
 display: block;

.about {
   margin: 10% 2%;
   width: 40%;
   text-align: justify;

h1 {
   text-decoration: underline;
   margin: 0.5em 0em;

p, h2, h6 {
   margin: 0.7em 0em;

a {
   text-decoration: none;

.visualization {
   display: flex;
   align-items: center;
   flex-direction: column;

#pieChart {
   margin-top: 4em;
   font-size: 12px;

#barChart {
   font-size: 9px;
   margin-top: 4em;

#pieChart .title, #barChart .title{
   font-weight: bold;

.slice {
   font-size: 8px;
    font-family: "Roboto Mono", monospace;
   fill: white;
   font-weight: bold;  
   cursor: pointer;

The interactive data dashboard has been successfully built! Finally, we will run our Flask app on the terminal as follows:


The web application will be hosted on our localhost:5000, which can be accessed via any browser.


In this article, we covered how to build an interactive dashboard for charts using served and preprocessed data with Python’s Flask. We manipulated DOM elements to render the visualizations with Javascript D3.js on a webpage. You can use this technique to render bar charts or pie charts, and easily incorporate data visualizations in your next project.

: Full visibility into your web apps

LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page and mobile apps.

Brain Aboze Technology Business Analyst | Data Scientist | Technical Writer

One Reply to “Build interactive charts with Flask and D3.js”

Leave a Reply