Here, we are going to use a python library readtime


sudo pip install readtime


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 file inside templatetags folder.
  • Create a file inside templatetags folder.

Now, we will create a tag or function inside 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)


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