DEV Community

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

Posted on β€’ Edited on

1 1

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 tool which draws basic graphs in the terminal

Termgraph

A command-line tool that draws basic graphs in the terminal, written in Python.

Graph types supported:

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

Examples

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

An example using emoji as custom tick:

termgraph data/ex1.dat --custom-tick "πŸƒ" --width 20 --title "Running Data"

# Running Data

2007: πŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒ 183.32
2008: πŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒ 231.23
2009:  16.43
2010: πŸƒ 50.21
2011: πŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒ 508.97
2012: πŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒπŸƒ 212.05
2014:  1.00

An example using stdin and emoji:

echo "Label,3,9,1" | termgraph --custom-tick "πŸ˜€" --no-label


πŸ˜€πŸ˜€πŸ˜€ 3.00
πŸ˜€πŸ˜€πŸ˜€πŸ˜€πŸ˜€πŸ˜€πŸ˜€πŸ˜€πŸ˜€ 9.00
πŸ˜€ 1.00

Most results can be copied and pasted wherever you like, since they use standard block characters. However the color charts will not show, since they use terminal escape codes for color. A couple images to…

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

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay