Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Flask Framework Cookbook
  • Table Of Contents Toc
Flask Framework Cookbook

Flask Framework Cookbook

By : Shalabh Aggarwal
4.3 (4)
close
close
Flask Framework Cookbook

Flask Framework Cookbook

4.3 (4)
By: Shalabh Aggarwal

Overview of this book

If you are a web developer who wants to learn more about developing applications in Flask and scale them with industry-standard practices, this is the book for you. This book will also act as a handy tool if you are aware of Flask's major extensions and want to make the best use of them. It is assumed that you have knowledge of Python and a basic understanding of Flask. If you are completely new to Flask, reading the book from the first chapter and going forward will help in getting acquainted with Flask as you go ahead.
Table of Contents (14 chapters)
close
close
13
Index

Decorator to handle requests beautifully


Some of us might think that checking whether a request is XHR or not every time kills code readability. To solve this, we have an easy solution. We can just write a simple decorator that will handle this redundant code for us.

Getting ready

In this recipe, we will be writing a decorator. For some of the beginners in Python, this might seem like alien territory. In this case, read http://legacy.python.org/dev/peps/pep-0318/ for a better understanding of decorators.

How to do it…

The following is the decorator method that we have written for this recipe:

from functools import wraps

def template_or_json(template=None):
    """"Return a dict from your view and this will either
    pass it to a template or render json. Use like:
 
    @template_or_json('template.html')
    """
    def decorated(f):
        @wraps(f)
        def decorated_fn(*args, **kwargs):
            ctx = f(*args, **kwargs)
            if request.is_xhr or not template:
              ...
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Flask Framework Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon