Python Flask- Delete Cookies

To Delete Cookies

To delete a cookie call set_cookie() method with the name of the cookie and any value and set the max_age argument to 0. Open the main2.py file and add the following code just after the cookie() view function.
flask_app/main2.py
@app.route('/delete-cookie/')
def delete_cookie():
    res = make_response("Cookie Removed")
    res.set_cookie('spark', 'bar', max_age=0)
    return res
Visit http://localhost:5000/delete-cookie/ and you will get the following response:

Now you should have a good understanding of how a cookie works. The practical example of how a cookie can be used to store the user preference is given as follows:
In main2.py add the following code after the delete_cookie() view function.
flask_app/main2.py
@app.route('/book/', methods=['POST', 'GET'])
def book():
    if request.method == 'POST':
        print(request.form)
        res = make_response("")
        res.set_cookie("font",  request.form.get('font'), 60*60*24*10)
        res.headers['location'] = url_for('book)
        return res, 302
    
    return render_template('book.html')
Create a new template book.html with the following code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Book</title>
</head>
<body style="{% if request.cookies.get('font') %}font-family:{{ request.cookies.get('font') }}{% endif %}">
 Select Font Preference: <br>
<form action="" method="post">
    <select name="font" onchange="submit()">
        <option value="">----</option>
        <option value="consolas" {% if request.cookies.get('font') == 'consolas' %}selected{% endif %}>consolas</option>
        <option value="arial" {% if request.cookies.get('font') == 'arial' %}selected{% endif %}>arial</option>
        <option value="verdana" {% if request.cookies.get('font') == 'verdana' %}selected{% endif %}>verdana</option>
    </select>
</form>
 
<h1>Flask is a micro web framework.</h1>
 <p>Flask and python are fun and cool</p>
 <p>Flask is a simple web framework.</p>
 </body>
</html>
The first time user visits http://localhost:5000/book, the page is displayed using the default browser font. When the user changes the font using the drop-down, we submit the form. The if condition request. method == 'POST' becomes true and we set a cookie named font with the value of the selected font that will expire in 15 days, the user is then redirected to http://localhost:5000/book. And the user has displayed the page in the selected font.
In the browser visit, http://localhost:5000/book and you will be displayed a page in a browser default font.
Select the font from the dropdown and you will be displayed the page in the selected font.

Drawbacks of Cookie

You must be aware of its shortcomings, before using the cookies extensively:
  • Cookies are not secure. You shouldn’t use it to store sensitive data like passwords, credit card details because the data stored in cookies are visible to anyone
  • Cookies can be disabled. Most of the browsers give users the option to disable cookies. When cookies are disabled you will not get any warnings or error messages, instead, the response header to set the cookie is simply discarded.
  • Each Cookie can store no more than 4KB of data. In addition to that browsers also imposes limits on the number of cookies a website can set. This limit varies from browser to browser.
  • Cookies are sent every time you request a page from the server. Say you have 20 cookies and each of them store 4KB of data. That means you have an additional payload of 80KB on every request!