Rendering Matplotlib Graphs in Django

If you have seen my post on the expense Django app, the dashboard is rendered using matplotlib’s static files which are passed on runtime to the webpage HTML.

See this here in action. All the png images are generated on the fly and sent to the browser to display.

Here’s the simple code stript down code to demonstrate this with a simple dummy graph

# DJANGO View code

from django.shortcuts import render
import matplotlib.pyplot as plt
import io
import urllib, base64

def home(request):
    plt.plot(range(10))
    fig = plt.gcf()
    #convert graph into dtring buffer and then we convert 64 bit code into image
    buf = io.BytesIO()
    fig.savefig(buf,format='png')
    buf.seek(0)
    string = base64.b64encode(buf.read())
    uri =  urllib.parse.quote(string)
    return render(request,'home.html',{'data':uri})
#HTML Template

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>matplot</title>
  </head>
  <body>
    <img src="data:image/png;base64,{{ data }}" alt="" height="250" ,width="250">
  </body>
</html>

Using matplotlib, io, base64 library to accomplish the whole thing. Matplotlib to plot, io to hold the plot byte buffer and then base64 to convert the bytes to string representations that the browser can use to display.

I could have used chartsjs or other javascript libraries which will give interactive plots, but this matplotlib implementation is a good place to start and it works without any more complexity.

Checkout these Similar Posts

Parsing Date Like a Boss with Python

Daily planning is one thing that I do regularly. For office related work, most of this is done in outlook and doing all this with GUI would be too distracting, so I have this cmd utility created in python. Here’s how this works

Type on cmd and it creates the appropriate meeting schedule.

meet Work on Scheduler Ehancements 12pm -d 60 

Here’s a demo of how this works in outlook

But this post isn’t about the meet app, maybe sometime later.

But behind the scene, the app works using the powerful date parser available in dateutil. The fuzzy=True keyword is that makes this possible and works like a boss.

Fuzzy=True, allows for strings like “Today is January 1, 2022 at 8:21:00AM”.

Few Examples

from dateutil.parser import parse

parse("2h30m", fuzzy=True)
>> datetime.datetime(2022, 2, 1, 2, 30)


parse("monday 10pm", fuzzy=True)
>> datetime.datetime(2022, 2, 7, 22, 0)

parse("3rd dec 10am", fuzzy=True)
>> datetime.datetime(2022, 12, 3, 10, 0)


Explore Docs here

Heart Equation

If you are following this blog for a long, you will know, there is always one heart post every other year mostly around valentines day,

The posts all almost always based on this equation.

Maths is so beautiful.

Well here’s this year’s rendition of the heart using the previously discussed comet plot.

The code for this is simple.

import matplotlib.pyplot as plt
import numpy as np



def comet(x,y=None, time=0.05, fill=False):
    """
    Displays a comet plot
 
    by Sukhbinder
    date: 15 Feb 2021
    """
    x = np.asarray(x)
    plt.ion()
    plt.xlim(x.min(), x.max())
    plt.axis("off")
    if y is not None:
        y = np.asarray(y)
        plt.ylim(y.min(), y.max())
    else:
        plt.ylim(0, len(x))
    if y is not None:
        plot = plt.plot(x[0], y[0])[0]
    else:
        plot = plt.plot(x[0])[0]
 
    for i in range(len(x)+1):
        if y is not None:
            plot.set_data(x[0:i], y[0:i])
        else:
            plot.set_xdata(x[0:i])
        plt.draw()
        plt.pause(time)
    
    if fill:
        plt.fill_between(x,y, zorder=100)
    plt.ioff()
 


def make_hearts(num=5):
    theta = np.linspace(0, 2 * np.pi, 100)
    # Generating x and y data
    x = 16 * ( np.sin(theta) ** 3 )
    y = 13 * np.cos(theta) - 5* np.cos(2*theta) - 2 * np.cos(3*theta) - np.cos(4*theta)
    
    
    for i in range(num):
        xx = x*(i+1)
        yy = y*(i+1)
        comet(xx,yy, time=0.00001, fill=True)

if __name__ == "__main__":
    make_hearts()

By the way. Happy Valentine’s Day.

You can also get the comet code from this gist repo

How Not to Miss any Outlook Appointment -Introducing Email Butler

I work on two PC’s. One is my local machine and another is a virtual remote desktop (VDI) machine where I connect remotely. The remote machine is where I spend most of my workday but the local machine has the primary email where all office-related meetings and other communications come by.

So have to manage and monitor two emails, this was hard and the context shift was enormous. And there was always a case that I missed many meetings just because I was working in the VDI machine.

So desperately needed a butler who can notify me of my emails and meetings in the local system when I was working on the remote system

As always turned o python for a solution. Here’s the result. A simple email_butler that monitors my email and calendar and keeps me up to date.

Combined with the winsay library package, this app is launched at a local system startup and I don’t have to keep an active eye on my local email.

How does this work, the system monitors the calendar entries every five minutes and informs the user if there is any new upcoming event/meeting coming in the next 30 mins.

How can you use this?

pip install git+https://github.com/sukhbinder/Email_Butler.git

and then add this batch command in your windows startup.

start ebutler

The added advantage of this setup is I don’t have to constantly look at my email anymore as I trust the email butler doing the work for me. 😛

Definition in Command Line

Kids are much smarter these days. Working from home I had this experience.

As soon I settle down to work my younger one will come to me and ask “Daddy what does ‘fluff’ mean? “ Sometimes I answered but most of the time, being busy with my own new work-from-home setup, the default answer was “ask google”.

This is when the whole thing goes south. Barun starts with this simple “define fluff” to google. Once he had his meaning he won’t stop, he will say, “hey google let’s play akinator?” or “ok Google play fart noise” That’s it from the simple definition, he soon transition to games with google.

If we asked them to use a dictionary that was more of a hustle as he kept pestering for more.

As always I turned to python. This project was meant for those situations. You need a definition, just type define that and the program will get the meaning and say the answer to you like an adult would.

A side benefit I noticed with this interface was kids increasing comfort around the shell and bash, which to me is good.

As always, the code can be found on my github

How To Make Your Computer `Chime`

When I was in year 4, my dad got me a Casio digital watch. Its hourly chime and alarm were my favourite features. Those days watches were the smart techs we had.

Three years back got myself a retro Casio digital watch just for the chime feature again. Since working from home, the watch has been in the closet. I missed the chimes

To compensate and to get the chime back in my work life, wrote this simple python program that does the same on my laptop.

Distractions and youtube binging and unproductive web surfing or too long on the desktop, this chime at every hour reminds me to be conscious of what I am doing. It’s a nice way to be intentional at the moment.

Please note this use my previous project where I make my windows system talk. Instead of chime I just made it to say the time.

Here’s the code for the same.

https://gist.github.com/sukhbinder/a0c8a83ee9231bda90b21f4ca9637174.js
import sched
import time
import datetime
import subprocess


event_schedule = sched.scheduler(time.time, time.sleep)


def subprocess_say(msg):
    startinfo = subprocess.STARTUPINFO()
    startinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    iret=subprocess.run(["say", msg],
                   stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
                   startupinfo=startinfo)
    return iret

def chime_now():
    """
    Simple program that chimes or tells the time when

    author:Sukhbinder
    """
    now = datetime.datetime.now()
    minute = now.minute
    if minute in [0, 30, 55]:
        hour = now.hour if now.hour <= 12 else now.hour - 12
        if minute == 0:
            msg = "Its {} O clock".format(hour)
        if minute == 30:
            msg = "Its half past {}".format(hour)
        if minute == 55:
            msg = "It is {0} {1} ".format(hour, minute)
        iret = subprocess_say(msg)
    
    event_schedule.enter(60, 1, chime_now, ())


if __name__ == "__main__":
    event_schedule.enter(10, 1, chime_now, ())
    event_schedule.run()

Hope someone else finds it useful.

4-7-8

This was one of the forwards from the HR team when the pandemic started for everyone’s well being.

4-7-8 Breathing technique to calm your mind. It was cool and just for the sake of it, did develop a mini-app in python that helps do this from command line.

Here’s a demo.

Works in windows, Linux and Mac.

As always, code available on github

How to Install and Use the Django Expense App

A cousin of mine wanted to use the Django-expense app that I shared a few weeks back, so here are the instructions I sent him.

Step 0:
Get python

Step 1:
Install the required modules using the below requirement.txt

asgiref==3.3.1
cycler==0.10.0
Django==3.1.7
git+https://github.com/sukhbinder/django-exp.git@b699077cc0846dbe40dfd1b2a17c1c164d1bc0aa#egg=django_exp
djangorestframework==3.12.2
et-xmlfile==1.0.1
kiwisolver==1.3.1
matplotlib==3.2.0
numpy==1.20.1
openpyxl==3.0.7
pandas==1.2.3
Pillow==8.1.2
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2021.1
six==1.15.0
sqlparse==0.4.1
xlrd==2.0.1

pip install -f requirements.txt

Step 2:
Create a new Django project

django-admin startproject expensesite

expensesite can be any name you want to call the website

cd expensesite

Step 3:

In the settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Add these two lines
    'rest_framework',
    'exp',
]

Step 4:

python manage.py makemigrations
python manage.py migrate

Step 5:

python manage.py runserver

This should launch the website.

Code as always is available at my github repo

Managing Housing Society Expenses

Last summer, while in home quarantine, received an update from the housing society committee on the general group, that nominations are invited for new managing committee. I ignored it. Come September, learnt that many ignored the same and now by way of a lottery system I was one of the chosen few to take up a role in the managing committee.


This is how I landed the job of the Treasurer of the housing society. The responsibilities sounded simple when it was explained but in practice it proved too much work.

Ad-hoc expenses, regular expenses. It was hard to manage. The only advise I received from the leaving Treasurer was, record the expense as soon as you incur it.


I bought a diary and started doing this but it became evident that recording expenses in diary and then transferring it to an excel sheet will be time consuming. The volume and variety of expenses from various sources overwhelmed my paper based system.

This is the problem that was beckoning to be solved and I turned to python.


Here’s the result of the solution.

Developed a Django webapp that can be accessed from anywhere, on any of my devices. The web app allows not only allows adding expenses but also shows and consolidate the expenses.


It was fun experience building this. The site is solely used by me now and has made logging of expenses straight forward. Solving this, turned my attention to another problem that had bigger time-toll in my duties as treasurer, but that’s a topic for another post.

Intersect

Last month, someone emailed me after checking out my post on intersection of two curves in numpy.

He wanted to use the code but enquired if the code was available on the pypi for easy installation. This enquiry prompted me to look at the code that I wrote 4 years ago. To my surprise, this code was the most starred and popular amongst my github contributions.

So I did what I have never done for my code, to put it on pypi.org for people to be able to install the library with a simple

pip install intersect

Thanks to the user!!

Record Screen With Python

There a tons of screen recording software available in the market, but you don’t want to install one more software for just one usage. Or sometimes installing a freeware or a paid ware is not allowed.

What to do? you improvise. If you know and have python on your system, you are in luck.

This is what I did, when I had few occasions where I wanted to show my work. A quick gif or a small movie goes a long way, so created this app using python to records shot clips .

Here’s a demo of the same using the tool.

The entire code and tool is available at my gihub here

Winsay

The thing I like about MacBook is that it comes with simple features/apps in-built that makes life easy. And being forced to use windows at office I have tried to use python as the bridge to fix these gaps.

One of the feature I like about mac for entertaining kids, and to relax my eyes while reading content is the ease with which Mac allows the reading of text on the screen. Alt escape and it reads the selected text. I have entertained kids with the say command on many rainy days and now use it as their spelling app to revise spellings.

So created winsay, an app that uses your computer to read in text and to have the same functionality as say in Mac.

Demo shown below.

Do check it out the code and the app here on GitHub.

Feap installation with visual studio

If you ever require a FEM system for quick fem for educational or research purpose. Feap is one of the easiest to get started with.

Here’s a rundown with screenshots on how to build it with visual studio with Intel Fortran.

Hope this helps.

Step 0:

Download from:

http://projects.ce.berkeley.edu/feap/feappv/feappv41.zip

Project Page:

http://projects.ce.berkeley.edu/feap/feappv/

Two steps

  1. Build a library
  2. Build the program
  1. Select New Project

  1. Select New Project
    1. Select Library: Select Static Library
    2. Name library e.g. lib22

  1. Under the Projects tab select Add Existing Item
    1. Add all subroutines in directories: Elements, Plot, Program, User, and Windows (do not include Unix, Include, or Main).

Under the Projects tab select Properties

Select Fortran then General

b.) Set additional include path to point to the feappv include

directory (e.g. c:\users\xxx\feappv\ver22\include) and the appropriate

    directory for 32-bit or 64-bit pointers (e.g.

    c:\users\xxx\feappv\ver22\include\integer4 or

    c:\users\xxx\feappv\ver22\include\integer8)

Build library

Building the Main program

Open Visual Studio or if open new project

a.) Select QuickWin Application, select QuickWin option

(not standard graphics QuickWin)

b.) Name main program e.g. feappv

At top select Release build (as opposed to Debug)

3. Under the Projects tab select Add Existing Item

a.) Set show all files in window and add library (e.g. lib22)

Visual Studio normally places this in

c:\users\xxx\documents\visual studio\projects\lib22\lib22\release

b.) Add feappv.f from the subdirectory Main.

4. Under the Projects tab select Properties

a.) Select Fortran then General

b.) Set additional include path to point to the feappv include

directory (e.g. c:\users\xxx\feappv\ver22\include) and the appropriate

    directory for 32-bit or 64-bit pointers (e.g.

    c:\users\xxx\feappv\ver22\include\integer4 or

    c:\users\xxx\feappv\ver22\include\integer8)

Add the libraries and the library path

Build… If you get error like this, you have not included all forttan file sin the liberay include and come pback..



Updated Binary STL File Reader

bent_plate

A recent comment on the post titled binary stl file reader in numpy prompted me to revisit the code and while I was looking at it , I updated the same for code to view the loaded stl file.

The above picture shows the result.

The code as usual available on github from here

from binarySTLreader import BinarySTL,ShowSTLFile
h,p,n,v1,v2,v3=BinarySTL('bent_plate.stl')
ShowSTLFile(v1,v2,v3)

OD Command in Windows with Python

I have seen OD command for last two years but had never bothered to look it up. It always remained under the surface.

Life went on well without it, until now.

During a recent debugging crisis, had to resort to this command for an efficient way to check binary results. All the while had to go to Linux and copy the files. This is when I knew I have to see what this OD is?

OD is octal dump. Simple functionality, don’t know if there is a windows equivalent. So before searching for a windows equivalent, I knew I can replicate this in python and here’s my first attempt.

Works for me and suits my requirement. Not an entire mimic of actual OD command but if you are looking for long and double precision values, this does the job.

I have kept the command line arguments similar to the actual OD command. Here’s a simple demo.

As always, the code is available from my github repo here. Hope to add more functionality as and when I get more time. Please free to fork and play around.

Special thanks to Andy for introducing the command and helping me make sense of the important arguments.

Quick Gantt Chart with Matplotlib

ProjectPlan_GANTT_CHart_MatplotlibThe problem: You need a quick Gantt chart for a quick proposal report and you dint have any project planner software installed.

Solution:

While there are many different ways, you only have access to python, well Here’s a simple Gantt chart plotter with just matplotlib.

Not for rigorous use but a good substitute to make quick Gantt plot for quick report.

Continue reading

Quick Intro to 2D and 3D Visualization in Python

contour_visualization_with_python
Posted a quick tutorial introducing visualization in python.

This tutorial offers a quick look at some common 2d and 3d Visualization available in python. This tutorial is meant to give anyone a quick start in matplotlib and mayavi.mlab.

The tutorial was written as part of CSRP program of AeSIAA which I am volunteering as a mentor to few aeronautical students.

View the tutorial by clicking this Quick Intro to 2D and 3D Visualization in Python