Posts Tagged ‘linkify’

Flask: linkify a text

July 12, 2015 Leave a comment

I have a text that I present in a Flask application. The text can contain URLs, and I would like to linkify them, i.e. make them clickable links. Example:


visit for...


visit <a href=""></a> for...

Before rolling out an own solution, it’s a good idea to check if there is a package for this problem. There is :), and it’s called bleach. Its usage couldn’t be simpler:

>>> import bleach
>>> bleach.linkify('an url')
u'an <a href="" rel="nofollow"></a> url

Flask integration
In your main file (that calls add the following filter:

import bleach

def linkify(s):
    return bleach.linkify(s)

Then use it in your jinja2 templates:

Description: {{ entry.description|linkify|safe }}

Warning! Apply the “safe” filter only if you trust the origin of the text you want to present in a linkified format.