1 00:00:00,560 --> 00:00:09,330 Hello, friends, in this video, I will discuss the cookies in a flask application. 2 00:00:09,350 --> 00:00:10,400 What are the cookies? 3 00:00:15,040 --> 00:00:25,030 Cookie is a very important piece of information that the server stores in the clients computer, so 4 00:00:25,030 --> 00:00:25,360 that. 5 00:00:26,320 --> 00:00:31,750 If the client comes into contact with the server again and again the next time. 6 00:00:33,230 --> 00:00:43,220 So in order to process or give a better performance out to the client, the server uses this information, 7 00:00:43,730 --> 00:00:44,600 for example. 8 00:00:45,770 --> 00:00:55,010 The statistics of the frequent usage of those resources by a particular claim, all these. 9 00:00:56,810 --> 00:01:03,290 Information that is stored by thus it is stored by the server, but it is badly stored in the cloud, 10 00:01:03,860 --> 00:01:10,190 so such a piece of information stored by the server in the plant's computer in the form of the text 11 00:01:10,190 --> 00:01:10,700 files. 12 00:01:11,480 --> 00:01:12,910 He's calling it cookie. 13 00:01:13,670 --> 00:01:14,580 What is the purpose? 14 00:01:14,930 --> 00:01:18,080 It is the purpose of these cookies is to remember. 15 00:01:19,810 --> 00:01:29,000 And track the data pertaining to the frequency of use of the world's resources by a particular class. 16 00:01:29,410 --> 00:01:33,960 That is the purpose of the cookies, the request object. 17 00:01:34,750 --> 00:01:40,390 We have seen that the request of the requests that request object attributes is cookies. 18 00:01:40,690 --> 00:01:42,760 One of the attributes is cookies. 19 00:01:43,330 --> 00:01:51,970 It is a dictionary object and it contains what the key value pairs of cookies and the name of the cookie 20 00:01:51,970 --> 00:01:53,200 and the value of cookie. 21 00:01:53,530 --> 00:02:01,720 All these cookies and their values stored by the seller in the client's computer are extracted whenever 22 00:02:01,720 --> 00:02:04,000 the client requests. 23 00:02:05,270 --> 00:02:11,060 I sent the request to the server along with the request, the cookies are also sent, cookies are also 24 00:02:11,060 --> 00:02:11,780 transmitted. 25 00:02:13,700 --> 00:02:17,840 In flask, the cookies are set on the response object. 26 00:02:19,600 --> 00:02:22,760 Went up when the server is required to set a cookie. 27 00:02:23,440 --> 00:02:27,520 It has to get hold of the response object. 28 00:02:28,300 --> 00:02:29,350 So in order to. 29 00:02:30,300 --> 00:02:39,030 Eastern seaboard or create a response object, use a function called let's make response so this response 30 00:02:39,030 --> 00:02:46,410 function gets the response object and with the help of that, you can execute a function called Let's 31 00:02:46,410 --> 00:02:47,160 Set Kookie. 32 00:02:48,940 --> 00:02:58,750 So there are two things, one is for us to find out or get hold of the response object, and then using 33 00:02:58,750 --> 00:03:05,680 that response object use said cookie function to set a particular cookie name and then. 34 00:03:06,920 --> 00:03:12,380 In order to bring back the cookie that is a function for let's get get the matter to the root of the 35 00:03:12,380 --> 00:03:20,480 request, not cookies, attribute will fetch the corresponding value to a particular cookie item in 36 00:03:20,480 --> 00:03:21,080 their dictionary. 37 00:03:26,980 --> 00:03:34,390 So in order to set up matter is said, underscore cookie, but then it is to be executed on the response 38 00:03:34,390 --> 00:03:34,780 object. 39 00:03:35,820 --> 00:03:37,290 What is the key and what is the value? 40 00:03:37,320 --> 00:03:41,360 This is the syntax of the second key function or cooking method. 41 00:03:42,990 --> 00:03:43,530 How do. 42 00:03:45,000 --> 00:03:57,030 Extract or obtain the response object by using make response, make response out of what this template 43 00:03:57,030 --> 00:03:57,780 is to be rendered. 44 00:03:58,050 --> 00:04:00,990 So this is the next thing you are well to. 45 00:04:01,350 --> 00:04:07,810 Your application is going so as from this particular, you are in. 46 00:04:08,670 --> 00:04:13,200 Find out what is the response and use this response object, said Kookie. 47 00:04:14,230 --> 00:04:17,840 So this is how the cookie operation is done. 48 00:04:18,670 --> 00:04:19,390 Similarly. 49 00:04:20,770 --> 00:04:21,880 In order to read back. 50 00:04:23,110 --> 00:04:30,880 That is a function called get so request cookies don't get key, what is the value associated with this 51 00:04:30,880 --> 00:04:31,460 particular thing? 52 00:04:31,930 --> 00:04:37,290 So in order to that, this is a python piece of code which is backed up cookie. 53 00:04:37,690 --> 00:04:38,140 So. 54 00:04:39,110 --> 00:04:45,980 You are only to find this get kookie, which is you can find our map with a function called Let's Get 55 00:04:45,980 --> 00:04:52,150 Cooking so requests, not cookies, get user I.D. The name of the cookies is ready. 56 00:04:52,430 --> 00:04:54,860 It will be stored in the variable called name. 57 00:04:55,280 --> 00:04:56,890 Just say welcome this name. 58 00:04:56,900 --> 00:05:00,180 So the welcome, whatever name was stored in the cookie will be printed. 59 00:05:00,740 --> 00:05:04,790 So these are the two or three different activities are involved in this. 60 00:05:05,480 --> 00:05:06,350 First is to. 61 00:05:07,730 --> 00:05:14,880 Set the cookie up, often the response object and get back the value of the cookie. 62 00:05:15,080 --> 00:05:21,630 So these three, three or four different options, I will do a small exercise in front of you wherein 63 00:05:22,190 --> 00:05:25,790 you will find that the key, the cookie is set. 64 00:05:27,180 --> 00:05:33,090 It will show you that the cookie is set and then I have another piece of code which will read back the 65 00:05:33,930 --> 00:05:38,650 user I.D. cookie to confirm the operation of this, get Metrozoo. 66 00:05:39,000 --> 00:05:42,720 All this I will do with the help of an example demonstration in front of you. 67 00:05:43,500 --> 00:05:52,320 So how to set and read the cookies in a flask application that will slowly with the help of this example. 68 00:05:54,350 --> 00:05:59,410 So what we have in this Python script is simply. 69 00:06:01,250 --> 00:06:03,770 Slash or back slash, you are all which is. 70 00:06:05,580 --> 00:06:11,220 Defined and you're bound to have you function Golez index. 71 00:06:12,220 --> 00:06:18,070 So when this index function will be executed, when it will be executed, when you when you starters 72 00:06:18,070 --> 00:06:21,490 are over and give the you less localhost 5000 less. 73 00:06:22,360 --> 00:06:28,280 So when this index function will be executed, it will render a template called circularly estimate. 74 00:06:28,310 --> 00:06:32,820 This extremophile is stored in the templates folder. 75 00:06:32,890 --> 00:06:43,390 What it has said Cookie will present you simply one text box form in which the user I.D. to be entered. 76 00:06:44,050 --> 00:06:47,470 So let us have a look at this set cookie rotation. 77 00:06:47,480 --> 00:06:52,120 Well, first, before we explain the rest of the pattern for. 78 00:06:53,000 --> 00:06:57,700 So this index page is going to handle this set, Cadart estimate. 79 00:07:01,660 --> 00:07:05,520 This is a simple form in which there is one. 80 00:07:06,620 --> 00:07:10,640 Input was equal to text in which you can enter the name of the text. 81 00:07:11,860 --> 00:07:16,360 Input, his name is equal to a name, and then there is a submit button. 82 00:07:17,420 --> 00:07:27,590 The submit button is clicked, the form that will be posted to the set, Kyuki United Methodist Post. 83 00:07:28,630 --> 00:07:37,970 Whether this is the purpose of this circus to let the user into the user and posted it to slash, said 84 00:07:37,990 --> 00:07:44,590 Wokingham, what is that, said Kookie, you are in how it is defined and what is the view function 85 00:07:44,590 --> 00:07:46,240 associated with that? 86 00:07:49,960 --> 00:07:57,940 So let us come back to this Python script in which they said, Slash said, Cookie, you are a rule 87 00:07:57,940 --> 00:07:58,630 is defined. 88 00:08:00,370 --> 00:08:06,490 Which the root method, the, you know, additionally accepts one more parameter method is equal to 89 00:08:07,030 --> 00:08:09,880 the word is good, but we are using the post method. 90 00:08:10,930 --> 00:08:13,360 The associated review function is said Kookie. 91 00:08:14,490 --> 00:08:20,630 If this condition is true, because anyway, the form is using the post methods we've request darte 92 00:08:20,640 --> 00:08:23,880 method is equal to post this condition is true. 93 00:08:23,890 --> 00:08:24,810 So therefore this. 94 00:08:26,180 --> 00:08:27,430 Block will be executed. 95 00:08:30,340 --> 00:08:37,270 What is there in the form, letter form, dot, dot form and so an attribute of the form. 96 00:08:38,800 --> 00:08:43,270 Whatever the user had entered into, it will be stored in the variable called user. 97 00:08:45,100 --> 00:08:50,260 OK, so now what is the value of this user will be used to set the cookie? 98 00:08:51,350 --> 00:08:54,340 The name of the cookie will be user already and the value will be. 99 00:08:55,800 --> 00:09:00,420 Now in flask, the cookies are. 100 00:09:02,100 --> 00:09:10,230 Attached to the response object, so you have to first obtain the response object by using the function, 101 00:09:10,230 --> 00:09:11,190 make response. 102 00:09:12,300 --> 00:09:12,870 So that. 103 00:09:14,030 --> 00:09:20,930 Brings us to this make response function to be important in the very beginning. 104 00:09:21,110 --> 00:09:28,220 In addition to the flask, object, template, function, request, object, as well as the make response. 105 00:09:28,730 --> 00:09:30,380 This is the make response function. 106 00:09:34,590 --> 00:09:42,030 It this make response function is going to extract the response object out of the template that will 107 00:09:42,030 --> 00:09:47,360 be rendered by this set Cucu function, the template is real, CUCHILLO traditional. 108 00:09:48,820 --> 00:09:55,720 So it will fetch the response object and use the set cooking method associated with the response object 109 00:09:55,720 --> 00:09:58,390 to set the cookie. 110 00:09:59,950 --> 00:10:02,530 This is the name of the cookie and the value is the user. 111 00:10:02,530 --> 00:10:04,240 What is the user the. 112 00:10:05,980 --> 00:10:10,080 You know, user oriented by user on the set, Wookey for. 113 00:10:11,940 --> 00:10:15,460 OK, and then return Harry the response object. 114 00:10:15,470 --> 00:10:22,560 So this is what this said kookie function is supposed to do, it will set the cookie. 115 00:10:24,110 --> 00:10:27,530 Right now, what is there what is there in Israel? 116 00:10:28,370 --> 00:10:31,700 Well, it is a simple hyperlink, which is. 117 00:10:32,790 --> 00:10:37,740 When the hyperlink is clicked, the browser will go to this get cookie. 118 00:10:39,320 --> 00:10:44,850 You know, you are sort of first let us have to have a look at Stephen. 119 00:10:48,210 --> 00:10:49,650 So this is a simple. 120 00:10:51,510 --> 00:10:54,030 From formed around click here to read Kookie. 121 00:10:55,540 --> 00:10:58,420 Which has a SRF is equal to. 122 00:10:59,500 --> 00:11:01,930 Local laws try to get kooky, worried. 123 00:11:04,480 --> 00:11:09,580 Nothing much in this ridiculous world, apart from providing a hyperlink. 124 00:11:12,280 --> 00:11:15,470 Come back to this Python script and we just get kooky. 125 00:11:15,700 --> 00:11:21,580 You are really defined and bound with this new function called Get Kooky. 126 00:11:22,300 --> 00:11:24,100 How to Read the Cookie Kids is very. 127 00:11:26,690 --> 00:11:33,200 Simply get function is available to the request darte, get cookies, not get. 128 00:11:34,150 --> 00:11:37,990 The request object has cookies, Dawngate function. 129 00:11:39,040 --> 00:11:45,610 The cookie that was said was usually the name of the cookie will be the parameter to this great function, 130 00:11:46,060 --> 00:11:48,790 so it will be obtained in the variable called name. 131 00:11:49,060 --> 00:11:54,460 All you have to do is just to render the text onto the browser. 132 00:11:54,940 --> 00:11:56,890 Ultimately, the result will be welcome. 133 00:11:57,460 --> 00:11:58,400 Whatever you ready? 134 00:11:58,420 --> 00:12:02,050 You have to enter in the beginning in the set cookie form. 135 00:12:03,630 --> 00:12:04,500 So this is the. 136 00:12:05,570 --> 00:12:08,320 Mechanism, so slightly complex mechanism. 137 00:12:09,860 --> 00:12:18,020 Setting the cookie and reading it back will now execute this court, first start the server, then open 138 00:12:18,020 --> 00:12:22,460 the browser, open the form, enter the user and read it back. 139 00:12:23,690 --> 00:12:26,270 That sequence will do. 140 00:12:29,380 --> 00:12:34,630 So the Python script is Europea execute to start the server. 141 00:12:41,220 --> 00:12:43,320 And in the browser and. 142 00:12:45,010 --> 00:12:46,150 You this you are in. 143 00:12:47,740 --> 00:12:55,430 Localhost fighter slash opens up, said capillaries will form, you know, you are to introduce usually. 144 00:12:56,390 --> 00:12:57,580 I would say every year. 145 00:12:59,870 --> 00:13:01,640 This login button. 146 00:13:02,840 --> 00:13:09,680 The data will be posted to set Cookie, this cookie will be set and the real cookie dough will be displayed. 147 00:13:10,680 --> 00:13:18,090 Give you a hyperlink to click, the click will lead you to the Slashdot wiki world, which will read 148 00:13:18,090 --> 00:13:20,910 back the copy that was set that. 149 00:13:22,600 --> 00:13:29,770 So this is this example, this is all demonstrates how to set and how to read back the Khorkina. 150 00:13:31,100 --> 00:13:32,530 Simple flask application.