1
00:00:00,820 --> 00:00:07,290
For this lecture, I'd like you to go to the resources of this lecture and download the three text files.

2
00:00:07,980 --> 00:00:13,050
Then in the Explorer here in your IDE, create a new folder.

3
00:00:15,270 --> 00:00:21,360
Give it a name such as quotes and then put those three files in that quotes folder.

4
00:00:22,260 --> 00:00:23,910
You can use drag and drop if you like.

5
00:00:24,150 --> 00:00:30,600
For example, I have this files somewhere and I'm going to just drag and drop them in this folder.

6
00:00:31,770 --> 00:00:35,040
So you see, that's now the files are in the quotes folder.

7
00:00:36,570 --> 00:00:41,310
So what we are going to do in this lecture is in the program that we have.

8
00:00:46,320 --> 00:00:51,240
We are going to write one of these three supported feelings for now.

9
00:00:51,510 --> 00:00:54,210
You can add more files later if you have some data.

10
00:00:54,810 --> 00:00:57,900
But this is what we have so far.

11
00:00:58,230 --> 00:01:06,720
And so when the user presses the "Enlighten me" button, we want to show one of the quotes that are inside

12
00:01:06,720 --> 00:01:09,030
the happy.txt file.

13
00:01:10,570 --> 00:01:12,700
So one of these lines.

14
00:01:14,710 --> 00:01:15,740
Some are long.

15
00:01:15,910 --> 00:01:16,810
Some are shorter.

16
00:01:20,420 --> 00:01:26,070
And so if the user enters sad, then you want to display here.

17
00:01:27,410 --> 00:01:33,890
In this area, one of the lines all of the sad.txt file, those are also our quotes.

18
00:01:35,600 --> 00:01:36,650
And so you get the idea.

19
00:01:37,250 --> 00:01:45,770
And if the user and there's an unknown feeling such as, let's say, excited, and when they press the

20
00:01:45,770 --> 00:01:48,130
button, you want to show: "Try another feeling".

21
00:01:48,290 --> 00:01:51,040
Because you cannot have all possible feelings

22
00:01:51,050 --> 00:01:52,050
covered.

23
00:01:52,370 --> 00:01:54,620
So this is what we are going to do in this lecture.

24
00:01:59,340 --> 00:01:59,530
Main.py.

25
00:01:59,900 --> 00:02:00,480
So

26
00:02:01,340 --> 00:02:03,620
let's start from the button.

27
00:02:03,710 --> 00:02:09,920
Try to locate the "Enlighten me" button, or at least that's how my button is called.

28
00:02:10,190 --> 00:02:14,960
So Button, that button is part of the LoginScreenSuccess page.

29
00:02:15,770 --> 00:02:19,400
And we don't have an on_press attributes yet.

30
00:02:20,030 --> 00:02:21,620
So on_press, what do you want to do?

31
00:02:21,620 --> 00:02:24,320
You want to execute a method

32
00:02:27,360 --> 00:02:35,170
f the logging screen success class which I have it right here, so that logging screen success, it

33
00:02:35,170 --> 00:02:41,250
already has a method logout which is executed when this button is pressed, the logout button.

34
00:02:42,100 --> 00:02:49,840
Now we want to implement the get quote method. Quote(self) as usual.

35
00:02:50,290 --> 00:02:57,490
And so this method now has to communicate the value that the user entered in the text input.

36
00:02:57,790 --> 00:03:00,600
So which is route.ids.

37
00:03:00,700 --> 00:03:04,450
We don't have an idea for text input.

38
00:03:04,450 --> 00:03:06,880
So let's put an ID.

39
00:03:07,720 --> 00:03:10,470
So this is a text input where the user enters the feeling.

40
00:03:10,990 --> 00:03:13,620
And so let's put feeling there.

41
00:03:14,020 --> 00:03:19,570
Ids.feeling, we want to get the text of that text input.

42
00:03:19,930 --> 00:03:21,680
So that feeling will go

43
00:03:21,700 --> 00:03:22,210
here.

44
00:03:24,760 --> 00:03:29,290
Let's just call it feel not to be confused with that feeling word.

45
00:03:30,070 --> 00:03:37,180
And the first thing we might want to do is let's see if we are accessing the feeling texts correctly.

46
00:03:37,870 --> 00:03:44,000
So print feel. Make sure you save the

47
00:03:45,090 --> 00:03:49,350
designe.kv file execute, login.

48
00:03:49,480 --> 00:03:55,510
I was able to log in because I have this username I created earlier when I signed up.

49
00:03:56,260 --> 00:04:00,370
I created one username, which is an empty string and empty string for the password.

50
00:04:00,670 --> 00:04:05,050
So basically it's a user with empty string empty strung as username and password, which works because

51
00:04:05,050 --> 00:04:09,130
I haven't put any jurisdictions or the username and password.

52
00:04:10,130 --> 00:04:14,840
So this is just for simplicity when you try and when you test the application.

53
00:04:15,500 --> 00:04:16,010
Happy.

54
00:04:16,100 --> 00:04:16,970
Enlighten me.

55
00:04:17,050 --> 00:04:18,260
And so we get the happy

56
00:04:19,970 --> 00:04:20,660
string there.

57
00:04:21,080 --> 00:04:26,030
And if that is with a H, you're going to get "Happy".

58
00:04:26,600 --> 00:04:34,610
So the first thing we might want to do is to change the feel into lower case.

59
00:04:34,970 --> 00:04:43,080
So even if the user enters happy with a capital H, we are going to convert that to all lower case

60
00:04:44,720 --> 00:04:50,210
so that we can compare now with the names in the text files, with the file names.

61
00:04:50,660 --> 00:04:52,040
So let's now see what's

62
00:04:55,400 --> 00:04:56,600
available, feelings we have.

63
00:04:56,990 --> 00:05:10,550
So I'm assigning a variable available_feelings and I'm going to use a globe module to get a list of file

64
00:05:10,550 --> 00:05:11,030
names.

65
00:05:15,970 --> 00:05:17,890
This is what this is going to give me

66
00:05:21,340 --> 00:05:24,590
an error, of course, because I didn't import Globe.

67
00:05:31,550 --> 00:05:34,940
So it gives you a list with all the files.

68
00:05:35,780 --> 00:05:38,570
This is available, all the available feelings.

69
00:05:40,560 --> 00:05:45,920
So we are here in the get_quote methods. 
Now the available feelings list

70
00:05:45,990 --> 00:05:48,770
now looks like a list of file paths.

71
00:05:49,320 --> 00:05:54,720
What I want to do is I want to extract only the names of these files.

72
00:05:55,290 --> 00:05:56,640
Only the sad part.

73
00:05:57,470 --> 00:06:01,650
A way to do that is by using

74
00:06:05,220 --> 00:06:07,770
the path object of the pathlib library.

75
00:06:07,800 --> 00:06:09,270
You don't have to install this library.

76
00:06:09,750 --> 00:06:10,770
It comes with Python.

77
00:06:10,920 --> 00:06:13,470
It's standard library using path.

78
00:06:13,590 --> 00:06:24,000
If you give path a path such as quotes/sad.txt and then extract the stem out of that, it will give

79
00:06:24,000 --> 00:06:30,210
you the name of the file without the extension, without the other part of the path.

80
00:06:32,040 --> 00:06:37,320
In fact, you have many methods of path, and stem is one of them.

81
00:06:37,590 --> 00:06:39,330
So you can get the suffix.

82
00:06:39,660 --> 00:06:42,450
The name of the file, including the extension.

83
00:06:45,990 --> 00:06:55,410
Such as that and so on, and so what we can do here now is we need to make use of that stem property

84
00:06:55,890 --> 00:06:59,460
and we need to apply that to all the file names.

85
00:07:00,130 --> 00:07:02,940
Well, to do that, so you can use a list comprehension.

86
00:07:03,440 --> 00:07:07,050
So available_feelings equal to path,

87
00:07:08,220 --> 00:07:15,030
let's say file name for file name in available_feelings.

88
00:07:16,600 --> 00:07:21,770
And here I would like to split the line into multiple lines.

89
00:07:24,290 --> 00:07:26,150
Since this is inside square brackets,

90
00:07:26,180 --> 00:07:27,710
I am allowed to split the line.

91
00:07:28,490 --> 00:07:31,460
And so we wanted the stem out of that.

92
00:07:34,640 --> 00:07:40,810
And then we might want to try a print in here off available feelings.

93
00:07:41,230 --> 00:07:51,610
Also make sure to import from pathlib import path and then execute the app.

94
00:07:57,640 --> 00:08:01,960
And I got an error when I pressed the bottom, string object has no attributes.

95
00:08:02,120 --> 00:08:06,710
Yeah, of course, because I did in here instead of putting stem

96
00:08:06,730 --> 00:08:11,060
after the path object, I put it just next to the file name.

97
00:08:11,180 --> 00:08:11,740
File name

98
00:08:11,750 --> 00:08:12,460
is a strig.

99
00:08:12,950 --> 00:08:14,940
So that's why you get:

100
00:08:14,960 --> 00:08:16,550
"str" has no stem.

101
00:08:17,660 --> 00:08:20,570
So let's put it in here and try again.

102
00:08:22,790 --> 00:08:23,450
Enlighten me.

103
00:08:23,840 --> 00:08:25,130
So happy said unlove.

104
00:08:25,190 --> 00:08:26,180
That's what we want.

105
00:08:26,240 --> 00:08:29,690
Now we have of the available feelings in a list.

106
00:08:33,300 --> 00:08:40,710
What can I do now? I can say if feel in available_feelings.

107
00:08:43,640 --> 00:08:47,750
So if that feeling is in the available_feelings,

108
00:08:50,490 --> 00:08:57,420
then what we want to do next is we want to open that file that has that feeling, that has the quote

109
00:08:57,720 --> 00:08:58,740
for that feeling.

110
00:08:59,220 --> 00:09:00,540
So let's use an open.

111
00:09:00,960 --> 00:09:02,760
I'm going to use string formatting here.

112
00:09:04,350 --> 00:09:14,820
So quotes, I'm going to open this file path, which has this feel.txt as file.

113
00:09:16,080 --> 00:09:17,610
So what am I doing here?

114
00:09:17,670 --> 00:09:24,420
Well, that thing what that is going to give me is let's say the feel that the user entered was happy.

115
00:09:24,900 --> 00:09:25,200
Right.

116
00:09:25,590 --> 00:09:27,510
So we get that valuable.

117
00:09:27,600 --> 00:09:28,800
We had it has a value.

118
00:09:29,250 --> 00:09:33,070
And if we get now this string

119
00:09:36,620 --> 00:09:41,150
what is going to give us is the file path for that feeling.

120
00:09:41,570 --> 00:09:50,600
So this expression here in these braces is going to be replaced with the value of the variable of the

121
00:09:50,600 --> 00:09:51,830
feel variable.

122
00:09:53,030 --> 00:09:55,760
So I hope that it's clear.

123
00:09:56,270 --> 00:10:04,310
And then we want to get all the quotes, all that file using the readlines method.

124
00:10:05,840 --> 00:10:08,990
So that will give us a list of all the quotes.

125
00:10:09,920 --> 00:10:16,500
So let me unindent and print the quotes to see what we've got this far.

126
00:10:18,260 --> 00:10:18,620
Happy.

127
00:10:18,620 --> 00:10:19,580
The quotes for happy.

128
00:10:19,940 --> 00:10:21,320
And this is a list.

129
00:10:21,500 --> 00:10:25,030
So as I told you, "Happiness is what you think and what you say".

130
00:10:25,070 --> 00:10:28,760
And so one and then it was told by

131
00:10:30,870 --> 00:10:31,870
Mahatma Gandhi.

132
00:10:32,400 --> 00:10:40,020
And then you have a coma and there is where the other quote starts. So one, we have a list of multiple

133
00:10:40,260 --> 00:10:40,980
strings.

134
00:10:42,330 --> 00:10:44,070
Each string representing a quote.

135
00:10:44,430 --> 00:10:46,560
So what can we do with these quotes now?

136
00:10:49,220 --> 00:10:58,820
What we can do is we can get the self.ids dot the ID of this label where we want to show

137
00:10:58,820 --> 00:11:02,540
the quote, which is just under the "Enlighten me" button.

138
00:11:03,620 --> 00:11:16,430
So the ID quote, quote. Quote.text, so the text of that label will be a random choice

139
00:11:17,780 --> 00:11:17,960
of

140
00:11:18,530 --> 00:11:20,120
the quotes list.

141
00:11:20,690 --> 00:11:22,640
So what does this method do?

142
00:11:22,820 --> 00:11:23,150
Is

143
00:11:27,440 --> 00:11:29,900
it gets a list as input.

144
00:11:31,260 --> 00:11:34,010
So one, two, three.

145
00:11:34,820 --> 00:11:38,660
And it returns a random item out of that list.

146
00:11:38,730 --> 00:11:42,890
Anytime you execute it. Say just pure random.

147
00:11:45,060 --> 00:11:52,170
So, therefore, we're going to get one of these strings, one of these quotes, and that quote is going

148
00:11:52,170 --> 00:11:55,110
to be set as the text of the label.

149
00:11:56,820 --> 00:11:57,750
So that's how it works.

150
00:11:58,380 --> 00:12:00,960
And let's make sure we import

151
00:12:03,660 --> 00:12:04,200
random.

152
00:12:05,820 --> 00:12:09,240
And we are ready to execute the app.

153
00:12:13,010 --> 00:12:13,520
Login.

154
00:12:13,940 --> 00:12:15,790
Happy, enlighten me.

155
00:12:18,580 --> 00:12:25,170
And we got this error, which is a quite common attribute error, 'super' has no attribute

156
00:12:25,280 --> 00:12:25,640
__getattri__.

157
00:12:26,200 --> 00:12:33,760
This happens when the Python file doesn't that ID, the quotes ID in this case.

158
00:12:35,620 --> 00:12:38,950
So you can see that the error happened in line 61.

159
00:12:39,370 --> 00:12:42,370
So that line in there is not working well.

160
00:12:42,970 --> 00:12:50,800
That is, I think because I didn't save the kivy file when I entered, when I updated the ID of the label

161
00:12:50,800 --> 00:12:51,070
here.

162
00:12:51,400 --> 00:12:53,890
So I wrote an ID, but I didn't save the kv file.

163
00:12:55,390 --> 00:12:56,410
So let me try again.

164
00:12:59,440 --> 00:13:03,130
I'm happy these errors emerge because you learn how to fix them.

165
00:13:04,900 --> 00:13:05,850
Enlighten me, and yeah.

166
00:13:06,600 --> 00:13:08,710
So we get the label displayed in here.

167
00:13:10,250 --> 00:13:16,760
When you press again, that random.choice function is going to get another random choice

168
00:13:16,760 --> 00:13:17,690
out of the list.

169
00:13:20,270 --> 00:13:27,410
Sometimes I suppose it may display the same quote, which is fine I think but there are ways to make

170
00:13:27,410 --> 00:13:30,140
it display a different quote from the previous one.

171
00:13:30,770 --> 00:13:32,660
But this is working quite good I think.

172
00:13:32,670 --> 00:13:37,370
Except of the case when

173
00:13:38,640 --> 00:13:44,190
there is a long quote, and it's out of the borders of the app window.

174
00:13:44,820 --> 00:13:51,480
So this is a problem, but nothing to worry about because we are going to implement a scroll view later

175
00:13:51,480 --> 00:13:56,060
on, which will squeeze this label and it will make it flexible.

176
00:13:57,740 --> 00:14:03,150
By resizing the label, depending on the widget on the app size.

177
00:14:03,440 --> 00:14:10,820
For now, let me let me close that and finish the if else block,

178
00:14:10,820 --> 00:14:11,640
so the ids.quote.

179
00:14:11,730 --> 00:14:12,410
text equals to

180
00:14:12,750 --> 00:14:15,530
"Try another

181
00:14:18,630 --> 00:14:19,230
feeling".

182
00:14:21,200 --> 00:14:25,490
So let me try one more time, and we are done with this video.

183
00:14:26,910 --> 00:14:27,960
So that is working.

184
00:14:28,860 --> 00:14:30,040
That is also working.

185
00:14:33,850 --> 00:14:35,440
And I feel pretty.

186
00:14:35,590 --> 00:14:36,380
Try another feeling.

187
00:14:36,550 --> 00:14:37,630
We don't support that.

188
00:14:38,500 --> 00:14:43,600
And this is how we make an app that picks a quote randomly.

189
00:14:43,840 --> 00:14:46,810
So the functionality of the program is pretty much done.

190
00:14:47,110 --> 00:14:53,980
What's left to do is we need to make it a bit pretty, the interface and the buttons, and the spaces

191
00:14:53,980 --> 00:14:56,440
between the buttons and add some animations.

192
00:14:56,480 --> 00:15:04,270
So these are things that will help you to make some real world apps which are modern and nice to

193
00:15:04,270 --> 00:15:05,590
use from the users.

194
00:15:05,800 --> 00:15:07,690
And to which you can actually sell.

195
00:15:08,140 --> 00:15:09,730
So let's do that in the next lecture's.

