Chart.js Flask: Create Beautiful Interactive Charts with Python

Tiffany Hwang, PythonChartJSData Visualization

As data visualization becomes more important in the business world, being able to create beautiful and interactive charts is a key skill for data scientists and analysts. Chart.js is a popular JavaScript library that allows users to create stunning charts and graphs, and Flask is a powerful web framework for Python. In this article, we’ll show you how to use Chart.js in Flask to create interactive charts and graphs with Python, even if you have no prior experience with JavaScript.

What is Chart.js?

Chart.js is a JavaScript library for creating beautiful and interactive charts and graphs. It’s easy to use and highly customizable, making it an excellent choice for anyone who needs to visualize data. Chart.js supports a wide range of chart types, including bar charts, line charts, pie charts, and more. It’s also highly configurable, allowing users to style their charts and add interactive features like tooltips and legends.

What is Flask?

Flask is a popular web framework for Python that allows users to create web applications quickly and easily. Flask is designed to be simple and lightweight, making it an excellent choice for smaller projects. It’s also highly customizable, allowing users to add extensions and libraries to enhance their applications.

Using Chart.js in Flask

To use Chart.js in Flask, you’ll need to include the Chart.js library in your application and create a route to render your chart. Here’s a step-by-step guide to getting started:

  1. Install Chart.js: You can download Chart.js from the Chart.js website, or you can include it as a CDN in your HTML file.

  2. Create a route: In your Flask application, create a route that will render your chart. Here’s an example:

def chart():
    return render_template('chart.html')
  1. Create a HTML file: Create a new HTML file called chart.html in your templates folder. This file will contain your Chart.js code. Here’s an example:
<!DOCTYPE html>
    <title>Chart.js Flask Example</title>
    <script src=""></script>
    <canvas id="myChart"></canvas>
        var ctx = document.getElementById('myChart').getContext('2d');
        var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
                datasets: [{
                    label: 'Sales',
                    data: [12, 19, 3, 5, 2, 3, 16],
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(255, 206, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(255, 159, 64, 0.2)'
                    borderColor: [
                        'rgba(255, 99, 132, 1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(153, 102, 255, 1)',
                        'rgba(255, 159, 64, 1)'
                    borderWidth: 1
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true
  1. Test your chart: Run your Flask application and navigate to the /chart route to see your chart.

Creating Interactive Charts

Now that you’ve created a basic chart using Chart.js and Flask, it’s time to add some interactivity. Chart.js supports a wide range of interactive features, including tooltips, legends, and animations. Here’s an example of how to add a tooltip to your chart:

options: {
    tooltips: {
        callbacks: {
            label: function(tooltipItem, data) {
                var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || '';
                return datasetLabel + ': $' + tooltipItem.yLabel;

This code will add a tooltip to your chart that displays the value of each bar when you hover over it.


In this article, we’ve shown you how to use Chart.js in Flask to create beautiful and interactive charts and graphs with Python. With its wide range of chart types and customizable features, Chart.js is a powerful tool for data visualization, and Flask makes it easy to create web applications that incorporate these charts. Whether you’re a data scientist or a web developer, learning how to use Chart.js in Flask is a valuable skill that will make your applications stand out. So start experimenting today and see what you can create!

© Big Data Analyst.SitemapAbout UsContact UsPrivacy Policy