Hello, friends, in this video, I will discuss the cookies in a flask application.
What are the cookies?
Cookie is a very important piece of information that the server stores in the clients computer, so
that.
If the client comes into contact with the server again and again the next time.
So in order to process or give a better performance out to the client, the server uses this information,
for example.
The statistics of the frequent usage of those resources by a particular claim, all these.
Information that is stored by thus it is stored by the server, but it is badly stored in the cloud,
so such a piece of information stored by the server in the plant's computer in the form of the text
files.
He's calling it cookie.
What is the purpose?
It is the purpose of these cookies is to remember.
And track the data pertaining to the frequency of use of the world's resources by a particular class.
That is the purpose of the cookies, the request object.
We have seen that the request of the requests that request object attributes is cookies.
One of the attributes is cookies.
It is a dictionary object and it contains what the key value pairs of cookies and the name of the cookie
and the value of cookie.
All these cookies and their values stored by the seller in the client's computer are extracted whenever
the client requests.
I sent the request to the server along with the request, the cookies are also sent, cookies are also
transmitted.
In flask, the cookies are set on the response object.
Went up when the server is required to set a cookie.
It has to get hold of the response object.
So in order to.
Eastern seaboard or create a response object, use a function called let's make response so this response
function gets the response object and with the help of that, you can execute a function called Let's
Set Kookie.
So there are two things, one is for us to find out or get hold of the response object, and then using
that response object use said cookie function to set a particular cookie name and then.
In order to bring back the cookie that is a function for let's get get the matter to the root of the
request, not cookies, attribute will fetch the corresponding value to a particular cookie item in
their dictionary.
So in order to set up matter is said, underscore cookie, but then it is to be executed on the response
object.
What is the key and what is the value?
This is the syntax of the second key function or cooking method.
How do.
Extract or obtain the response object by using make response, make response out of what this template
is to be rendered.
So this is the next thing you are well to.
Your application is going so as from this particular, you are in.
Find out what is the response and use this response object, said Kookie.
So this is how the cookie operation is done.
Similarly.
In order to read back.
That is a function called get so request cookies don't get key, what is the value associated with this
particular thing?
So in order to that, this is a python piece of code which is backed up cookie.
So.
You are only to find this get kookie, which is you can find our map with a function called Let's Get
Cooking so requests, not cookies, get user I.D. The name of the cookies is ready.
It will be stored in the variable called name.
Just say welcome this name.
So the welcome, whatever name was stored in the cookie will be printed.
So these are the two or three different activities are involved in this.
First is to.
Set the cookie up, often the response object and get back the value of the cookie.
So these three, three or four different options, I will do a small exercise in front of you wherein
you will find that the key, the cookie is set.
It will show you that the cookie is set and then I have another piece of code which will read back the
user I.D. cookie to confirm the operation of this, get Metrozoo.
All this I will do with the help of an example demonstration in front of you.
So how to set and read the cookies in a flask application that will slowly with the help of this example.
So what we have in this Python script is simply.
Slash or back slash, you are all which is.
Defined and you're bound to have you function Golez index.
So when this index function will be executed, when it will be executed, when you when you starters
are over and give the you less localhost 5000 less.
So when this index function will be executed, it will render a template called circularly estimate.
This extremophile is stored in the templates folder.
What it has said Cookie will present you simply one text box form in which the user I.D. to be entered.
So let us have a look at this set cookie rotation.
Well, first, before we explain the rest of the pattern for.
So this index page is going to handle this set, Cadart estimate.
This is a simple form in which there is one.
Input was equal to text in which you can enter the name of the text.
Input, his name is equal to a name, and then there is a submit button.
The submit button is clicked, the form that will be posted to the set, Kyuki United Methodist Post.
Whether this is the purpose of this circus to let the user into the user and posted it to slash, said
Wokingham, what is that, said Kookie, you are in how it is defined and what is the view function
associated with that?
So let us come back to this Python script in which they said, Slash said, Cookie, you are a rule
is defined.
Which the root method, the, you know, additionally accepts one more parameter method is equal to
the word is good, but we are using the post method.
The associated review function is said Kookie.
If this condition is true, because anyway, the form is using the post methods we've request darte
method is equal to post this condition is true.
So therefore this.
Block will be executed.
What is there in the form, letter form, dot, dot form and so an attribute of the form.
Whatever the user had entered into, it will be stored in the variable called user.
OK, so now what is the value of this user will be used to set the cookie?
The name of the cookie will be user already and the value will be.
Now in flask, the cookies are.
Attached to the response object, so you have to first obtain the response object by using the function,
make response.
So that.
Brings us to this make response function to be important in the very beginning.
In addition to the flask, object, template, function, request, object, as well as the make response.
This is the make response function.
It this make response function is going to extract the response object out of the template that will
be rendered by this set Cucu function, the template is real, CUCHILLO traditional.
So it will fetch the response object and use the set cooking method associated with the response object
to set the cookie.
This is the name of the cookie and the value is the user.
What is the user the.
You know, user oriented by user on the set, Wookey for.
OK, and then return Harry the response object.
So this is what this said kookie function is supposed to do, it will set the cookie.
Right now, what is there what is there in Israel?
Well, it is a simple hyperlink, which is.
When the hyperlink is clicked, the browser will go to this get cookie.
You know, you are sort of first let us have to have a look at Stephen.
So this is a simple.
From formed around click here to read Kookie.
Which has a SRF is equal to.
Local laws try to get kooky, worried.
Nothing much in this ridiculous world, apart from providing a hyperlink.
Come back to this Python script and we just get kooky.
You are really defined and bound with this new function called Get Kooky.
How to Read the Cookie Kids is very.
Simply get function is available to the request darte, get cookies, not get.
The request object has cookies, Dawngate function.
The cookie that was said was usually the name of the cookie will be the parameter to this great function,
so it will be obtained in the variable called name.
All you have to do is just to render the text onto the browser.
Ultimately, the result will be welcome.
Whatever you ready?
You have to enter in the beginning in the set cookie form.
So this is the.
Mechanism, so slightly complex mechanism.
Setting the cookie and reading it back will now execute this court, first start the server, then open
the browser, open the form, enter the user and read it back.
That sequence will do.
So the Python script is Europea execute to start the server.
And in the browser and.
You this you are in.
Localhost fighter slash opens up, said capillaries will form, you know, you are to introduce usually.
I would say every year.
This login button.
The data will be posted to set Cookie, this cookie will be set and the real cookie dough will be displayed.
Give you a hyperlink to click, the click will lead you to the Slashdot wiki world, which will read
back the copy that was set that.
So this is this example, this is all demonstrates how to set and how to read back the Khorkina.
Simple flask application.