Web development: Google Charts, Flask, and Jinja2

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

The rendered webpage produced by my simple webapp.

Ta-da!

 

I hope that was helpful!

AC

Tags: , , , , ,

End of term in sight

A post from my phone! If it seems short, that’s why.

I’m excited to finish my work term in another two weeks. I’ll be visiting Mississauga for the last week, which I’m looking forward to. Work itself seems promising, especially my work term report.

I’m almost definitely getting some new camera equipment, including the Canon 50mm f/1.4, and a bunch of accessories. I haven’t picked it all out yet, but I’m excited nevertheless.

Good night!

Tags:

Python decorators

If you’ve ever had to create a class method or a static method in Python, then you’ve probably used a decorator.  Decorators basically wrap extra goodness around a function – that’s why @classmethod and @staticmethod are built-in: they’re commonly-needed functionality that you might need to add to some otherwise-ordinary function. The Python docs are surprisingly light on information about decorators; the entry for Function definitions mentions them in passing, but PEP 318 documents quite a lot about what they are, how they work, and which decisions were made in their implementation.

Today, I was getting comfortable with ReportLab at work, and noticed an opportunity to write my own decorator.  Here’s some code straight from the user guide:

Notice those calls to save the state and restore it again after each function finishes? That’s just boilerplate that we can put in a decorator. Try this code on for size, instead:

Now, admittedly, this doesn’t appear to save us a lot of space. We save two lines per function, but we add one to decorate it. Since the decorator definition took six lines, that means we have to apply it six times to break even, with regards to code size. Personally, I find the increase in readability makes it worthwhile even for just two functions, at a net gain of four lines of code. The semantic content of the function is expressed more concisely. Furthermore, in general, decorators can save you a lot more than one line of code per use, and be used many more times than twice in the course of your program.

For some more interesting applications of decorators in Python, such as memoization, check this out.  Have fun!

Tags: , ,

Amateur Development

Many people have said that it takes approximately 10,000 hours of dedicated effort to become an expert at something, perhaps most notably Malcolm Gladwell in his book Outliers. A good friend of mine characterizes this in everything he does. The guy doesn’t know the meaning of the phrase “slow down” – if he wants to do something, he’ll do it, and he’ll give it his all. This is a guy who decided to learn to play the guitar, and was playing Classical Gas in a month. As an engineering undergrad, he decided to be more artistic, so he took up sketching and painting, and is now very talented – I lent him my Nintendo DS once, with Colours! on it, and it came back with phenomenal paintings of a samurai, a robot, and a still life of a bowl of fruit. He’s a juggernaut, and I’ve decided that there’s nothing intrinsically special about him that lets him do it, aside from an abundance of passion and drive.

There are a number of things I would like to get better at myself, so I’m going to take a page out of his book, suck it up, and produce. I want to be a better programmer, so I’m going to write more code. I want to be a better photographer, so I’m going to take more photos. I’m going to play more music, get more exercise, and cook more food. One thing I don’t need to get any better at is wasting time on the Internet. There are way too many cool things to be doing, and I’m going to go do them!

In order to hold myself to task, I’m also going to try to publish them. Code will be pushed to my GitHub account; photos, to my Flickr account. Exercise is already getting logged by CardioTrainer. I’ll try and post more to Slapapatula, although I’ve been enjoying some new recipes lately despite the lack of updates there. Finally, I’ll post here whenever I work on any of those.

Okay! Let’s go!

Tags: ,

Hello, Android!

I have some time off right now between an academic term and a work term, so I thought I’d entertain myself by working on a short project.  While I’m definitely taking advantage of the warm weather to go for bike rides and take photos of the valley, and getting the parental units to take me out for dinner here and there, I decided to write an app for Google’s Android platform.

I chose Android for a couple reasons.  For one, I can develop for it for free – I don’t have to pay anybody $99/year, or buy a brand-new laptop, just to write a software program.  For another, I have an Android phone, so whatever I create will be useful to me personally.

Installing and configuring the Android development environment was fairly straightforward.  Google makes life very easy on developers using Eclipse, so that’s what I did.  The process was simple:

  1. Download the SDK here,
  2. Follow the instructions here.

Once the Eclipse ADT plugin was installed & configured, the included Android SDK and AVD Manager made downloading new Android platforms, Google APIs, sample code, and documentation a breeze, and all from within Eclipse!  Better yet, the AVD in Android SDK and AVD Manager stands for Android Virtual Device – an emulated Android phone to test your applications on!

In about ten minutes, I was up and running with a “Hello, World!” application.  This tutorial walks you through platform installation (which version of Android you want to target), creation of a virtual Android device, and then the actual coding itself – first, with a programmatic user interface layout, and then using XML.

Hello, Android!

Hello, Android!

So far, I’m very impressed with the ease of creating a UI layout.  The XML is very nice to work with, and it’s a very natural way of expressing the interface layout.  The Android docs, too, have been excellent so far – they have some great tutorials, they’re easy to follow, they’re visually appealing, and they’re thorough.  I haven’t actually created anything particularly complex yet, but I anticipate that the logic side of an app will be just as easy as the UI has been – and this is coming from a guy who’s never programmed a GUI in his life!

I’m looking forward to becoming more familiar with the Android platform in the days to come – but I think that the immediate future holds a Clint Eastwood movie and some bulk candy.  Stay tuned!

Tags: ,

Debugging

Excerpts from the commit log of a Subversion repository I am privy to, in chronological order, for your enjoyment:

r281: Fixed parsing bug in (filename).  Now, it doens’t care if there are double spaces or no spaces at the ends of lines.
r282: Added a debug message.
r283: This parsing bug is starting to drive me out of my mind.
r284: Okay, I think this bug is actually squished now.  (*crossed fingers*)
r285: THERE SHOULD BE A GODDAMN LICENSE FOR USE OF ‘#define’ !!!!

These were all made by the same author.  I hope he doesn’t murder his poor coworker…

Tags: ,

New host; CTo'W

Welcome to my weblog’s new home at WordPress!  Make sure to update your bookmark or RSS feed address.

Sorry I’ve been out of touch for awhile; I’ve been pretty busy lately – which isn’t to say I’m not now! – but with the new address and some recent finds I thought it was about time for another edition of CTo’W.

This arbitrary period of time, I’ll be focusing on tech stuff.  Given my technical interests, this should hardly be surprising.  First of all is an article I found some time ago about genetic programming, titled Creatures From Primordial Silicon.  Definitely worth a read, whether or not you’re a genetic programmer.  Here’s a taste: “A further five cells appeared to serve no logical purpose at all–there was no route of connections by which they could influence the output. And yet if he disconnected them, the circuit stopped working.

For those of you unfamiliar with genetic programming, it is, in short, a way of phrasing a complicated problem in a way that the computer can find a good solution for it.  That description is extremely general and pretty useless without more detail, though, so here we go: the kind of problem that genetic programming typically solves is one with no one correct answer, but rather a multitude of them – and when you have a “good” solution, it may not have a black-and-white distinction from a not-so-good one.  For example: “Design a bicycle” is a very difficult problem.  There are many factors to consider: what kind of riding is being done?  How much shock absorption do you need?  How light should the frame be?  How strong should the frame be?  (These last two requirements are an example of a tradeoff – more strength means more weight.)  The problem pretty obviously has no one right answer, or there would only be one kind of bicycle on the market.

Genetic programming tries to answer the problem by taking a page out of evolution’s book: it starts off with a bunch of random “gene sequences”, analyzes their performance according to a “fitness function”, “mates” some of the better ones, introduces random “mutations”, and ends up with a new pool of genes.  This process is repeated many, many times, until one of the gene sequences performs acceptably well according to the fitness function.  In genetic programming, a “gene” is one of the parameters of the problem – for example, “length of front shock”, “length of rear shock”, “number of front gears”, “number of rear gears”, “frame composition”, etc.  A “gene sequence” is simply the combination of all of the genes required of a solution, with a unique combination of values for all the genes.  The “fitness function” is what, in nature, causes “survival of the fittest” – those gene sequences with the highest survival and reproduction rate tend to dominate the gene pool.  In genetic programming, you define what the fitness function is, so you get to pick which traits to focus on.  In our bicycle example, the fitness function may include such factors as “failure rate of the frame” (i.e. how often the frame bends or breaks), “effort required” (how much work it takes to perform the same task on different bicycles), and some measure of “safety” – how often an equally-talented rider will end up falling off the bike, for example.  “Mating” and “mutation” are simply ways of producing new gene sequences; obviously, in mating, some genes are taken from two (or more) parents to create a new sequence, while in mutation, random changes are made to some genes, producing a new sequence.  The child of a mating process may also get mutated a bit, too.  There you have it!  A two-minute introduction to genetic programming.  (If I haven’t done a very good job explaining something, please feel free to ask questions in the comments.)

My next Cool Thing is targeted to programmers: Free Programming Books.  It’s an excellent collection of programming books on a variety of languages and topics, including such classics as The C Programming Language, Programming Pearls, and How To Design Programs.  I’ll be spending some time very soon downloading many of these to my collection of ebooks, even though I already own some of them in hard copy.

And, finally, my last Cool Thing for this arbitrary time unit: Moon, a film by Duncan Jones.  Don’t watch too much of the trailer; it’s rather long and gives away a lot of the movie.  Don’t be afraid to stop it before it’s finished playing!  Having said that, I’m looking forward to seeing it in theatres in June.  Think you recognize the astronaut?  He played Zaphod Beeblebrox in HHGttG.  The voice of the computer?  Kevin Spacey.  Yep, I can’t wait either.

Aaand that’ll just about do it for me.  You stay classy, The Internet.

AC

Tags: , ,

CTo'W

Hey, it’s been a few weeks! What’s happening?

I thought I’d stop by and drop you a link to this interesting article about work, hours, and the seemingly odd statement that “counting hours doesn’t make sense”. Even if you don’t really have the option to simply work when you like, and not when you don’t, there are some valuable insights about activities that can help increase your energy.

Short post for today. That’s all for now!

AC

Tags:

CTo'W and a personal project

Hello again! I am back. This week, I’ll be talking about LaTeX, XP-Dev, and a personal project I’m working on.

LaTeX is a programming language used to typeset documents. Instead of a WYSIWYG editor like Microsoft Word, it’s a WYSIWYM language to describe elements of what you’re writing and let styling rules decide how your document should look. This is nice for a few reasons; notably, once you’re familiar with the language, it is very easy to create documents. You can write it in whatever text-editor you like, set up your document information (which paper size you’re using, whether it’s a report, an article, a book, etc.) and go. Whenever you have a section, you just say so:
\section{Section Title}
Lorem ipsum dolor sit amet…

and LaTeX (or one of its slaves – I’m still pretty fuzzy on what happens behind the scenes) will make it look nice for you. The Wikipedia page has some example LaTeX code and the corresponding output to check out. I’m trying to move to LaTeX for future documents – probably my work term report this term, for example – because I don’t enjoy dealing with Word to try and get my document to look just right.

Next up: XP-Dev.com! In an earlier post, I mentioned Subversion. XP-Dev is a free Subversion host that includes project tracking capabilities as well. I’m really enjoying it so far – I had a minor problem the other day with something, so I reported a bug. The issue was dealt with in four hours, complete with a friendly, “Hey – should be fixed now :)” I’m working on a couple different projects we’re hosting at XP-Dev; for one of them, there are five of us on the team and the forums are proving to be a very useful way to discuss the project. We can also create and assign tasks, so it’s very easy to see what’s been done and what is left to do. This brings me to…

My personal project! I haven’t been making much progress on it lately, but my plan is to write an audio visualizer, like you can see in Windows Media Player or foobar2000 or Winamp or Amarok or whatever else you like. Since I’ve been using XP-Dev lately, I decided to track the project with its utilities, and create this wiki page describing the project in a little more detail than I’ll do here. There are still a lot of details to work out, but I’m looking forward to it! I might wrangle a programmer friend or two into helping me with it over Reading Week… (Not that I have Reading Week while I’m on co-op, but they do.)

That’s it for now. Cheers!
AC

Tags: , , ,

CTo'W, round 2

For those of you just joining us, CTo’W is my “Cool Thing of the Week”. It may or may not actually be weekly; we haven’t figured that one out yet. For those of you still with us, welcome back! Glad to have you here. Huddle up and let it snow (preferably from the right side of a nice, thick, well-insulated wall), because here we go!

This week, I decided to share an article I read at defmacro.org called Taming Perfectionism, by Slava Akhmechet. I liked this article because it corresponded so closely with what I’ve been feeling lately – unmotivated to start anything because nothing I do will live up to my own expectations or standards. When I conceive of something, I have a vision in my head, and when I try and implement it, it either disappoints my vision or it falls by the wayside, incomplete. When either of these things happen, I end up frustrated and unhappy, and less likely to pursue another project in future. Slava writes about Wabi-sabi, a Japanese school of thought centred on the concept of beauty as it applies to things that are transient or impermanent. I really enjoyed the article and it helped me understand my own behaviour a little better. I’m going to have to pick up the Book of Tea he mentions… if I ever get around to reading the half-dozen books or so I already have piled up and waiting!

That’s all for the CTo’W for now. Work has been interesting lately. I am very enthusiastic about it, and I’m looking forward to see where we get by the end of the week! It’s very motivational to be working for my boss; he’s an enthusiastic, intelligent guy, and his enthusiasm for work, life, and everything else is highly contagious. Maybe I’ll write more about the work I’m doing in a later post.

Cheers for now,
AC

Tags: , , ,