lowmanio.co.uk

Blog tags in Python

Wed, 29 Jul 2009 05:10PM

Category: Programming

Written by Sarah | No comments

Creating the blog tags for this website was a bit tricky because I wasn't sure how to make the tags have different sizes according to their significance. I started off with 5 spans and ordered the tags in terms of frequency and divided them equally into the spans. However tags are not evenly distributed, so instead I calculated the normalized weight of the tag according to the others, and made the font size a percentage of that.

In the model:

@staticmethod
def generateSpans():
        counts = session.query(Tag, 
                func.count(Tag.id)).join(blog_tags).group_by(Tag).all()
        
        smallest = min(count for (tag, count) in counts)
        scale = max(count for (tag, count) in counts) - smallest

        scaled = []
        for tag, count in counts:
                if scale != 0:
                        scaled.append((tag, (count - smallest)/float(scale)))
                else:
                        scaled.append((tag, 1))

        return scaled

In the view:

<p>
% for tag, weight in tags:
        <span style="font-size: ${int(70 + (80 * weight))|h}%"><a href=
        "${urls.build('tag.entries', dict(tag=tag.link))|h}">${tag.name|h}</a> </span>
% endfor
</p>

It looks pretty good when there are at least 3 different weightings as you can see to the right.

Tagged with: Python, tags

Comments

No comments.

Add a comment

captcha