Insane URL-fu

I had an interesting use case.
I needed to get some data out of a database, and then return the result in a graph. So far, the thing is not so interesting. However, there were a few twists.

First twist, I needed to provide a web form for the user to type in parameters for the DB search.

Second twist, I needed the result graph to show up on the same page as the original form, so that the user could refine the query by tweaking the inputs and seeing the results right away.

I’m using Django, which provides a very interesting service; that is, it can turn URLs into function calls. If I enter a URL that it knows about, it calls the configured function, passing along any HTTP request parameters with the call.

There is a template mechanism, too, so that I can set up the input form as a normal HTML < input > tag set.

So I get the input, no sweat. Pass it along to the DB, yawn. Parse the data into a graph — took a little effort, but still, no problem. Convert the graph to an in-memory PNG file to display in a browser, done.

And displaying that PNG in the browser on its own page was a cinch, you just pass a PNG to the browser, and it displays it.

But how do you put that PNG into the HTML template? Ah, there’s the rub.
The way to put an image into an HTML file is using < img src="file.png" >, but I don’t *have* a “file.png”, my PNG is in memory (and I’m not keen on writing it to disk every time).

And here’s where the URL-fu comes in.

Recall that Django can turn any URL into a function call. This includes .png files called out in the URL!

So to the template, I added:

< img src=”results.png” >

And to the URL-to-function config, I added:

(r’^result.png$’, ‘function_to_create_chart’),

And then I created a function:

def function_to_create_chart(request):
return create_the_chart(request)

(create_the chart being a function that eventually returns that in-memory PNG, which I dutifully pass along)

… and this, although it looks awesome, completely failed to work.

… because the *request* was empty! All those parameters that I sent from the form were not getting passed along.

OK, how to get the request information in there?

And this is where the beauty that is Django impressed itself upon me.

I changed the template to include HTTP GET parameters:

< img src="results.png" > became
< img src="results.png?key1=value1&key2=value2&key3=value3" >

(no, I’d never seen HTTP GET parameters passed to a PNG, either)

and then I made sure that when the user submits the form, I pass along the proper parameters.

And it worked. Ask for the initial URL, get a blank form. User fills in the form, presses submit, and here comes the chart.

I have to say that even though I made this, the technology in question is very nearly indistinguishable from magic.

Thus endeth the lesson.

This entry was posted in Computers, Knowledge, Technology and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *