1 00:00:00,560 --> 00:00:08,990 Hello, friends, in this video, we shall see the use of the session object in a flask application. 2 00:00:13,580 --> 00:00:22,670 Sessions are a little different from cookies where the cookies are stored in the client's computer. 3 00:00:23,180 --> 00:00:26,110 The session is stored on the server itself. 4 00:00:26,300 --> 00:00:30,120 So that is the main difference between the cookies and the sessions. 5 00:00:31,370 --> 00:00:33,400 What is the session session? 6 00:00:33,410 --> 00:00:43,430 Is the time interval between the time the user has logged into a Web application to the time the user 7 00:00:43,430 --> 00:00:45,350 is logged out of the application. 8 00:00:45,650 --> 00:00:51,770 So the time interval between these two instances, logging in to an application and logging out of the 9 00:00:51,770 --> 00:00:54,720 application, the time duration is called into session. 10 00:00:55,100 --> 00:01:06,440 So if you want a certain data to be persisted throughout this time interval, then certain session variables 11 00:01:06,440 --> 00:01:14,150 are stored which can be used across the different you else that the user might be visiting during the 12 00:01:14,150 --> 00:01:15,780 course of that session. 13 00:01:16,250 --> 00:01:25,460 So the data, which is not needed to be held across the session is that is those variables. 14 00:01:25,460 --> 00:01:30,350 Those are stored in a temporary directly in the server. 15 00:01:31,750 --> 00:01:41,320 When a client logs into or visits a particular Web application server, then each server, each client 16 00:01:41,320 --> 00:01:44,020 or each session is given a unique. 17 00:01:45,430 --> 00:01:48,850 So that particular decision is identified by this. 18 00:01:49,300 --> 00:02:01,120 Every session data, the variables are stored on the top of the cookies and the and the data is then 19 00:02:01,360 --> 00:02:07,930 encrypted with the help of a certain encryption key, which is called secret key. 20 00:02:08,530 --> 00:02:10,090 So therefore, what is necessary? 21 00:02:10,600 --> 00:02:17,650 If you want a certain Web application to hold certain session data, then the application configuration 22 00:02:17,650 --> 00:02:21,730 must have a secret key notified or stored. 23 00:02:23,620 --> 00:02:24,760 What is the decision object? 24 00:02:24,760 --> 00:02:26,350 Just like a cookie object. 25 00:02:27,370 --> 00:02:32,200 An object is also a dictionary object which contains the key value pairs. 26 00:02:32,710 --> 00:02:37,770 Key is the name of the station variable and the data is the associated value. 27 00:02:38,230 --> 00:02:40,480 So it is also a dictionary object. 28 00:02:46,470 --> 00:02:54,060 So first and foremost, you have to set the secret key app provided app is the name of your application 29 00:02:54,060 --> 00:02:58,080 object dark secret underscore key is equal to now. 30 00:02:58,170 --> 00:03:05,970 You find that a certain string is provided or given the value to this secret. 31 00:03:06,390 --> 00:03:13,470 Remember that this must be a very random and such a string which is not easily recognized or which is 32 00:03:13,470 --> 00:03:15,310 not easily, easily reproducible. 33 00:03:15,690 --> 00:03:25,710 So such a random string is used as secret key of the application in order to set the variable on your 34 00:03:25,710 --> 00:03:29,380 to do that sation, the name of the label that you want to use. 35 00:03:29,580 --> 00:03:31,930 Put it into the bracket is equal to the value. 36 00:03:32,700 --> 00:03:34,880 So this will set the value. 37 00:03:35,250 --> 00:03:43,140 And in order to remove the third transition variable, you just use the function called pop station 38 00:03:43,170 --> 00:03:43,850 or pop. 39 00:03:44,350 --> 00:03:52,130 Give the name of the variable that Flast will take, the values you put into position object and they 40 00:03:52,140 --> 00:03:55,380 will be serialized in the form of cookies. 41 00:03:56,860 --> 00:04:04,180 Some you know, sometimes what may happen that the sessions are not persisted throughout the session, 42 00:04:05,500 --> 00:04:12,970 even if the cookies are enable the sessions, the session variable does not contain or hold to the values. 43 00:04:13,300 --> 00:04:14,440 So what may be the reason? 44 00:04:14,440 --> 00:04:18,940 The reason may be that your Web browser may not be supporting the length of the cookie. 45 00:04:19,360 --> 00:04:27,250 That is all you have to verify that the site supported by the Web browser is matching with the size 46 00:04:27,250 --> 00:04:29,010 of the cookies in your application. 47 00:04:29,230 --> 00:04:33,550 If not, then there will be certain exception that may be generated. 48 00:04:34,030 --> 00:04:42,400 So these are all the different activities that you will have to verify with the help of an example. 49 00:04:42,430 --> 00:04:50,340 So set an example which will, you know, statistician, create a variable and read back to the station 50 00:04:50,350 --> 00:04:57,560 variable that I will demonstrate to you with the help of a python example running a flask application. 51 00:04:58,630 --> 00:05:01,750 So let us now try to understand how. 52 00:05:02,980 --> 00:05:11,200 The session is created, how the session variables are defined, even if needed, how the session variables 53 00:05:11,200 --> 00:05:18,160 are removed and how those variables remains persistent throughout the entire session. 54 00:05:18,190 --> 00:05:18,970 What is decision? 55 00:05:19,390 --> 00:05:28,320 The time interval for which the user is logged into a particular Web application and logged out of it? 56 00:05:30,130 --> 00:05:38,850 OK, so for this exercise, different resources that are needed from our last module are important first. 57 00:05:38,870 --> 00:05:42,400 So this long list of import. 58 00:05:43,840 --> 00:05:50,710 You have to import the flask object, the session object, the request object and the functions redirect 59 00:05:51,040 --> 00:05:57,040 you are for and render a template to, these are our requirements which we have imported. 60 00:05:58,080 --> 00:06:01,470 Then, as usual, cleared the last object. 61 00:06:03,450 --> 00:06:07,020 ABC called Flask Doubleheaders codename. 62 00:06:09,230 --> 00:06:12,530 This will create the flask application object. 63 00:06:13,740 --> 00:06:21,780 The mandatory thing is to set up a secret key of this plastic object, a secret key, you can give any 64 00:06:21,960 --> 00:06:22,650 string here. 65 00:06:24,140 --> 00:06:25,100 The rest of the. 66 00:06:27,380 --> 00:06:27,950 Script. 67 00:06:29,940 --> 00:06:33,900 Has three, you are really rules defined. 68 00:06:33,930 --> 00:06:34,680 One is the. 69 00:06:35,710 --> 00:06:42,070 Index, the other is a slash login and slash logout. 70 00:06:43,160 --> 00:06:45,680 So to begin with, when the server starts. 71 00:06:47,320 --> 00:06:53,230 And you give the localhost lost 5000, slash, this index will function, will be executed. 72 00:06:54,250 --> 00:06:55,480 So, as you will expect. 73 00:06:56,390 --> 00:07:04,940 In the very first place, as soon as you log into the server by this flash, you are well, there will 74 00:07:04,940 --> 00:07:06,080 be no username. 75 00:07:07,820 --> 00:07:10,680 That will be in the session object. 76 00:07:10,700 --> 00:07:11,770 What is the object? 77 00:07:11,780 --> 00:07:17,930 Incidently, it is a it is a dictionary of all the variables and its associated value. 78 00:07:17,960 --> 00:07:20,990 So you are you have just started the server. 79 00:07:20,990 --> 00:07:26,530 So naturally, there will be no you there will be no session variable by the name username. 80 00:07:26,540 --> 00:07:27,960 So this condition will be false. 81 00:07:28,370 --> 00:07:31,020 So this will not take place. 82 00:07:31,850 --> 00:07:33,200 So what what will happen? 83 00:07:34,960 --> 00:07:36,070 It will come into this. 84 00:07:38,250 --> 00:07:45,570 And the message that will be displayed are rendered on your brother's surface will be you are not logged 85 00:07:45,570 --> 00:07:45,780 in. 86 00:07:46,740 --> 00:07:52,860 And it will give you a hyperlink to this slash login you are in. 87 00:07:54,580 --> 00:07:57,350 And we'll ask you to click here to login. 88 00:07:57,370 --> 00:08:00,150 So if you click on this login, what will happen? 89 00:08:02,020 --> 00:08:02,680 This. 90 00:08:03,620 --> 00:08:06,860 Browser will be redirected towards this, you are logging. 91 00:08:06,890 --> 00:08:09,670 This is the second rule that has been defined here. 92 00:08:10,890 --> 00:08:14,040 And additionally, the road to function. 93 00:08:16,490 --> 00:08:20,750 Taking two arguments, as usual, the usual and the Metters. 94 00:08:22,190 --> 00:08:23,180 Get that post. 95 00:08:26,090 --> 00:08:27,770 OK, so for the first time in. 96 00:08:29,160 --> 00:08:32,010 The user clicks this click here to login. 97 00:08:33,330 --> 00:08:37,590 And for the first time, the Log-in, you are really stressed. 98 00:08:38,850 --> 00:08:42,330 Naturally, the request not matter will not be equal to post. 99 00:08:43,350 --> 00:08:44,010 It will be. 100 00:08:45,830 --> 00:08:49,010 It will be a great method, so therefore this. 101 00:08:51,010 --> 00:08:54,070 Rendered template session, that e-mail will. 102 00:08:56,720 --> 00:08:58,460 I mean, this action will take place. 103 00:08:58,480 --> 00:09:06,650 So what will happen, the browser will render that the template or the Web page for legislation or e-mail, 104 00:09:06,650 --> 00:09:08,030 which you will store into. 105 00:09:10,230 --> 00:09:12,310 The templates folder of your Web application. 106 00:09:12,690 --> 00:09:17,360 So at this juncture, we shall have a look at what is the intersession that Stephen. 107 00:09:20,160 --> 00:09:28,170 So this is the session that is the core of this session, not Estherville, in which basically there 108 00:09:28,170 --> 00:09:29,850 is a small form. 109 00:09:30,890 --> 00:09:36,530 In which there is an input type equal to text in which you can type the. 110 00:09:37,760 --> 00:09:41,540 Text, which will be stored in the attribute called name. 111 00:09:42,510 --> 00:09:49,170 And then the submit button, so if you click on the submit button, the data will be posted back to 112 00:09:49,170 --> 00:09:49,800 the same. 113 00:09:49,800 --> 00:09:52,140 You are ill because you have not given any other. 114 00:09:52,380 --> 00:09:54,210 You are as an action attribute. 115 00:09:54,930 --> 00:09:55,920 OK, so this is the. 116 00:09:57,610 --> 00:09:58,180 This is our. 117 00:09:59,560 --> 00:10:00,840 Not far. 118 00:10:03,710 --> 00:10:11,390 So the session will be opened in the browser will be textbooks, you will enter some user name submit 119 00:10:11,690 --> 00:10:18,650 the data will be now again redirected towards this login with the post maternelle. 120 00:10:18,830 --> 00:10:20,240 So that means now this. 121 00:10:21,520 --> 00:10:22,480 Will be executed. 122 00:10:22,660 --> 00:10:29,320 This condition is true, so session, not username, is equal to whatever username that you had, input 123 00:10:29,320 --> 00:10:33,250 will be faced from the request, object, request or form username. 124 00:10:34,090 --> 00:10:37,360 And the social variable will be variable by the name. 125 00:10:37,360 --> 00:10:41,740 Username will be set up and then your brother will be redirected towards. 126 00:10:43,370 --> 00:10:47,870 The you are all for index that are getting back to this leisz. 127 00:10:48,870 --> 00:10:55,230 The second time when it comes back to this, then the username is already present, so therefore. 128 00:10:56,190 --> 00:11:04,830 It will be fetched, and the text that will be rendered on your webpage or browser will be logged in 129 00:11:04,830 --> 00:11:08,670 as whichever username you might have given in the station. 130 00:11:10,890 --> 00:11:15,150 OK, so it will now tell you that you are logged in as this. 131 00:11:16,940 --> 00:11:17,410 OK. 132 00:11:19,470 --> 00:11:23,040 And it will again also give you another hyperlink. 133 00:11:25,030 --> 00:11:32,640 If you click on the hyperlink, it will redirect to your redirect the browser to the slash logout you 134 00:11:32,650 --> 00:11:32,770 are. 135 00:11:35,040 --> 00:11:42,120 OK, so what is this logout, you are doing it, we just pop the username session with evil that you 136 00:11:42,120 --> 00:11:43,440 had created and. 137 00:11:44,560 --> 00:11:50,240 So the media will be released and it will be again redirected towards the same index. 138 00:11:50,260 --> 00:11:51,870 Now the user name is not present. 139 00:11:51,880 --> 00:11:54,070 So again, back to this. 140 00:11:54,430 --> 00:11:55,870 You are not logged in and. 141 00:11:56,780 --> 00:12:02,270 You are given a chance by clicking the hyperlink to back to logging. 142 00:12:02,750 --> 00:12:10,430 So this is a slightly, uh, complex activity that we are going to perform as a demonstration of this 143 00:12:10,430 --> 00:12:10,910 example. 144 00:12:12,040 --> 00:12:14,470 OK, so the Python script is there. 145 00:12:16,830 --> 00:12:24,340 There are three warrants for the road logging and logging, and then, of course, there is a secondary 146 00:12:24,390 --> 00:12:25,200 e-mail page. 147 00:12:26,070 --> 00:12:34,000 Finally, there is a app that run in the department that we are to when the when this session is not 148 00:12:34,010 --> 00:12:36,270 people will be executed, the server will start. 149 00:12:37,080 --> 00:12:38,940 So this is the explanation of this. 150 00:12:40,770 --> 00:12:43,050 Gore on the flask application. 151 00:12:44,390 --> 00:12:47,480 So let us now try to understand how. 152 00:12:48,710 --> 00:12:56,930 The session is created, how the session variables are defined, even if needed, how the session variables 153 00:12:56,930 --> 00:13:03,890 are removed and how those variables remains persistent throughout the entire session. 154 00:13:03,920 --> 00:13:04,700 What is decision? 155 00:13:05,120 --> 00:13:14,060 The time interval for which the user is logged into a particular Web application and logged out of it? 156 00:13:15,860 --> 00:13:24,590 OK, so for this exercise, different resources that are needed from our flask model are important first. 157 00:13:24,610 --> 00:13:28,100 So this long list of import. 158 00:13:29,570 --> 00:13:36,470 You have to import the flask object, the session object, the request object and the functions redirect 159 00:13:36,770 --> 00:13:39,170 you are for and render a template. 160 00:13:39,920 --> 00:13:42,770 So these are our requirements which we have imported. 161 00:13:43,810 --> 00:13:47,200 Then, as usual, cleared the flask object. 162 00:13:49,190 --> 00:13:52,760 ABC to Flask DeVillers codename. 163 00:13:55,000 --> 00:13:58,270 This will create the flask application object. 164 00:13:59,480 --> 00:14:07,520 The mandatory thing is to set up a secret key of this plastic object, a secret key, you can give any 165 00:14:07,670 --> 00:14:08,390 string here. 166 00:14:09,850 --> 00:14:10,840 The rest of the. 167 00:14:13,130 --> 00:14:13,700 Script. 168 00:14:15,670 --> 00:14:19,660 Has three, you are really rules defined. 169 00:14:19,690 --> 00:14:20,410 One is the. 170 00:14:21,440 --> 00:14:27,800 Index, the other is lagging and the other is logout. 171 00:14:28,920 --> 00:14:31,410 So to begin with, when the server starts. 172 00:14:33,060 --> 00:14:38,970 And you give the localhost column five 5000, slash this index view function will be executed. 173 00:14:39,980 --> 00:14:41,210 So, as you will expect. 174 00:14:42,120 --> 00:14:50,760 And the very first place, as soon as you log into the server by the slash you are in, there will be 175 00:14:50,760 --> 00:14:51,810 no username. 176 00:14:53,560 --> 00:14:56,390 That will be in the session object. 177 00:14:56,440 --> 00:14:57,510 What is this object? 178 00:14:57,520 --> 00:15:03,680 Incidentally, it is a it is a dictionary of all the social variables and its associated values. 179 00:15:03,700 --> 00:15:06,700 So you are you have just started the server. 180 00:15:06,710 --> 00:15:12,250 So naturally, there will be no you there will be no session variable by the name username. 181 00:15:12,260 --> 00:15:13,700 So this condition will be false. 182 00:15:14,110 --> 00:15:16,760 So this will not take place. 183 00:15:17,590 --> 00:15:18,940 So what what will happen? 184 00:15:20,700 --> 00:15:21,810 It will come into this. 185 00:15:23,980 --> 00:15:31,300 And the message that will be displayed are rendered on your brother's surface will be you are not logged 186 00:15:31,300 --> 00:15:31,510 in. 187 00:15:32,440 --> 00:15:38,550 And it will give you a hyperlink to the slash login you are in. 188 00:15:40,310 --> 00:15:43,080 And we'll ask you to click here to login. 189 00:15:43,100 --> 00:15:45,890 So if you click on this login, what will happen? 190 00:15:47,750 --> 00:15:48,410 This. 191 00:15:49,350 --> 00:15:52,590 Browser will be redirected towards this, you are logging. 192 00:15:52,620 --> 00:15:55,380 This is the second rule that has been defined here. 193 00:15:56,620 --> 00:15:59,770 And additionally, the road to function. 194 00:16:02,220 --> 00:16:06,480 Taking two arguments, as usual, the usual and the Metters. 195 00:16:07,940 --> 00:16:08,900 Get that boost. 196 00:16:11,780 --> 00:16:13,500 OK, so for the first time in. 197 00:16:14,870 --> 00:16:17,750 The user clicks this click here to login. 198 00:16:19,030 --> 00:16:23,320 And for the first time, the in you are really stressed. 199 00:16:24,590 --> 00:16:28,070 Naturally, the request not matter will not be equal to post. 200 00:16:29,080 --> 00:16:29,740 It will be. 201 00:16:31,560 --> 00:16:34,740 It will be a great method, so therefore this. 202 00:16:36,740 --> 00:16:39,830 Rendered template session, that e-mail will. 203 00:16:42,450 --> 00:16:44,190 I mean, this action will take place. 204 00:16:44,210 --> 00:16:52,380 So what will happen, the browser will render that the template or the Web page or legislation or e-mail, 205 00:16:52,380 --> 00:16:53,760 which you will store into. 206 00:16:55,960 --> 00:16:58,040 The templates folder of your Web application. 207 00:16:58,420 --> 00:17:03,080 So at this juncture, we shall have a look at what is the intersession, that estimate. 208 00:17:05,860 --> 00:17:15,580 So this is the session of the court of this session that in which basically there is a small form. 209 00:17:16,630 --> 00:17:22,270 In which there is a input type is equal to text in which you can type the. 210 00:17:23,480 --> 00:17:27,290 Text, which will be stored in the attribute caller's name. 211 00:17:28,270 --> 00:17:34,900 And then the submit button, so if you click on the submit button, the data will be posted back to 212 00:17:34,900 --> 00:17:35,530 the same. 213 00:17:35,530 --> 00:17:37,870 You are ill because you have not given any other. 214 00:17:38,110 --> 00:17:39,970 You are as an action attribute. 215 00:17:40,660 --> 00:17:46,590 OK, so this is the this is our second not form. 216 00:17:49,440 --> 00:17:56,080 So is the mill will be opened in the browser, there will be textbooks, you will enter some name, 217 00:17:56,370 --> 00:18:04,370 press submit, the data will be now again redirected towards this login with the post method now. 218 00:18:04,560 --> 00:18:05,970 So that means now this. 219 00:18:07,250 --> 00:18:08,210 Will be executed. 220 00:18:08,390 --> 00:18:15,050 This condition is true, so session user username is equal to whatever username that you had, input 221 00:18:15,050 --> 00:18:18,980 will be first from the request, object, request or form username. 222 00:18:19,820 --> 00:18:23,090 And the social variable will be variable by the name. 223 00:18:23,090 --> 00:18:27,470 Username will be set up and then your browser will be redirected towards. 224 00:18:29,110 --> 00:18:33,610 The you are all for index that are getting back to this leisz. 225 00:18:34,580 --> 00:18:40,970 No second time when it comes back to this, then the username is already present, so therefore. 226 00:18:41,930 --> 00:18:50,570 It will be fetched, and the text that will be rendered on your webpage or browser will be logged in 227 00:18:50,570 --> 00:18:54,410 as whichever username you might have given intersession. 228 00:18:56,620 --> 00:19:00,880 OK, so it will now tell you that you are logged in as this. 229 00:19:02,700 --> 00:19:03,150 OK. 230 00:19:05,190 --> 00:19:08,760 And it will again also give you another hyperlink. 231 00:19:10,760 --> 00:19:18,500 If you click on the hyperlink, it will redirect to redirect the browser to the slash logout, you are 232 00:19:18,500 --> 00:19:18,830 a little. 233 00:19:20,770 --> 00:19:27,850 OK, so what is this logout you are doing, it will just pop the username session with one that you 234 00:19:27,850 --> 00:19:29,170 had created and. 235 00:19:30,270 --> 00:19:35,980 So the social media will be released and it will be again redirected towards the same index. 236 00:19:36,000 --> 00:19:37,610 Now the user name is not present. 237 00:19:37,620 --> 00:19:39,810 So again, back to this. 238 00:19:40,170 --> 00:19:48,000 You are not logged in and you are given a chance by clicking the hyperlink to back to login. 239 00:19:48,480 --> 00:19:56,670 So this is a slightly complex activity that we are going to perform as a demonstration of this example. 240 00:19:57,740 --> 00:20:00,200 OK, so the Python script is there. 241 00:20:02,530 --> 00:20:10,060 There are three warrants out the road, logging and logging, and then, of course, there is a 600 242 00:20:10,090 --> 00:20:10,930 e-mail page. 243 00:20:11,800 --> 00:20:16,600 Finally, there is an app that ran in the development that we are to. 244 00:20:17,310 --> 00:20:22,000 When the vendors next door will be executed, the server will start. 245 00:20:22,810 --> 00:20:24,670 So this is the explanation of this. 246 00:20:26,500 --> 00:20:28,810 More on the flask application.