Archives for the 'Web Design' Category

In The Pursuit Of Shiny Web Stats

Being a very visual person, I naturally wanted a nice way of visualising the traffic lastgraph gets (especially as I nurse it through the initial round of bugs, most of which have gone). The ever-wonderful gltail always helps, but the same author has gone one further and come up with gltrail:

It draws paths between pages visited, and looks pretty nice when visualising the lastgraph logs. It’s also incredibly tempting to put it on a large screen in my room, the only problem being that I lack a large screen. And enough room.

In a related note, it would be nice if there were some sort of AJAXy, real-time, web-based statistics like gltail; anyone know of any? Someone must have put that <canvas> element to good use by now…

28 May 2008 | LastGraph, Web Design | 3 Comments

LastGraph3 Beta!

After much work, lastgraph3’s beta is now out. Please be warned that it may not work, may explode, and may abduct your cat, dog or goldfish in the process of failing.

Apart from that, please feel free to give it a try at lastgraph3.aeracode.org. Report any and all problems back to me, either here, or at my email or something similar.

Oh, and it’s also very slow at fetching until I re-negotiate fast fetching with last.fm, since I’ve moved IP.

Update: I’ve decided to make this one of those public, neverending betas, so LastGraph is now basically open for anyone and everyone to use. The last feature I added was LastGraph Premium, my new way of soliciting donations (this time, you can donate, and in return get a few extra features). I’m still not sure about it, but I’ve had some emails before from people who would have liked it, so we’ll see how it turns out.

24 May 2008 | Django, LastGraph, Python, Web Design | 19 Comments

Django and Cairo: Rendering Pretty Titles

One of the overwhelming horrors of designing for the web (or so it would appear from a lot of the mockups I’ve seen) is that designers (and people who are just bored of Arial and Times New roman) want to make their titles on web pages using non-standard fonts. “But that’s shockingly non-accessible and uses more bandwidth”, I hear you cry; well, there’s a reason the alt tag is around, and why broadband is much more common.

Well, perhaps this isn’t the sole reason, but nonetheless it’s a more than feasible idea these days to have headings, titles and short lines of text using fonts a user doesn’t have installed on their system. And, until the spec for embedding fonts is finialised in around 2065, there are two main options:

  • Render the text using *cough* Flash. While Flash is perhaps not as bad as some would make out, it’s still horribly proprietary as well as having a noticeable loading time and a few other invented disadvantages that strengthen my case for using…
  • Images. They’re lighterweight, and have been used for showing custom graphics since the dawn of [UNIX] time.

So, we need header images. One horribly labour-intensive way of doing this is making them manually in Generic Graphics Editor 8.6. However, since we’re sensible people, we’ll generate them on the fly. And, since we’re sensible people, we’ll be using Django*, so we need to write some nice Python code.

I’ll be using Cairo to generate graphics, in part because it’s a nice library, and is pretty common these days. You’ll need the Python Cairo bindings; on debian-like systems, this is the package python-cairo; in other places, your mileage may vary.

The key to making Cairo work with Django is wrapping a Cairo canvas in a django view. For this reason, I have this function lying around:

def render_image(drawer, width, height):
import os, tempfile, cairo
# We render to a temporary file, since Cairo can’t stream nicely
filename = tempfile.mkstemp()[1]
# We render to a generic Image, being careful not to use colour hinting
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width), int(height))
font_options = surface.get_font_options()
font_options.set_antialias(cairo.ANTIALIAS_GRAY)
context = cairo.Context(surface)
# Call our drawing function on that context, now.
drawer(context)
# Write the PNG data to our tempfile
surface.write_to_png(filename)
surface.finish()
# Now stream that file’s content back to the client
fo = open(filename)
data = fo.read()
fo.close()
os.unlink(filename)
return HttpResponse(data, mimetype=”image/png”)

The idea is, you pass it a function which will draw the image onto a context, and the image’s width and height, and it takes care of all the boring tedium of wrapping cairo and django together.

Now, that’s not very useful by itself, is it? Time to draw some text!

Firstly, as an aside, we need a way of seeing how big a certain text string will be for a given font and size, so we can render an image just big enough for it. This function achieves that:

def text_bounds(text, size, font="Sans", weight=cairo.FONT_WEIGHT_NORMAL, style=cairo.FONT_SLANT_NORMAL):
import cairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
context = cairo.Context(surface)
context.select_font_face(font, style, weight)
context.set_font_size(size)
width, height = context.text_extents(text)[2:4]
return width, height

Yes, yes, it’s somewhat cryptic, but it does the job. Now, we can write a text-rendering view!


def render_title(request, text, size=60):
# Get some variables pinned down
size = int(size) * 3
font = “Meta”
width, height = text_bounds(text, size, font)
def draw(cr):
import cairo
# Paint the background white. Replace with 1,1,1,0 for transparent PNGs.
cr.set_source_rgba(1,1,1,1)
cr.paint()
# Some black text
cr.set_source_rgba(0,0,0,1)
cr.select_font_face(font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
cr.set_font_size(size)
# We need to adjust by the text’s offsets to center it.
x_bearing, y_bearing, width, height = cr.text_extents(text)[:4]
cr.move_to(-x_bearing,-y_bearing)
# We stroke and fill to make sure thinner parts are visible.
cr.text_path(text)
cr.set_line_width(0.5)
cr.stroke_preserve()
cr.fill()
return render_image(draw, width, height)

Here, we construct the draw function with a simple text drawing command, and run the wrapper.

There’s some interesting text positioning going on up there; for more on this, and cairo in general, read through the excellent Cairo Tutorial for Python Programmers.

The last thing is to add an appropriate URL into your URLconf, such as

(r'^title/([^\/]+)/(\d+)/$’, “render_title”)

And then, when you browse to /title/HelloWorld/20/, you’ll hopefully get a nice PNG of your new title! Then, you can just use img tags instead of titles, in this sort of style:

<img src="/title/{{ item.title }}/20" alt="{{ item.title }}" />

This process is quite quick, but not without a small cost of processing power; if you’re using it a lot, think about some sort of caching. Apart from that, be happy with your newfound title freedom…

* Or possily Pylons. As long as you don’t go and cavort with those Gems On Guiderails people, or heaven forbid the [PH/AS]P guys…

15 December 2007 | Django, Python, Web Design | 6 Comments

Re-Re-Redesign

I’ve redesigned the ByteHoard website again; for those of you who’ve missed its last two years this is about the seventh design. This new one focuses on clean lines and readability, as well as saving a lot of bandwidth.

CSS is still a bit behind where it should be, although that’s obviously not the specifications’ fault (I’m looking at you, IE). However, I only had to make one change to my CSS to get IE to play nice (and it’s not a hack, I just had to set only one of a bunch of nested elements to display:block), so there is that.

Apart from that it seems to have gone well. My usual basic txt CMS is driving it, with my new submenus which I first developed for the Aeracode main site thrown in too. With some RSS fetching optimisation (i.e. only do it on the news page) it’s also a lot faster.

The logo also got an overhaul; I finally found the right font, NeoSans, and used that to make a much cleaner logo.

Overall, it went pretty well, I think (well, for only 4 hours of coding that is). Now back to ByteHoard proper.

28 October 2006 | ByteHoard, Web Design | No Comments