Archive

Archive for September, 2015

get the title of a web page

September 8, 2015 Leave a comment

Problem
You need the title of a web page.

Solution

from bs4 import BeautifulSoup

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

I found the solution here.

Advertisements
Categories: python Tags: , , ,

python-markdown: add support for strikethrough

September 7, 2015 1 comment

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

Solution
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))

Input:

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

Output:
TODO list
* strikethrough in Python-Markdown

Categories: python Tags: ,

[flask] render a template and jump to an anchor

September 6, 2015 Leave a comment

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

Solution

Here is a route:

@app.route('/about/')
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: , ,