I’m doing an internship in New York City this summer developing Android software. It’s a lot of fun! One of the many reasons why is that we have hackathons at the end of every two-week iteration.
For my project last Friday, I wanted to write a tool that would improve the visibility of some of our metrics. I wanted it to grab some data from our database and display it on a webpage. I’ve never done any web development before, but in a day (and a half) I managed to write a script to grab the data I wanted from our DB, and display it in a dynamically-generated column chart on a webpage. I sanitized my code and published it as an example on GitHub. Here’s a quick walkthrough.
The Data
Running SQL queries from Python is easy, and there’s plenty of information about that elsewhere. Once I parsed some data, I wrote the interesting bits to a simple text file for my web app to read. In the example, I just made up some data as a literal in the application code, but it would be very simple to write a file parser instead, or grab the data from some other source.
The Framework
Flask is easy to install and use. You can just pip install flask and then import it in your application! There’s a simple quickstart tutorial here which I followed – but I didn’t even need to read the whole thing to get it working well enough for my purposes.
The Magic
Surprisingly, that’s pretty much it. Once we’ve set up our app framework, all we have to do is make sure it routes our requests properly, and then use Jinja2 to plug our data into the Google Charts API. But first, we have to tell Google Charts what kind of data we’re giving it – in my case, a string (describing the date) and a number (containing the value to be graphed for that date). This is very easy and feels a lot like Python: see for yourself! If you don’t understand what’s going on there, spend a little time reading about Jinja2′s templating variables. We’re just wrapping up the calls to print the values of our variables (dayname and num) with the syntax that Google Charts expects to find them in (an array containing a string and a number).
The Result
I hope that was helpful!
AC

