Python中的HTML标签云

6 投票
1 回答
2858 浏览
提问于 2025-04-16 00:50

我在找一个简单的库,这个库可以接收一组物品和对应的数值,然后生成一个标签云作为输出。

最好这个库是用Python写的。

1 个回答

5

在你的css文件中定义字体大小。使用来自

size-0{
   font-size: 11px;
}

size-1{
   font-size: 12px;
}

等的类,直到你需要的字体大小为止。

然后只需使用这个代码片段:

CSS_SIZES = range(1, 7) # 1,2...6 for use in your css-file size-1, size-2, etc.

TAGS = {
    'python' : 28059,
    'html' : 19160,
    'tag-cloud' : 40,
}

MAX = max(TAGS.values()) # Needed to calculate the steps for the font-size

STEP = MAX / len(CSS_SIZES)

for tag, count in TAGS.items():
    css = count / STEP        
    print '<a href="%s" class="size-%s">%s</a>' % (tag, css, tag),

就这样。无需使用任何库哦;-)

撰写回答