1 00:00:00,590 --> 00:00:10,400 Hello, friends, in this video, I'll tell you about the message flashing mechanism in a flask application. 2 00:00:17,340 --> 00:00:19,560 Mean you are basing application. 3 00:00:21,380 --> 00:00:31,460 Uses a kind of a mechanism by which the user is prompted or told about certain activity or events that 4 00:00:31,460 --> 00:00:33,300 are taking place are happening. 5 00:00:33,800 --> 00:00:43,730 So this mechanism of providing the feedback to the user about it's about his interaction, such a mechanism 6 00:00:43,730 --> 00:00:53,210 or such a feature available in the mini desktop applications built in languages like Java or dot net 7 00:00:53,210 --> 00:00:57,220 languages, for example, that Java has a feature of dialogue. 8 00:00:57,230 --> 00:01:00,470 There is there are dialogues boxes that can be created. 9 00:01:02,390 --> 00:01:10,820 Languages like visual, basic or C Sharp has a facility or a class message box of both is the same. 10 00:01:11,150 --> 00:01:15,380 It flashes the message on the top of the underlying frame. 11 00:01:15,890 --> 00:01:19,580 For example, the name of the file is already present. 12 00:01:19,580 --> 00:01:21,150 Do you want to write it? 13 00:01:21,430 --> 00:01:27,230 This is a dialogue or the message that is popped up when a particular kind of activity takes place. 14 00:01:27,500 --> 00:01:33,020 So such a dialogue or message box is generated by their desktop applications. 15 00:01:33,260 --> 00:01:37,550 Similar kind of activity is also finally found in JavaScript. 16 00:01:37,850 --> 00:01:46,850 JavaScript has a function called as alert, so it pops up a message to let the user know that what is 17 00:01:46,850 --> 00:01:48,070 to be done or what has happened. 18 00:01:49,130 --> 00:01:51,260 So the same purpose. 19 00:01:52,880 --> 00:02:00,260 The purpose that is provided by their dialogue, message box or alert in flask, it is provided by the 20 00:02:00,260 --> 00:02:01,190 message flashin. 21 00:02:02,750 --> 00:02:09,390 The flashing system of this flask, it makes it possible to create a message, it will create a message. 22 00:02:10,460 --> 00:02:19,370 We have seen that when a particular order is given, the associated review function is executed during 23 00:02:19,370 --> 00:02:20,930 the course of this execution. 24 00:02:21,290 --> 00:02:30,730 If a certain message is to be displayed to the user, then the inside of you function flash. 25 00:02:30,780 --> 00:02:31,660 Flash is used. 26 00:02:32,360 --> 00:02:33,820 So what is what it will do? 27 00:02:33,830 --> 00:02:41,100 It will create a certain message and that message will be passed on to the next of your function. 28 00:02:41,540 --> 00:02:48,680 Remember that the flash or the flashing will not be done in the same you function in the flashing has 29 00:02:48,680 --> 00:02:49,380 been generated. 30 00:02:50,210 --> 00:02:59,630 So the important thing to note is that it makes it creates a message in one view and render that message 31 00:02:59,780 --> 00:03:01,570 in another view or the next to you. 32 00:03:02,180 --> 00:03:07,360 So the flash method passes a message to the next request that is very important. 33 00:03:08,580 --> 00:03:08,920 Right. 34 00:03:09,090 --> 00:03:18,840 So the syntax of function is that it takes two arguments, plus there is a message, the message that 35 00:03:18,840 --> 00:03:21,650 is actually to be flashed on the client's browser. 36 00:03:21,840 --> 00:03:30,300 And the second is the optional parameter or less category message parameter is the actual message category 37 00:03:30,310 --> 00:03:31,380 parameters optional. 38 00:03:31,740 --> 00:03:37,680 Three types of categories can be mentioned error info or warnings. 39 00:03:37,980 --> 00:03:46,200 So what kind of message it is accordingly, on the next you function or on the next to you are, the 40 00:03:46,200 --> 00:03:52,740 user will come to know that this is an error or this is the information, or is it a warning to such 41 00:03:52,970 --> 00:03:59,160 a flash function can be executed in one view and on the other view the flash will be displayed. 42 00:04:03,140 --> 00:04:11,870 Not now that one view passes the flashes to another view that it is the duty of the next of you to display 43 00:04:11,870 --> 00:04:12,720 those flashes. 44 00:04:13,550 --> 00:04:18,670 So what to do the next time you remove those messages from the station? 45 00:04:18,680 --> 00:04:21,940 Those flashes will be stored in the station from that station. 46 00:04:21,980 --> 00:04:28,470 Those will be extracted, removed, and they will be used to display with the help of a function. 47 00:04:28,510 --> 00:04:30,830 But let's get flashing the messages. 48 00:04:32,150 --> 00:04:39,760 The syntax or prototype of use of this get flashed flash messages is like this get flashing messages. 49 00:04:39,770 --> 00:04:44,440 Both arguments are optional with categories and category filter. 50 00:04:44,450 --> 00:04:51,640 If they are flashed, the messages have certain categories, then there will be a value to this parameter. 51 00:04:53,480 --> 00:05:00,320 And if there are any categories, if there are multiple categories and you want the viewer to display 52 00:05:00,320 --> 00:05:06,750 only those flashes or those messages of a particular category, only you can apply the filter. 53 00:05:07,400 --> 00:05:10,730 So get flashily messages to Offaly barometer's. 54 00:05:10,730 --> 00:05:13,340 Maybe you're not a may not be OK. 55 00:05:14,030 --> 00:05:16,370 Now this get flashed. 56 00:05:16,370 --> 00:05:22,520 The messages will collect all those messages and how these messages messages are processed. 57 00:05:22,790 --> 00:05:29,960 They are processed during or inside the template that is being rendered by the first of you function. 58 00:05:30,740 --> 00:05:33,320 The first of you function will call another. 59 00:05:33,320 --> 00:05:38,600 You are in by a render template and that template will have some such mechanism. 60 00:05:40,130 --> 00:05:46,990 What will happen first statement is first, a template statement is with messages equal to get flashily 61 00:05:47,000 --> 00:05:52,490 messages, there may be any optional parameters like this or not. 62 00:05:53,360 --> 00:05:56,100 So what will happen this get flashed? 63 00:05:56,100 --> 00:06:01,040 The messages function will find out whether there are any messages to be flashed or not. 64 00:06:01,940 --> 00:06:05,060 If there are any messages, there may be multiple messages. 65 00:06:05,060 --> 00:06:07,010 So there will be a for loop. 66 00:06:09,430 --> 00:06:14,170 So one message at a time will be flashed on the browser. 67 00:06:15,600 --> 00:06:16,390 Of that line. 68 00:06:17,010 --> 00:06:26,660 So this is the eve and this is done with such a nested arrangement of wit and with if and if any, for 69 00:06:26,670 --> 00:06:32,350 and for those will be used on the template code. 70 00:06:32,670 --> 00:06:39,030 So with the help of this, the messages that are received from the previous view will be will be flashed 71 00:06:39,360 --> 00:06:40,170 by the next room. 72 00:06:40,470 --> 00:06:51,120 So such a mechanism now will have a very comprehensive example which will generate the messages and 73 00:06:51,120 --> 00:06:54,490 those messages will be passed to a template and that template be displayed. 74 00:06:54,490 --> 00:07:01,670 The messages set an example I will show you with the help of those examples, we will come to know or 75 00:07:02,040 --> 00:07:06,900 understand better the use of messages and flashing in a flask application. 76 00:07:07,920 --> 00:07:15,330 So this is a demonstration of the message flashing in the flask application. 77 00:07:15,330 --> 00:07:19,920 And for that purpose, you will have to import the flash function in our last module. 78 00:07:20,550 --> 00:07:27,900 So in addition to the other required functions like redirect the render template you are for, you'll 79 00:07:27,900 --> 00:07:32,110 also need a request object and of course the object to be important. 80 00:07:32,130 --> 00:07:35,820 So those requirements are important in the beginning. 81 00:07:36,510 --> 00:07:40,470 Then you of course create the application object set of the. 82 00:07:41,470 --> 00:07:42,210 Secretary. 83 00:07:43,730 --> 00:07:46,730 First, you are defined is backslash. 84 00:07:48,750 --> 00:07:56,010 And bound new function index, what it is going to do, it is going to render their template or index 85 00:07:56,010 --> 00:07:56,820 not estimate. 86 00:07:57,650 --> 00:07:59,600 So what is their index not estimate? 87 00:08:02,340 --> 00:08:04,980 This is the index, not SGML. 88 00:08:06,130 --> 00:08:06,760 Now that. 89 00:08:08,070 --> 00:08:14,160 Index view function is likely to be executed when. 90 00:08:16,560 --> 00:08:20,730 It is to the user is already logged in and it is he's trying to log out. 91 00:08:22,140 --> 00:08:24,240 Or the logging is unsuccessful. 92 00:08:25,430 --> 00:08:30,410 In either cases, your flask application is going to generate some. 93 00:08:31,610 --> 00:08:35,240 Flash messages, so before. 94 00:08:36,800 --> 00:08:44,780 Displaying this part of the index, not if there are any messages to be flashed, those will be done 95 00:08:44,780 --> 00:08:46,280 by this school. 96 00:08:48,650 --> 00:08:53,360 But of course, now now that you are just starting the application, there are no messages to be flashed, 97 00:08:53,360 --> 00:08:55,600 so therefore this condition will be not true. 98 00:08:55,850 --> 00:09:00,380 So therefore, the this entire segment of the court will not be executed. 99 00:09:00,390 --> 00:09:01,100 You'll only see. 100 00:09:01,100 --> 00:09:05,980 Do you want to log in and do this logging work? 101 00:09:05,990 --> 00:09:09,470 That is a hyperlink leading to your all for logging. 102 00:09:10,510 --> 00:09:11,260 The other. 103 00:09:12,510 --> 00:09:14,880 You are a little different in your Python application. 104 00:09:15,820 --> 00:09:18,020 So we'll come back to this afterwards. 105 00:09:18,220 --> 00:09:21,430 What what is the purpose of this right now? 106 00:09:23,980 --> 00:09:31,120 There will be a simple hyperlinked, do you want to login and the hyperlink will lead you to you are 107 00:09:31,120 --> 00:09:35,700 all for logging the logging view function that is defined in your application. 108 00:09:35,710 --> 00:09:36,910 So we'll have a look at that. 109 00:09:38,750 --> 00:09:45,620 So this is the slash log-in you are in, which is defined by the route, the greater taking to arguments, 110 00:09:46,040 --> 00:09:48,080 the you are in and the methods. 111 00:09:49,740 --> 00:09:57,930 Again, when this log-in you is visited for the first time, obviously the request not is equal to this 112 00:09:57,930 --> 00:10:00,310 condition will not be true, isn't it? 113 00:10:01,210 --> 00:10:02,140 This is not true. 114 00:10:03,770 --> 00:10:04,490 So therefore. 115 00:10:05,650 --> 00:10:07,280 This part will not be executed. 116 00:10:11,620 --> 00:10:12,970 So therefore, you will. 117 00:10:15,200 --> 00:10:16,160 Come across this. 118 00:10:18,290 --> 00:10:18,630 Right. 119 00:10:18,650 --> 00:10:19,130 So. 120 00:10:20,180 --> 00:10:23,730 It will render the template cold logic, not estimate. 121 00:10:24,330 --> 00:10:29,510 Now you see that the render template function is making taking two arguments. 122 00:10:29,520 --> 00:10:30,050 One is the. 123 00:10:31,960 --> 00:10:35,830 And you know, the template to be rendered login that e-mail and there is a. 124 00:10:37,560 --> 00:10:44,280 Error parameter, the value of the error parameter is the default value of the error parameters defined 125 00:10:44,280 --> 00:10:46,290 as none, because initially it will be not. 126 00:10:50,050 --> 00:10:55,180 Then what will happen this log in the e-mail template will be rendered with error is equal to none. 127 00:10:56,050 --> 00:10:58,060 OK, so what is the legendary. 128 00:11:01,190 --> 00:11:06,380 This is the estimable logging estimate in which there is a. 129 00:11:09,170 --> 00:11:17,540 Ginge to templated court, if at all, there was any error that was raised before coming to this, that 130 00:11:17,540 --> 00:11:18,560 error will be displayed. 131 00:11:18,710 --> 00:11:26,850 Otherwise it will display form where there are two text text boxes. 132 00:11:26,850 --> 00:11:32,960 So to save one for username adelfa the password, and then there is a submit button which will post 133 00:11:32,960 --> 00:11:34,280 the data to the same. 134 00:11:34,310 --> 00:11:36,150 You are in log. 135 00:11:38,390 --> 00:11:38,820 Right. 136 00:11:39,470 --> 00:11:44,270 So that means if you click the submit button, the data will be posted the same slash login. 137 00:11:45,730 --> 00:11:48,610 You are the view function now. 138 00:11:49,880 --> 00:11:52,670 For the second time when that log in. 139 00:11:53,870 --> 00:11:58,820 You are visited, the Log-in view function is executed, another method is post. 140 00:12:00,650 --> 00:12:02,090 Isn't it so? 141 00:12:03,820 --> 00:12:09,740 We will have a look at that log in are religion and see what happens. 142 00:12:11,230 --> 00:12:12,540 So it comes again here. 143 00:12:12,940 --> 00:12:14,220 Now the method is post. 144 00:12:14,470 --> 00:12:15,820 So this condition is true. 145 00:12:17,650 --> 00:12:22,930 No, it will verify whether the username and password are admin admin. 146 00:12:25,920 --> 00:12:32,580 If they are not admin admin, then it will raise an error, the variable error will be initialized to 147 00:12:32,580 --> 00:12:34,190 invalid username and password. 148 00:12:34,860 --> 00:12:44,520 If, on the other hand, if the username and password the values of the two data fields in the in our 149 00:12:44,520 --> 00:12:45,330 e-mail form. 150 00:12:47,240 --> 00:12:54,260 Do have the value of admin admin, then the message will be, you are you will successfully logged in. 151 00:12:55,280 --> 00:13:00,670 And there will be another flash log logout before the logging, before logging again. 152 00:13:02,240 --> 00:13:08,750 OK, and taking these two flash messages, it will go back to the index. 153 00:13:11,150 --> 00:13:14,360 It will go back to the index you function. 154 00:13:16,110 --> 00:13:20,730 Right, so we'll have to revisit our indexer testimony and then analyze. 155 00:13:21,710 --> 00:13:29,240 The, you know, segment of the ginger to template wherein the get flashed messages are processed. 156 00:13:33,250 --> 00:13:38,770 So this is that part now that the index, not e-mail, is going to be rendered for the next for the 157 00:13:38,770 --> 00:13:39,360 second time. 158 00:13:41,890 --> 00:13:42,430 There are. 159 00:13:44,220 --> 00:13:51,510 The messages to be flashed, those will be collected with this and for each if there are any messages. 160 00:13:52,500 --> 00:13:59,670 If there are any messages, the messages will there will be messages object, so if messages, yes, 161 00:14:00,150 --> 00:14:04,920 so far, each message in these messages, that message will be displayed. 162 00:14:06,380 --> 00:14:12,280 Right, so this will display all the messages in our case, there are two that are going to be two messages 163 00:14:12,280 --> 00:14:19,280 if the logging is successful, that there are two flash to cause the flash flash function. 164 00:14:19,780 --> 00:14:21,100 So there were two messages. 165 00:14:21,310 --> 00:14:24,220 Those two messages are going to be displayed by these four. 166 00:14:26,740 --> 00:14:34,690 OK, so this block will display all the flash messages generated by the earlier view function. 167 00:14:37,020 --> 00:14:44,490 And then again, of course, after the messages are flashed, you again have this hyperlink to re login. 168 00:14:47,080 --> 00:14:56,500 OK, so this is the slightly complex explanation of the flushing mechanism in our flask application 169 00:14:56,840 --> 00:14:58,510 will do these activities one by one. 170 00:15:02,130 --> 00:15:10,410 So our Python code for this entire flash activity is written in flash, not ever, so I will start the 171 00:15:11,250 --> 00:15:17,460 server and I will go to the browser to visit the localhost 5000, slash whatever. 172 00:15:20,580 --> 00:15:25,290 So I will enter that you are into the browser's address bar. 173 00:15:26,900 --> 00:15:28,760 It is giving me a hyperlink to. 174 00:15:30,110 --> 00:15:31,760 Take me to this login page. 175 00:15:34,560 --> 00:15:35,670 This is the login page. 176 00:15:36,510 --> 00:15:39,600 Now I will show you both the scenarios where. 177 00:15:40,760 --> 00:15:42,050 I use some random. 178 00:15:43,250 --> 00:15:49,940 Username and password, now what will happen, the error will be raised through the error message will 179 00:15:49,940 --> 00:15:50,510 be displayed. 180 00:15:52,320 --> 00:15:57,740 Second time I will give you a username and password that is acceptable. 181 00:15:59,770 --> 00:16:03,580 And then log in now, the messages will be flashed. 182 00:16:04,930 --> 00:16:08,240 You were successfully logged in longer before the log in again. 183 00:16:09,090 --> 00:16:11,410 OK, these are generated by. 184 00:16:13,110 --> 00:16:17,820 The flashes that were raised by the login function. 185 00:16:18,920 --> 00:16:22,010 Do you want to log in, it will again take me back to the login page. 186 00:16:23,290 --> 00:16:31,570 OK, so this is the entire cycle of events that will that the application will go through for the message 187 00:16:31,570 --> 00:16:31,960 flashing.