Author Archive

Anaconda: problem with the readline module under Manjaro

October 5, 2015 Leave a comment

I tried the Anaconda Python distribution under Manjaro, but there was a problem with the “readline” module:

>>> import readline
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot open shared object file: No such file or directory

As root:

# cd /usr/lib
# ln -s

It may be an ugly hack, but it solved the issue for me.

Categories: python Tags: , ,

get the title of a web page

September 8, 2015 Leave a comment

You need the title of a web page.


from bs4 import BeautifulSoup

soup = BeautifulSoup(html)
print soup.title.string

I found the solution here.

Categories: python Tags: , , ,

python-markdown: add support for strikethrough

September 7, 2015 Leave a comment

In a webapp of mine I use markdown with the excellent Python-Markdown package. However, it doesn’t support strikethrough by default.

The good news is that you can add 3rd-party extensions to Python-Markdown. With the extension “mdx_del_ins” you can use the <del> and <ins> tags.

Here is a Python function that converts markdown to HTML:

import bleach
from markdown import markdown

def md_to_html(md):
    Markdown to HTML conversion.
    allowed_tags = ['a', 'abbr', 'acronym', 'b',
                    'blockquote', 'code', 'em',
                    'i', 'li', 'ol', 'pre', 'strong',
                    'ul', 'h1', 'h2', 'h3', 'p', 'br', 'ins', 'del']
    return bleach.linkify(bleach.clean(
        markdown(md, output_format='html', extensions=['nl2br', 'del_ins']),
        tags=allowed_tags, strip=True))


TODO list
* ~~strikethrough in Python-Markdown~~

TODO list
* strikethrough in Python-Markdown

Categories: python Tags: ,

[flask] render a template and jump to an anchor

September 6, 2015 Leave a comment

You render a page but you want to jump to an anchor on the rendered page.


Here is a route:

def fn_about():
    return render_template('about.html')

In the view add this (assuming you have jQuery):

<script> $(function(){ window.location.hash = "jump_here"; }); </script>

It’ll run once the HTML is loaded. Found it here.

Categories: flask, python Tags: , ,

create a virtual environment easily

August 30, 2015 Leave a comment

I prefer to put my larger projects in a virtual environment (there are people who put every project in a virt. env. …). I keep my projects in Dropbox (thus they are available on all my machines), but the virt. env.’s are kept outside Dropbox since they can grow quite big (and they are easily reproducible).

For creating virt. env.’s, I use virtualenvwrapper, which (by default) puts virt. env.’s in the folder ~/.virtualenvs. Say I have a project in Dropbox, and I want to create a virt. env. for it. How to do it easily?

First, you need to know if your project is written in Python 2 or Python 3. Then, you need to use the mkvirtualenv command but I always need to look up its syntax. Solution: in the root folder of my project I want a script that will create a virt. env. for the project. Here is the script:

#!/usr/bin/env bash


# which Python version to use in the created virt. env. (2 or 3)

source `which` && mkvirtualenv -p `which python${PYTHON_VER}` $f

Just set PYTHON_VER and launch the script. It will figure out the name of the current folder and create a virt. env. with this name. For instance, if you have your project in ~/projects/stuff, then the virt. env. will be created in the folder ~/.virtualenvs/stuff.

Related links

Categories: bash, python Tags: ,

[flask] validate a URL

August 23, 2015 Leave a comment

In a Flask application I wanted to verify if a user-given URL is valid.

I found a simple validator package for that called validators (see it on GitHub).

Sample usage:

$ pip install validators
$ python
>>> import validators
>>> url = ""
>>> validators.url(url)
>>> url = "http://index.h/"
>>> validators.url(url)
ValidationFailure(func=url, args={'value': 'http://index.h/', 'require_tld': True})

The ValidationFailure class implements the __bool__ method, so you can easily check if validation failed:

if not validators.url(url):
    flash("Error: you must provide a valid URL!")
Categories: flask, python Tags: ,

[flask] show render time on a page

August 22, 2015 Leave a comment

For debugging purposes, I wanted to see the rendering time of each page.

I found a solution here. Here are the links:

Note that Flask-DebugToolbar is very useful but it really slowed down my application. I even switched it off and I only use it when I want to debug something. When the issue is solved I switch it off again.


Get every new post delivered to your Inbox.

Join 99 other followers