For this lecture, I'd like you to go to the
resources of this lecture and download the three text files.
Then in the Explorer here in your IDE, create a
new folder, give it a name such as quotes and
then put those three files, in that quotes folder.
You can use drag and drop if you like.
For example, I have these files somewhere and I'm going
to just drag and drop them in this folder.
So you see that, now the files are in the quotes folder.
So what we are going to do in this lecture
is, in the program that we have,
[Author Typing]
we are going to, write one of these three supported fieldings for now.
You can add more files later if you have some data.
But, this is what we have so far.
And so when the user presses the Enlighten
me button, we want to show one of
the quotes that are inside the happy.txt file.
So one of these lines,
[No Audio]
some are long, some are shorter.
[No Audio]
And so if the user enters sad, then you
want to display here, in this area, one of
the lines of the sad.txt file.
Those also are quotes, and so you get the idea.
And if the user enters an unknown feeling,
such as let's say, excited, and when they press a
button you want to show, try another felling, because
you cannot have all possible fellings there covered.
So this is what we are going to do in this lecture.
[No Audio]
main.py, so let's start from the button.
Try to locate the Enlighten me button, or at
least that's how my button is called. So Button,
that button is part of the LoginScreenSuccess
page. And we don't have an on_press attribute yet.
So on_press, what you want to do is you
want to execute a method,
[Author Typing]
of the LoginScreenSuccess class, which I have it right here.
So that LoginScreenSuccess, it already has
a method log_out, which is executed when this
Button is pressed, the Logout Button.
Now we want to implement the get_quote method,
quote self, as usual until this method now has
to communicate, the value that the user entered in
the TextInput, so which is root.ids.
we don't have an id for TextInputs, so let's put an id.
So this is the TextInput where the user entered
the feeling. And so let's put feeling there, ids.feeling,
we want to get the text of that
TextInput, so that feeling will go here.
[No Audio]
Let's just call it feel, not to
be confused with the feeling root.
And, the first thing we might want to do is
let's see if we are accessing the felling text correctly.
So print feel, and make sure you save the design.kv file,
execute Login, I was able to Login because, I have this username I created earlier.
When I signed up, I created one username which is
an empty string, and an empty string for the Password.
So basically it's a user with empty string, empty
string as username and password, which works because I
haven't put any restrictions on the Username and Password.
So this is just for simplicity.
When you try, when you test
the application, happy, Enlighten me
and so we get the happy, string there.
And if that is with a big
H, you're going to get Happy.
So the first thing we might want to
do is, to, change the feel into lowercase.
So even if the user enters Happy with a capital
H, we are going to convert that to all lowercase.
So that we can compare now with the names in
the text files, with the file names.
So let's now see what,
[Author Typing]
available_feelings we have.
So I'm assigning a variable available_feelings, and
I'm going to use the glob module to
[Author Typing]
get a list of file names.
[Author Typing]
This is what this is going to give me
[Author Typing]
an error, of course, because I didn't import glob.
[Author Typing]
It gives you a list with all the files.
This is the available, all the available feelings.
So we are here in the get_quote method.
Now, the available_feelings, atlest now looks
like a list of file paths.
What I want to do is, I want
to extract only the names of these files.
So only the sad part.
A way to do that, is by using
[Author Typing]
the path object of the pathlib library.
You don't have to install this
library, it comes with Python,
it's standard library. Using Path,
if you give Path, a path such as quotes/sad.txt,
and then extract the stem out of that, it
will give you the name of the file without the
extension, without the other part of the Path.
In fact, you have many methods of
path and stem is one of them.
So you can get the suffix, the name of the
file, including the extension,
[Author Typing]
such as that and so on.
And, so what we can do here now is, we need
to make use of that stem property and we need to
apply that to all the file names. To do that,
so you can use a list comprehension.
So available_feelings equal to Path.
Let's say filename for
filename in available_feelings.
And here, I would like to
split the line into multiple lines.
Since this is inside square brackets,
I'm allowed to split the line.
And so we want the stem out of that.
[Author Typing]
Then we might want to try a
print in here, of available_feelings.
Also make sure to import, from pathlib
import Path, and then execute the app.
[No Audio]
And I got an error when I press the button,
string object has no attribute stem.
Yeah, of course, because I did here, instead
of putting stem after the Path object, I
put it just next to the filename,
filename is a string.
So that's why you get str has no stem.
So let's put it in here and try again.
Enlighten me, so happy, sad, unloved, that's what we want.
Now we have the available feelings in a list.
[Author Typing]
What can I do now? I can say, if feel in available_feelings.
[No Audio]
So if that feeling is in the available_feelings,
[No Audio]
then, what we want to do next, is we
want to open that file, that has that feeling,
that has the quote for that feeling.
So let's use an open I'm going
to use string formatting here f.
So quotes, I'm going to open this file
path, which has this feel.txt as file.
So what am I doing here?
Well, that thing, what that is going to give me is
let's say the field, that the user entered was happy, right?
So we get that variable, it has a value.
And if we get now this string,
[No Audio]
what is going to give us, is the file path for that felling.
So this expression here in these braces, is
going to be replaced with the value of
that variable of the field variable.
So, I hope that is clear.
And then we want to get all the quotes,
of that file, using the readlines method.
So that will give us a list of all the quotes.
So let me unindent and print the quotes,
to see what we've got this for. happy,
the quotes were happy, and this is a list.
So as I told you, happiness is when you what
you think, and what you say, and so on.
And then it was told by, Mahatma Gandhi and
then you have a comma, and there is where
the other quote starts. And so on,
we have a list of multiple
strings. Each string representing a quote.
So what can we do with this quote, now?
What we can do is, we can get the self.ids.,
the id of this label where we want to show
the quote, which is just under the Enlighten me button.
So the id quotes, quote, quote.text,
so the text of that label will
be, random.choice of the quotes list.
So what does this method do is,
[Author Typing]
it gets a list as input.
So 1, 2, 3, and it returns a
random item out of that list, anytime you
execute it, say just pure random.
So therefore we're going to get one of these strings,
one of these quotes, and that quote is going to
be set as a text of the label.
So that's how it works.
And let's make sure we import random and
we are ready to execute the app.
[No Audio]
Login, happy, Enlighten me
[No Audio]
and we got this error, which is quite common AttributeError.
super has no attribute, __getattr__
And this happens, when the Python file doesn't find
that id, the quote id, in this case.
So you can see that the error happened in line 61.
So that line in there is not working.
Well, that is I think because I didn't
save the kivy file when I entered, when
I updated the id of the Label here.
So I wrote an id, by id, and save the kivy file.
So let me try again.
[No Audio]
I'm happy these errors emerge because
you learn how to fix them.
Enlighten me, and yeah, so we get the label displayed in here.
When you press again, that random.choice function is going
to get another random.choice out of the list.
[No Audio]
Sometimes I suppose it may display the same code, which
is fine, I think, but there are ways to make
it display a different quote from the previous one.
But this is working quite good, I think.
Expect of the case, when there is a long quote
and it's out of the borders of the app window.
So this is a problem, but nothing to worry
about, because we are going to implement a scroll
view later on, which will squeeze this label and
it will make it flexible, by resizing the label,
depending on the widget, on the app size.
For now, let me close that and finish the if else
block, self.ids.quote.text = To Try another feeling.
So let me try one more time
and we are done with this video.
So that is working.
That is also working,
[No Audio]
and I feel pretty oh, Try another feeling,
we don't support that.
And this is how we make an app, that picks a quote randomly.
So the functionality of the
program is pretty much done.
What's left to do is, we need to make it
a big pretty, the interface and the buttons and the
spaces between the buttons and add some animations.
So these are things that will help you to make some
real world apps, which are mordern and nice to use
from the users, and which you can actually sell.
So let's do that in the next lectures.