Posts Tagged ‘bleach’

remove tags from HTML

July 13, 2016 Leave a comment

You have an HTML string and you want to remove all the tags from it.

Install the package “bleach” via pip. Then:

>>> import bleach
>>> html = "Her <h1>name</h1> was <i>Jane</i>."
>>> cleaned = bleach.clean(html, tags=[], attributes={}, styles=[], strip=True)
>>> html
'Her <h1>name</h1> was <i>Jane</i>.'
>>> cleaned
'Her name was Jane.'

Tip from here.

Categories: python Tags: , ,

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.