In this post, i describe the math behind nicely scaling tag clouds. Here is some sample code:

import math

# some test data in roughly the format that you would get it back from Tasty

tags = [('admin', 1), ('annotation', 1), ('annotations', 6), ('apache', 1),
        ('authentication', 2), ('backend', 27), ('caching', 1), ('cgi_app', 1),
        ('cherrypy', 1), ('cherrypy-branch', 2), ('client', 3), ('cookies', 1),
        ('CSS', 2), ('dependencies', 4), ('documentation', 8), ('email', 8),
        ('exif', 1), ('fields', 4), ('flash', 2), ('forum', 3), ('fozzy', 1),
        ('frink', 1), ('groups', 3), ('HTML', 2), ('items', 29),
        ('javascript', 2), ('keywords', 7), ('lifecycle', 6), ('linking', 1),
        ('meta', 1), ('metadata', 3), ('milestones', 8), ('mod_perl', 1),
        ('mod_python', 1), ('mysql', 1), ('navigation', 1), ('notification', 2),
        ('numbers', 1), ('paths', 5), ('permissions', 2), ('polish', 4),
        ('priority', 2), ('project', 17), ('publish', 1), ('python', 2),
        ('regexp', 1), ('related', 1), ('REST', 2), ('rotation', 1),
        ('search', 5), ('security', 3), ('selenium', 1), ('slideshow', 3),
        ('sporadic', 1), ('stickies', 1), ('svg', 1), ('sysadmin', 1),
        ('tagging', 4), ('tasty', 1), ('templates', 2), ('test', 2),
        ('testing', 1), ('tests', 1), ('tiki', 1), ('tiki regexp', 1),
        ('tutorial', 1), ('unicode', 2), ('usability', 3), ('user', 10),
        ('video', 2), ('wiki', 1), ('wind', 1), ('windows', 2), ('xml-rpc', 1),
        ('xmlrpc', 2)]

levels = 5


def ex_weights(l): return [int(w) for (t,w) in l]

max_weight = max(ex_weights(tags))
min_weight = min(ex_weights(tags))

thresholds = [math.pow(max_weight - min_weight + 1,float(i) / float(levels))
              for i in range(0,levels)]


def class_from_weight(w,thresholds):
    i = 0
    for t in thresholds:
        i += 1
        if w <= t:
            return i
    return i


for (t,w) in tags:
    c = class_from_weight(w,thresholds)
    print "<span class='cloud-level%d'>%s</span> | " % (c,t)