Skip to Content
Flask Framework Cookbook - Second Edition
book

Flask Framework Cookbook - Second Edition

by Shalabh Aggarwal
July 2019
Beginner to intermediate
302 pages
9h 38m
English
Packt Publishing
Content preview from Flask Framework Cookbook - Second Edition

How to do it...

First, let's create a filter to format a currency based on the current local language. Add the following code to my_app/__init__.py:

import ccy 
from flask import request 
 
@app.template_filter('format_currency') 
def format_currency_filter(amount): 
    currency_code = ccy.countryccy(request.accept_languages.best[-       2:]) 
    return '{0} {1}'.format(currency_code, amount) 
request.accept_languages might not work in cases where a request does not have the ACCEPT-LANGUAGES header.

The preceding snippet will require the installation of a new package, ccy, as follows:

$ pip3 install ccy

The filter created in this example takes the language that best matches the current browser locale (which, in my case, is en-US), takes the last two characters ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Writing a Web Application with Flask

Writing a Web Application with Flask

Doug Farrell
Mastering Flask

Mastering Flask

Jack Stouffer

Publisher Resources

ISBN: 9781789951295Supplemental Content