ReadTime

Here, we are going to use a python library readtime

Installation

sudo pip install readtime

Usage

import readtime

To calculate read time of Text:

str(readtime.of_text('The shortest blog post in the world!'))

# Output
u'1 min read

To calculate read time of Markdown:

readtime.of_markdown('This is **Markdown**')

# Output
1 min read

To calculate read time of HTML:

readtime.of_html('<h1>This is HTML</h1>')

# Output
1 min read

Integration with django templates

  • Create a folder `templatetags` inside your django app.
  • Create __init__.py file inside templatetags folder.
  • Create a example.py file inside templatetags folder.

Now, we will create a tag or function inside example.py file which will calculate the time taken by an article or post

from django import template
import readtime

register = template.Library()

def read(html):
    return readtime.of_html(html)

register.filter('readtime',read)

Now in your template.html, load the templatetag we just created at the top of the page.

{% load example % }

Now use your function you registerd, anywhere in your template where you want to calculate readtime

{{post.body|readtime}}