Update [04/2021]

A couple of discoveries followed my initial playing with Clojure and Vega library.

First, the author of Babashka himself pointed to me on twitter that Bb is now bundled with http-kit which should streamline creation of app-server scripts. No need to bootstrap everything from scratch, unlike in the following example.

Another one is Feedgnuplot: a nice frontend to Gnuplot that simplifies data piping to charts. I ran into it while searching for an ascii-terminal plotter that would fit for my StumpWM extensions. However, it is in fact a handy "every day gnuplot inliner".

Babaska + Vega-lite plotter app (Original)

For the Clojure shell scripting environment with a funny name Babashka there is a simple example of a web server. It's funny and impressive at the same time, being only a short single script.

I've remixed it and thus got me a console Vega-Lite plotter (that of course requires a browser to view the plot).

The command looks like

$ vega-serv.bb -p PORT -f FILE

Where FILE is a clojure EDN with a map containing full Vega-Lite grammar spec for the plot. It is converted to JSON via Cheshire library shipped with Babashka. An example that contains data points:

{:$schema     "https://vega.github.io/schema/vega-lite/v4.json"
 :description "A simple bar chart with embedded data."
 :mark "bar"
 :encoding {:x {:field "a" :type "ordinal"}
            :y {:field "b" :type "quantitative"}}
 :width  800
 :height 600
 :data {:values [{:a "A" :b 28}
                 {:a "B" :b 55}
                 {:a "C" :b 43}
                 {:a "D" :b 91}]}}

Looks even cleaner than JSON to me.

I might want to remix it a bit further, to e.g. serve a CSV-file with data points separately from the view-encoding map. But right now I see whole maps like this more likely to be built inside a shell pipe through other calls to BB.

Such implementation already has a couple of pros:

  1. Starts pretty fast, thanks to Babashka binary
  2. I can tweak the resulting plot by editing the description file, no need to stop the server
  3. It is a single-file exectutable script placed under $PATH

Motivation? A rapid cli- plotter better than gnuplot!

Unless otherwise credited all material Creative Commons License by Vladimir Dikan.
Powered by Coλeslaw. Design elements' artwork created with MagicaVoxel.