Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask Basics Character Builder Cookies

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 67,995 Points

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

7 Answers

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

Note:

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():
    try:
        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.datetime.now() + 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

data.update(dict(request.forms.items()))

Step 6: Set the cookie

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

Sorry about minor rant.

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

Kenneth is mad here

Great explanations, Shyam, they helped a lot!

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,995 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.

This video might be better if it was broken down into 10 mini lessons. Went a bit over my head.

Thank you, Shyam! That was a great explanation of the video and helped me a lot! Are cookies stored on the browser per domain or per URL? Is that why we need to make sure to use .update()? Because there might be other cookies from other pages that will get overwritten if we don't use update()?