23.3. Flask Cookies

When a server executes code to create a cookie, part of the command sets a time limit for how long to save the data. If no time is given in the code, then the cookie gets erased when we quit the browser. Putting our device to sleep or closing the tab does NOT clear cookies!

Recall that Flask runs a server on our machine when we launch an application. This means we can include some Python code to set and access our own cookies.

Let’s see how to do this.

23.3.2. Viewing Cookies

As shown in the video, we can use our browser tools to view the cookies saved for a particular webpage. The specific commands will vary with each browser, but right-clicking on the page is a good thing to try first. Select Inspect Element from the options that pop up, and then open the Storage tab. (For the Chrome browser, the tab is called Application).

View cookies using the browser developer tools. The 'Storage' panel is selected.

View stored cookies by using the browser tools. You might need to look carefully to find the tab you want!


Safari Users: If right-clicking on the page doesn’t show the Inspect Element option, you need to activate the Developer Tools.

Under the Safari menu, select Preferences. Click the Advanced tab, then select the Show Develop menu in menu bar option. Right-clicking should now work as expected!

Note that the cookie data is NOT sent to the webpage as a value. Instead, the browser saves a file on our machine to keep track of the key/value pair. Even if we stop the Flask application, the cookie persists.

The storage panel shows that the cookie remains on our device.

Even when we stop the Flask application, any cookies set by the program remain on our device.

If we change the name of the cookie in our Python code and refresh the page, a new cookie file is saved. However, the old cookie file remains.

The storage panel shows both the old and new cookie files.

Renaming a cookie does not remove the old file.

23.3.4. Resources

This page just scratches the surface on how to use Flask to manage cookies. We won’t need to go deeper for this course. However, for those who would like to explore further, here are a couple of good places to start:

  1. Flask Cookies

  2. OverIQ

23.3.5. Check Your Understanding


Where is cookie data stored?

  1. In a file on our device.
  2. In the webpage open in our browser.
  3. As a value assigned to a variable.
  4. On a web server.


Refreshing a webpage erases cookies.

  1. True
  2. False


Quitting the browser application erases cookies.

  1. Always
  2. Never
  3. Sometimes