DEV Community

Cover image for Draw charts from terminal with psql and termgraph
adriens
adriens

Posted on • Edited on

Draw charts from terminal with psql and termgraph

πŸ’‘ Open Data and charting in terminal

As a big fan of both Open Data, PostgreSQL, terminal tricks and data analysis, I wondered what kind of story I could tell on these topics.

Since one year now I feed an Open Data repo with COVID-19 from New-Caledonia :

GitHub logo adriens / covid19-action-plan-nc

Projet de gestion de mes tΓ’ches liΓ©es aux actions autour de la crise saniatire du COVD-19 en Nouvelle-CalΓ©donie

covid19-action-plan-nc

Projet de gestion de mes tΓ’ches liΓ©es aux actions autour de la crise sanitaire du COVID-19 en Nouvelle-CalΓ©donie

⚠️ Convention sur les données

Depuis le mercredi 08/09/2021, cf #64, les chiffres suivants ne sont plus communiquΓ©s :

  • nb. de personnes en quatorzaine
  • nb. de tests quotidiens

En conséquence, et afin de garantir l'intégralité du modèle de données :

  • Lorsqu'un champ n'a pas la donnΓ© numΓ©rique : remplir avec -1
  • Un nouveau fichier simplifiΓ© covid19-nc-cas-actifs_deces.csv a Γ©tΓ© créé et sera maintenu avec moins de colonnes : y sont consignΓ©s les cas confirmΓ©s, le nb. de dΓ©cΓ¨s, le nb. de premiΓ¨res et secondes injections

RΓ©alisations

To "celebrate" this full year of daily datas, I decided to implement something cool around it and thought about the following scenario :

πŸ‘‰ The plan

  1. Git clone the Open Data COVID cases I feed everyday
  2. Load the csv data in the PostgreSQL table with the COPY statement
  3. Prepare some ready to use reporting views
  4. Dump these views as csv files
  5. Produce charts from the command line, for the command line with the help of a great and cool tool called termgraph :

GitHub logo mkaz / termgraph

A Python command-line and library that draws basic graphs in the terminal

Termgraph

A command-line tool and Python library that draws basic graphs in the terminal.

Graph types supported:

  • Bar Graphs
  • Color charts
  • Multi-variable
  • Stacked charts
  • Histograms
  • Horizontal or Vertical
  • Calendar heatmaps
  • Emoji!

Quick Start

Command Line Usage

$ termgraph data/ex1.dat

# Reading data from data/ex1.dat

2007: β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡ 183.32
2008: β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡ 231.23
2009: β–‡ 16.43
2010: β–‡β–‡β–‡β–‡ 50.21
2011: β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡ 508.97
2012: β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡ 212.05
2014: ▏ 1.00

Python Module Usage

from termgraph import Data, Args, BarChart

# Create data
data = Data([[10], [25], [50], [40]], ["Q1", "Q2", "Q3", "Q4"])

# Configure chart options
args = Args(
    title="Quarterly Sales",
    width=50,
    format="{:.0f}",
    suffix="K"
)

# Create and display chart
chart = BarChart(data, args)
chart.draw()
Enter fullscreen mode Exit fullscreen mode

Output:

# Quarterly Sales
Q1:
…

Also, to make it available even for non technical people.

I decided to implement it on Katacoda, so everyone can enjoy it from any web browser and look like a kind of hacker 😎

🎦 Demo

Top comments (0)