def prettify_html(element, depth=0):
    """
    Turn an HTML element into a nicely formatted string. It is based on
    `django.test.html.Element.__str__` method and add indentation.
    """
    indent = '  ' * depth
    if isinstance(element, str):
        return '%s%s' % (indent, element)
    output = '%s<%s' % (indent, element.name)
    for key, value in element.attributes:
        if value:
            output += ' %s="%s"' % (key, value)
        else:
            output += ' %s' % key
    output += '>'
    if len(element.children) == 1 and isinstance(element.children[0], str):
        output += '%s</%s>' % (element.children[0], element.name)
    elif element.children:
        depth += 1
        for child in element.children:
            output += '\n%s' % (prettify_html(child, depth))
        output += '\n%s</%s>' % (indent, element.name)
    return output