The power of simple python scripting is one of the themes of this blog, and now I can show a great practical example.
I needed to create something that looks like this:
This is a simple HTML table with some CSS. But trying to write it in HTML would result in a lot of copy-pasting of stuff that looks like this:
That’s a lot of boring and error prone copy-pasting. And I’ll need to create lots of these in the coming months.
OK, so I need to write a script that will turn this:
Into the correct HTML.
It’s actually really easy:
It’s a small command-line tool that knows how to create an empty .py file for a canvas with the correct template, and knows how to read a .py file as a python file (since this is for self-use only I’m fine with no security at all, otherwise reading it as a python file would be very unwise) and do some preprocessing and some search-and-replacin’ to get the right thing into an HTML template.
I needed to be able to control how many notes to have in a row, so I added parsing of a space at the beginning of a note caption to mean “newline before this”. I needed to write many similar notes for “customer segments” – so I used the fact that the canvas definition file is in python to just write some python that generates it:
“customers”: [x + ” Fans” for x in [“Comics”, “RPG”, ” MTG”, “Anime”]] + [” Gamers”],
And that’s all there is to it. One of my students could write this in ten minutes. From now on, something that I will be doing every day will take a few minutes less.
The whole code can be found here: https://github.com/SonOfLilit/quickbmc and the complete result here: https://github.com/SonOfLilit/quickbmc/tree/gh-pages.