Python Flask Basics Character Builder Cookies

Eric Hamers
Eric Hamers
10,373 Points

Dude.. you completely lost it in this video... calm down

All the sudden Keneth starts going without even explaining what he is doing... Treehouse, cmon, please....

Chris Freeman
Chris Freeman
Treehouse Moderator 58,986 Points

What do mean by "lost it" and "calm down"? Can you reference time points in the video?

5 Answers

Eric Hamers
Eric Hamers
10,373 Points

Sorry about minor rant.

At 5:15 on, what is saves=data? From that point video evolved way too rapidly...Not much was explained

Chris Freeman
Chris Freeman
Treehouse Moderator 58,986 Points

There is a lot of information covered at that point in the video. From the Flask docs, render_templates takes a template as an argument followed by any number of keyword arguments which will be added to the template context. So, assigning saves makes saves available in the template. The next area is how to handle getting the cookie information, updating it, and then passing the updated cookie back to the browser.

Post back if you need more clarification.

Great explanations, Shyam, they helped a lot!

Shyam Gupta
Shyam Gupta
7,736 Points

I also spent a considerable time to understand what was going on, below explanation should help


json.loads() converts/decodes JSON data into Python dictionary 

json.dumps() converts Python dictionary to JSON

The request.form object is an immutable dictionary.

Cookies can be thought of as key-value pairs that we may or may not receive by default from return visitors. When a user makes choices, we create or update their cookie to reflect these changes, and when a user requests our site, we check to see whether a cookie exists and read as much of the unspecified information from this as possible.

Step1- Define a function to retrieve the cookie if it exists.

def get_saved_data():
        data = json.loads(request.cookies.get(‘cookie_name’) ##loads converts JSON to a Python dictionary.
    except TypeError:
        data = {} #Return empty dictionary if cookie does not exist.
    return data

Step2 - Create a response variable We will wrap a make_response() call around our render_template() or redirect call instead of returning the rendered template directly. This means that our Jinja templates will be rendered, and all the placeholders will be replaced with the correct values, but instead of returning this response directly to our users, we will load it into a variable so that we can make some more additions to it.

response = make_response(redirect(url_for(‘index’)))

Step3: Set cookie expiration (This wasnt part of the lecture, but good to know) Once we have this response object, we will create a datetime object with a value of 365 days (or whatever duration) from today's date

expires = + datetime.timedelta(days=365)

Step 4: Check if a cookie already exists & retrieve it

data = get_saved_data()

Step 5: If the cookie exists, only update the values that have changed


Step 6: Set the cookie

response.set_cookie(‘cookie_name’, json.dumps(data), expires=expires ) 
return response

Kenneth is mad here