1 00:00:00,810 --> 00:00:02,948 For this lecture, I'd like you to go to the 2 00:00:02,949 --> 00:00:07,970 resources of this lecture and download the three text files. 3 00:00:07,971 --> 00:00:12,388 Then in the Explorer here in your IDE, create a 4 00:00:12,389 --> 00:00:17,828 new folder, give it a name such as quotes and 5 00:00:17,829 --> 00:00:22,058 then put those three files, in that quotes folder. 6 00:00:22,059 --> 00:00:24,100 You can use drag and drop if you like. 7 00:00:24,101 --> 00:00:28,268 For example, I have these files somewhere and I'm going 8 00:00:28,269 --> 00:00:30,800 to just drag and drop them in this folder. 9 00:00:31,530 --> 00:00:35,430 So you see that, now the files are in the quotes folder. 10 00:00:36,410 --> 00:00:38,662 So what we are going to do in this lecture 11 00:00:38,663 --> 00:00:41,500 is, in the program that we have, 12 00:00:41,501 --> 00:00:45,766 [Author Typing] 13 00:00:45,767 --> 00:00:51,504 we are going to, write one of these three supported fieldings for now. 14 00:00:51,505 --> 00:00:54,724 You can add more files later if you have some data. 15 00:00:54,725 --> 00:00:58,100 But, this is what we have so far. 16 00:00:58,101 --> 00:01:00,218 And so when the user presses the Enlighten 17 00:01:00,219 --> 00:01:03,832 me button, we want to show one of 18 00:01:03,833 --> 00:01:09,410 the quotes that are inside the happy.txt file. 19 00:01:10,390 --> 00:01:12,833 So one of these lines, 20 00:01:12,834 --> 00:01:14,800 [No Audio] 21 00:01:14,801 --> 00:01:17,066 some are long, some are shorter. 22 00:01:17,067 --> 00:01:20,170 [No Audio] 23 00:01:20,171 --> 00:01:24,208 And so if the user enters sad, then you 24 00:01:24,209 --> 00:01:28,544 want to display here, in this area, one of 25 00:01:28,545 --> 00:01:31,933 the lines of the sad.txt file. 26 00:01:32,266 --> 00:01:37,124 Those also are quotes, and so you get the idea. 27 00:01:37,125 --> 00:01:41,133 And if the user enters an unknown feeling, 28 00:01:42,533 --> 00:01:45,848 such as let's say, excited, and when they press a 29 00:01:45,849 --> 00:01:48,552 button you want to show, try another felling, because 30 00:01:48,553 --> 00:01:52,334 you cannot have all possible fellings there covered. 31 00:01:52,335 --> 00:01:55,030 So this is what we are going to do in this lecture. 32 00:01:55,033 --> 00:01:59,466 [No Audio] 33 00:01:59,466 --> 00:02:03,724 main.py, so let's start from the button. 34 00:02:03,725 --> 00:02:08,127 Try to locate the Enlighten me button, or at 35 00:02:08,128 --> 00:02:11,072 least that's how my button is called. So Button, 36 00:02:11,073 --> 00:02:14,368 that button is part of the LoginScreenSuccess 37 00:02:14,369 --> 00:02:19,904 page. And we don't have an on_press attribute yet. 38 00:02:19,905 --> 00:02:22,132 So on_press, what you want to do is you 39 00:02:22,133 --> 00:02:24,433 want to execute a method, 40 00:02:24,434 --> 00:02:27,300 [Author Typing] 41 00:02:27,301 --> 00:02:32,100 of the LoginScreenSuccess class, which I have it right here. 42 00:02:32,630 --> 00:02:35,768 So that LoginScreenSuccess, it already has 43 00:02:35,769 --> 00:02:39,384 a method log_out, which is executed when this 44 00:02:39,385 --> 00:02:41,832 Button is pressed, the Logout Button. 45 00:02:41,833 --> 00:02:45,250 Now we want to implement the get_quote method, 46 00:02:47,430 --> 00:02:52,572 quote self, as usual until this method now has 47 00:02:52,573 --> 00:02:56,348 to communicate, the value that the user entered in 48 00:02:56,349 --> 00:03:02,566 the TextInput, so which is root.ids. 49 00:03:02,670 --> 00:03:07,472 we don't have an id for TextInputs, so let's put an id. 50 00:03:07,473 --> 00:03:09,898 So this is the TextInput where the user entered 51 00:03:09,899 --> 00:03:15,690 the feeling. And so let's put feeling there, ids.feeling, 52 00:03:15,691 --> 00:03:18,756 we want to get the text of that 53 00:03:18,757 --> 00:03:22,420 TextInput, so that feeling will go here. 54 00:03:22,433 --> 00:03:24,566 [No Audio] 55 00:03:24,566 --> 00:03:26,664 Let's just call it feel, not to 56 00:03:26,665 --> 00:03:29,910 be confused with the feeling root. 57 00:03:29,911 --> 00:03:33,080 And, the first thing we might want to do is 58 00:03:33,081 --> 00:03:37,930 let's see if we are accessing the felling text correctly. 59 00:03:37,931 --> 00:03:47,000 So print feel, and make sure you save the design.kv file, 60 00:03:47,001 --> 00:03:54,518 execute Login, I was able to Login because, I have this username I created earlier. 61 00:03:54,519 --> 00:03:57,648 When I signed up, I created one username which is 62 00:03:57,649 --> 00:04:00,550 an empty string, and an empty string for the Password. 63 00:04:00,551 --> 00:04:02,634 So basically it's a user with empty string, empty 64 00:04:02,635 --> 00:04:05,188 string as username and password, which works because I 65 00:04:05,189 --> 00:04:09,600 haven't put any restrictions on the Username and Password. 66 00:04:09,890 --> 00:04:11,902 So this is just for simplicity. 67 00:04:11,903 --> 00:04:13,560 When you try, when you test 68 00:04:13,561 --> 00:04:17,031 the application, happy, Enlighten me 69 00:04:17,032 --> 00:04:20,968 and so we get the happy, string there. 70 00:04:20,969 --> 00:04:23,560 And if that is with a big 71 00:04:23,561 --> 00:04:26,428 H, you're going to get Happy. 72 00:04:26,429 --> 00:04:29,068 So the first thing we might want to 73 00:04:29,069 --> 00:04:34,898 do is, to, change the feel into lowercase. 74 00:04:34,899 --> 00:04:38,130 So even if the user enters Happy with a capital 75 00:04:38,131 --> 00:04:43,370 H, we are going to convert that to all lowercase. 76 00:04:44,590 --> 00:04:47,488 So that we can compare now with the names in 77 00:04:47,489 --> 00:04:50,554 the text files, with the file names. 78 00:04:50,555 --> 00:04:52,254 So let's now see what, 79 00:04:52,255 --> 00:04:55,233 [Author Typing] 80 00:04:55,234 --> 00:04:56,884 available_feelings we have. 81 00:04:56,885 --> 00:05:01,433 So I'm assigning a variable available_feelings, and 82 00:05:01,434 --> 00:05:04,900 I'm going to use the glob module to 83 00:05:04,901 --> 00:05:07,030 [Author Typing] 84 00:05:07,031 --> 00:05:11,410 get a list of file names. 85 00:05:11,410 --> 00:05:15,770 [Author Typing] 86 00:05:15,771 --> 00:05:18,280 This is what this is going to give me 87 00:05:18,280 --> 00:05:21,130 [Author Typing] 88 00:05:21,131 --> 00:05:25,200 an error, of course, because I didn't import glob. 89 00:05:25,201 --> 00:05:31,630 [Author Typing] 90 00:05:31,631 --> 00:05:35,710 It gives you a list with all the files. 91 00:05:35,711 --> 00:05:38,810 This is the available, all the available feelings. 92 00:05:40,466 --> 00:05:43,754 So we are here in the get_quote method. 93 00:05:43,755 --> 00:05:46,676 Now, the available_feelings, atlest now looks 94 00:05:46,677 --> 00:05:49,306 like a list of file paths. 95 00:05:49,307 --> 00:05:51,300 What I want to do is, I want 96 00:05:51,301 --> 00:05:54,958 to extract only the names of these files. 97 00:05:54,959 --> 00:05:57,350 So only the sad part. 98 00:05:57,351 --> 00:06:01,860 A way to do that, is by using 99 00:06:01,866 --> 00:06:05,110 [Author Typing] 100 00:06:05,111 --> 00:06:07,842 the path object of the pathlib library. 101 00:06:07,843 --> 00:06:08,988 You don't have to install this 102 00:06:08,989 --> 00:06:10,882 library, it comes with Python, 103 00:06:10,883 --> 00:06:13,570 it's standard library. Using Path, 104 00:06:13,571 --> 00:06:20,100 if you give Path, a path such as quotes/sad.txt, 105 00:06:20,101 --> 00:06:23,552 and then extract the stem out of that, it 106 00:06:23,553 --> 00:06:26,672 will give you the name of the file without the 107 00:06:26,673 --> 00:06:30,590 extension, without the other part of the Path. 108 00:06:31,890 --> 00:06:34,580 In fact, you have many methods of 109 00:06:34,581 --> 00:06:37,540 path and stem is one of them. 110 00:06:37,541 --> 00:06:40,484 So you can get the suffix, the name of the 111 00:06:40,485 --> 00:06:42,600 file, including the extension, 112 00:06:42,601 --> 00:06:45,833 [Author Typing] 113 00:06:45,834 --> 00:06:48,260 such as that and so on. 114 00:06:48,790 --> 00:06:52,408 And, so what we can do here now is, we need 115 00:06:52,409 --> 00:06:56,492 to make use of that stem property and we need to 116 00:06:56,493 --> 00:07:00,988 apply that to all the file names. To do that, 117 00:07:00,989 --> 00:07:03,394 so you can use a list comprehension. 118 00:07:03,395 --> 00:07:07,370 So available_feelings equal to Path. 119 00:07:07,950 --> 00:07:10,688 Let's say filename for 120 00:07:10,689 --> 00:07:15,290 filename in available_feelings. 121 00:07:16,350 --> 00:07:18,690 And here, I would like to 122 00:07:18,691 --> 00:07:22,110 split the line into multiple lines. 123 00:07:23,730 --> 00:07:26,218 Since this is inside square brackets, 124 00:07:26,219 --> 00:07:28,324 I'm allowed to split the line. 125 00:07:28,325 --> 00:07:31,860 And so we want the stem out of that. 126 00:07:31,866 --> 00:07:35,030 [Author Typing] 127 00:07:35,031 --> 00:07:37,816 Then we might want to try a 128 00:07:37,817 --> 00:07:41,102 print in here, of available_feelings. 129 00:07:41,103 --> 00:07:44,878 Also make sure to import, from pathlib 130 00:07:44,879 --> 00:07:51,800 import Path, and then execute the app. 131 00:07:51,801 --> 00:07:57,400 [No Audio] 132 00:07:57,410 --> 00:07:59,880 And I got an error when I press the button, 133 00:07:59,881 --> 00:08:02,110 string object has no attribute stem. 134 00:08:02,111 --> 00:08:05,816 Yeah, of course, because I did here, instead 135 00:08:05,817 --> 00:08:08,488 of putting stem after the Path object, I 136 00:08:08,489 --> 00:08:11,180 put it just next to the filename, 137 00:08:11,181 --> 00:08:12,930 filename is a string. 138 00:08:12,931 --> 00:08:16,870 So that's why you get str has no stem. 139 00:08:17,450 --> 00:08:20,840 So let's put it in here and try again. 140 00:08:22,670 --> 00:08:26,256 Enlighten me, so happy, sad, unloved, that's what we want. 141 00:08:26,257 --> 00:08:29,820 Now we have the available feelings in a list. 142 00:08:29,833 --> 00:08:33,299 [Author Typing] 143 00:08:33,309 --> 00:08:40,990 What can I do now? I can say, if feel in available_feelings. 144 00:08:41,000 --> 00:08:43,650 [No Audio] 145 00:08:43,651 --> 00:08:47,870 So if that feeling is in the available_feelings, 146 00:08:47,870 --> 00:08:50,390 [No Audio] 147 00:08:50,391 --> 00:08:53,288 then, what we want to do next, is we 148 00:08:53,289 --> 00:08:56,350 want to open that file, that has that feeling, 149 00:08:56,351 --> 00:08:59,102 that has the quote for that feeling. 150 00:08:59,103 --> 00:09:01,244 So let's use an open I'm going 151 00:09:01,245 --> 00:09:03,640 to use string formatting here f. 152 00:09:04,250 --> 00:09:08,018 So quotes, I'm going to open this file 153 00:09:08,019 --> 00:09:15,210 path, which has this feel.txt as file. 154 00:09:15,950 --> 00:09:17,760 So what am I doing here? 155 00:09:17,761 --> 00:09:21,040 Well, that thing, what that is going to give me is 156 00:09:21,041 --> 00:09:25,424 let's say the field, that the user entered was happy, right? 157 00:09:25,425 --> 00:09:29,156 So we get that variable, it has a value. 158 00:09:29,157 --> 00:09:33,466 And if we get now this string, 159 00:09:33,500 --> 00:09:36,200 [No Audio] 160 00:09:36,201 --> 00:09:41,582 what is going to give us, is the file path for that felling. 161 00:09:41,583 --> 00:09:46,056 So this expression here in these braces, is 162 00:09:46,057 --> 00:09:49,592 going to be replaced with the value of 163 00:09:49,593 --> 00:09:51,910 that variable of the field variable. 164 00:09:52,890 --> 00:09:56,140 So, I hope that is clear. 165 00:09:56,141 --> 00:10:00,070 And then we want to get all the quotes, 166 00:10:00,730 --> 00:10:04,570 of that file, using the readlines method. 167 00:10:05,630 --> 00:10:09,710 So that will give us a list of all the quotes. 168 00:10:09,711 --> 00:10:14,182 So let me unindent and print the quotes, 169 00:10:14,183 --> 00:10:18,576 to see what we've got this for. happy, 170 00:10:18,577 --> 00:10:21,444 the quotes were happy, and this is a list. 171 00:10:21,445 --> 00:10:24,052 So as I told you, happiness is when you what 172 00:10:24,053 --> 00:10:26,116 you think, and what you say, and so on. 173 00:10:26,117 --> 00:10:32,632 And then it was told by, Mahatma Gandhi and 174 00:10:32,633 --> 00:10:35,352 then you have a comma, and there is where 175 00:10:35,353 --> 00:10:38,712 the other quote starts. And so on, 176 00:10:38,713 --> 00:10:40,334 we have a list of multiple 177 00:10:40,335 --> 00:10:44,338 strings. Each string representing a quote. 178 00:10:44,339 --> 00:10:47,066 So what can we do with this quote, now? 179 00:10:48,900 --> 00:10:54,833 What we can do is, we can get the self.ids., 180 00:10:55,933 --> 00:10:58,816 the id of this label where we want to show 181 00:10:58,817 --> 00:11:02,620 the quote, which is just under the Enlighten me button. 182 00:11:03,550 --> 00:11:11,412 So the id quotes, quote, quote.text, 183 00:11:11,413 --> 00:11:13,812 so the text of that label will 184 00:11:13,813 --> 00:11:20,550 be, random.choice of the quotes list. 185 00:11:20,551 --> 00:11:23,366 So what does this method do is, 186 00:11:23,367 --> 00:11:27,200 [Author Typing] 187 00:11:27,201 --> 00:11:30,210 it gets a list as input. 188 00:11:31,050 --> 00:11:35,916 So 1, 2, 3, and it returns a 189 00:11:35,917 --> 00:11:39,516 random item out of that list, anytime you 190 00:11:39,517 --> 00:11:43,290 execute it, say just pure random. 191 00:11:44,750 --> 00:11:48,358 So therefore we're going to get one of these strings, 192 00:11:48,359 --> 00:11:52,512 one of these quotes, and that quote is going to 193 00:11:52,513 --> 00:11:55,450 be set as a text of the label. 194 00:11:56,610 --> 00:11:58,244 So that's how it works. 195 00:11:58,245 --> 00:12:06,644 And let's make sure we import random and 196 00:12:06,645 --> 00:12:09,566 we are ready to execute the app. 197 00:12:09,567 --> 00:12:12,678 [No Audio] 198 00:12:12,679 --> 00:12:16,200 Login, happy, Enlighten me 199 00:12:16,201 --> 00:12:18,533 [No Audio] 200 00:12:18,534 --> 00:12:23,050 and we got this error, which is quite common AttributeError. 201 00:12:23,051 --> 00:12:25,933 super has no attribute, __getattr__ 202 00:12:25,934 --> 00:12:30,508 And this happens, when the Python file doesn't find 203 00:12:30,509 --> 00:12:33,980 that id, the quote id, in this case. 204 00:12:35,390 --> 00:12:39,440 So you can see that the error happened in line 61. 205 00:12:39,441 --> 00:12:42,080 So that line in there is not working. 206 00:12:42,081 --> 00:12:44,902 Well, that is I think because I didn't 207 00:12:44,903 --> 00:12:48,148 save the kivy file when I entered, when 208 00:12:48,149 --> 00:12:51,284 I updated the id of the Label here. 209 00:12:51,285 --> 00:12:54,510 So I wrote an id, by id, and save the kivy file. 210 00:12:55,170 --> 00:12:56,720 So let me try again. 211 00:12:56,733 --> 00:12:59,190 [No Audio] 212 00:12:59,191 --> 00:13:01,672 I'm happy these errors emerge because 213 00:13:01,673 --> 00:13:03,540 you learn how to fix them. 214 00:13:04,710 --> 00:13:09,140 Enlighten me, and yeah, so we get the label displayed in here. 215 00:13:10,090 --> 00:13:13,996 When you press again, that random.choice function is going 216 00:13:13,997 --> 00:13:17,960 to get another random.choice out of the list. 217 00:13:17,966 --> 00:13:20,090 [No Audio] 218 00:13:20,091 --> 00:13:24,528 Sometimes I suppose it may display the same code, which 219 00:13:24,529 --> 00:13:27,392 is fine, I think, but there are ways to make 220 00:13:27,393 --> 00:13:30,640 it display a different quote from the previous one. 221 00:13:30,641 --> 00:13:33,500 But this is working quite good, I think. 222 00:13:34,110 --> 00:13:40,042 Expect of the case, when there is a long quote 223 00:13:40,043 --> 00:13:44,714 and it's out of the borders of the app window. 224 00:13:44,715 --> 00:13:47,838 So this is a problem, but nothing to worry 225 00:13:47,839 --> 00:13:50,766 about, because we are going to implement a scroll 226 00:13:50,767 --> 00:13:54,744 view later on, which will squeeze this label and 227 00:13:54,745 --> 00:13:59,950 it will make it flexible, by resizing the label, 228 00:13:59,951 --> 00:14:03,308 depending on the widget, on the app size. 229 00:14:03,309 --> 00:14:09,244 For now, let me close that and finish the if else 230 00:14:09,245 --> 00:14:19,530 block, self.ids.quote.text = To Try another feeling. 231 00:14:20,990 --> 00:14:22,720 So let me try one more time 232 00:14:22,721 --> 00:14:25,680 and we are done with this video. 233 00:14:26,690 --> 00:14:28,080 So that is working. 234 00:14:28,690 --> 00:14:30,199 That is also working, 235 00:14:30,200 --> 00:14:33,633 [No Audio] 236 00:14:33,634 --> 00:14:36,538 and I feel pretty oh, Try another feeling, 237 00:14:36,539 --> 00:14:38,372 we don't support that. 238 00:14:38,373 --> 00:14:43,790 And this is how we make an app, that picks a quote randomly. 239 00:14:43,791 --> 00:14:45,208 So the functionality of the 240 00:14:45,209 --> 00:14:47,032 program is pretty much done. 241 00:14:47,033 --> 00:14:50,328 What's left to do is, we need to make it 242 00:14:50,329 --> 00:14:53,496 a big pretty, the interface and the buttons and the 243 00:14:53,497 --> 00:14:56,574 spaces between the buttons and add some animations. 244 00:14:56,575 --> 00:14:59,816 So these are things that will help you to make some 245 00:14:59,817 --> 00:15:04,784 real world apps, which are mordern and nice to use 246 00:15:04,785 --> 00:15:07,936 from the users, and which you can actually sell. 247 00:15:07,937 --> 00:15:09,900 So let's do that in the next lectures.