Read Time
NotesIt’s a nice touch to include an estimate of the time a blog post takes to read. The usual estimate is based on an average reading speed of around 265 words per minute (the same figure Medium uses), which is simple enough to calculate yourself. Add a small function to web/views/blog_views.py:
web/views/blog_views.pypython
WORDS_PER_MINUTE = 265
def read_time_minutes(text: str) -> int:
return max(1, round(len(text.split()) / WORDS_PER_MINUTE))max(1, ...) makes sure even a very short post says “1 min read” rather than “0 min read”.
note
Earlier versions of this guide used the readtime package. It works, but it hasn’t had a release since 2023 and pulls in five other packages (including lxml) for what is one line of arithmetic.
Update the blogpost_get_view function to calculate the read time from the content field:
web/views/blog_views.pypython
@router.get('/blog/{id}', response_class=HTMLResponse, status_code=status.HTTP_200_OK)
def blogpost_get_view(*, id: uuid.UUID, request: Request, session: Session = Depends(get_session_db)):
post = get_blogpost_by_id_api(id=id, session=session)
markdown_content = nh3.clean(markdown.markdown(post.content))
context = {
"page": "blog",
"post": post,
"markdown": markdown_content,
"blogpost_readtime": read_time_minutes(post.content), # NEW
}
return templates.TemplateResponse(request, 'blogpost.html', {'context': context})And update the blogpost.html template:
web/templates/blogpost.htmljinja
<article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<small>{{ context.blogpost_readtime }} min read</small>
{{ context.markdown|safe }}
</div>
</div>
</div>
</article>