Tag Archives: django code for practice

how-to-create-bar-chart-with-matplotlib-and-django-3

How to create Bar Chart image with Matplotlib in Django

Hello World, Welcome to projectsplaza.com. Today I am going to discuss how to create a bar chart image with the Matplotlib in Django 3. We will create a bar chart according to data and save a png image and then display this image in the Django template. I have divided this tutorial into the following parts:

  • Matplotlib¬†introduction
  • Install¬†Matplotlib
  • Create a bar chart view method
  • Show bar chart in the template view

Continue reading

How to share common data in all Django templates

Hello World, Welcome to ProjectsPlaza.com. Today I am going to discuss how to share common data in all Django templates. In this tutorial, we will show common data in all templates with the help of a context processor.
According to the official documentation:
context_processors is a list of dotted Python paths to callables that are used to populate the context when a template is rendered with a request. These callables take a request object as their argument and return a dictionary of items to be merged into the context.

We will create a very simple example. In this example, we will create some view methods and their relative template. Then we will pass dictionary data to context processor and show in all templates.
I have assumed that you have basic knowledge of Django. If you are new to Django then please see our beginner tutorials.
I have divided this tutorial into the following steps:
  • Create shareData app and add to project
  • Create two view method index and show with their relative HTML templates
  • Create a URL and add to the project to show the template view
  • Create a third view method to pass data in a context processor
  • Access the data in both templates

Continue reading

create-piechart-in-django-with-matplotlib

How to create piechart with matplotlib in django 3

Hello World, Welcome to projectsplaza.com. Today I am going to discuss how to create a piechart with the Matplotlib in Django 3. We will create a piechart according to data and save a png image and then display this image in the Django template. I have divided this tutorial into the following parts:
  • Matplotlib introduction
  • Install Matplotlib
  • Create a piechart view method
  • Show piechart in the template view

Matplotlib Introduction

According to the official website: Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.


Install Matplotlib

First of all, we will install the Matplotlib package with the following command.

pip install matplotlib

Add this package to your project-level settings.py  in the installed apps list.


Create a view method for piechart

In this step, we will create a method to create a piechart with the matplotlib package. Add the following method in views.py file.

# MatPlotLib
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
import numpy as np

# Pie Chart
def piechart(request):
    # Pie chart, where the slices will be ordered and plotted counter-clockwise:
    labels = 'Sale', 'Purchase'
    sizes = [random.randint(10,30), random.randint(30,50)]
    explode = (0.1, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

    fig1, ax1 = plt.subplots()
    ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
            shadow=True, startangle=90)
    ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
    plt.savefig('media/sale_purchase_peichart.png',dpi=100)
    return render(request,'piechart.html')

In the above code, We are passing random numbers for sale and purchase value. Every time you refresh you will see the different piechart. You can replace the random number with dynamic values. We are showing a piechart as an image so we will do the following things to show the image.

  • Add the following code to the urls.py file.
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns=[
    path('',views.piechart,name='piechart')
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • Create a media directory as root because we are saving the image in this folder.

Show piechart in the template view

Till now we have saved the piechart in the folder. In this step, we will show the image in the Django template.

  • Create a piechart.html file in the templates folder and add the following code.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PieChart with Matplotlib</title>
</head>
<body>
    <h3>Sale Purchase Via PieChart</h3>
    <hr/>
    <img src="./media/sale_purchase_peichart.png" />
</body>
</html>

When you will run the application in the browser, you will see the following screen.