WEBVTT

0
00:00.300 --> 00:04.680
We are now within sight of the finish line. We're onto the last step.

1
00:04.710 --> 00:09.240
So this is the solution for step 4. And in step 4

2
00:09.270 --> 00:14.270
we want to be able to improve our program so that the words where the user

3
00:14.910 --> 00:18.780
clicks the checkmark to means they already know that word.

4
00:18.810 --> 00:23.040
They know what it means and they don't wanna see it again in this list.

5
00:23.580 --> 00:28.580
So what we want to be able to do is to remove the words that the user knows from

6
00:29.010 --> 00:33.510
this dictionary of words to_learn. And to do that,

7
00:33.630 --> 00:38.610
we have to change this command. When the user clicks on the unknown button,

8
00:38.880 --> 00:41.340
we're going to simply just give them the next card,

9
00:41.370 --> 00:44.700
which is a random word from the list of words.

10
00:45.210 --> 00:47.850
But when they use it clicks on the known button,

11
00:48.000 --> 00:52.440
instead of clicking on next card, we're going to call a different function

12
00:52.710 --> 00:56.610
which I'm going to call is_known. Now is_known

13
00:56.640 --> 00:58.050
is going to be a function

14
00:58.110 --> 01:03.110
that's going to remove the current card from the cards that are in the list of

15
01:06.300 --> 01:07.170
words to learn.

16
01:07.830 --> 01:12.120
So what we're going to do is we're going to get hold of our list to learn,

17
01:13.470 --> 01:17.070
and then we're going to call the remove method.

18
01:17.610 --> 01:20.460
And then we're going to remove a particular element from it.

19
01:20.880 --> 01:24.240
And that happens to be the current card.

20
01:25.410 --> 01:28.770
So remember, the current card is a dictionary

21
01:29.040 --> 01:33.480
which we got by randomly choosing from our list to learn.

22
01:34.080 --> 01:36.990
So now, when the user says is_known,

23
01:37.320 --> 01:41.670
then the current card is going to be removed from the list of words to learn.

24
01:42.120 --> 01:43.560
And then after we've done that,

25
01:43.920 --> 01:48.920
then we're going to call next card from this particular method. At the moment,

26
01:49.440 --> 01:53.250
if we run this code and I just keep clicking the check mark,

27
01:53.670 --> 01:58.670
eventually, we're going to have a very small list of words that we're going to

28
01:59.370 --> 02:01.260
pick from. In fact,

29
02:01.290 --> 02:06.290
if I go ahead and print the length of my list, to_learn,

30
02:08.190 --> 02:13.190
you can see that the first time I run this code and I click on the is_known

31
02:14.640 --> 02:19.020
button, we have a hundred words. But if I keep clicking on this,

32
02:19.050 --> 02:21.090
then you'll see that every single time,

33
02:21.090 --> 02:25.050
I'm just reducing that list because I'm saying,

34
02:25.080 --> 02:27.660
I already know what this word compris means

35
02:28.110 --> 02:31.950
and that word is now taken out of my list of words to learn.

36
02:32.250 --> 02:36.630
So now that list is smaller by one entry.

37
02:37.530 --> 02:41.670
Now this works right now, but if I rerun the app,

38
02:41.700 --> 02:45.540
then you can see it goes back right to the beginning

39
02:45.750 --> 02:47.550
and it has a hundred words again.

40
02:48.150 --> 02:52.950
So in order to keep hold of the words that I still need to learn,

41
02:53.280 --> 02:57.540
I have to save this list to a new permanent file

42
02:57.840 --> 03:01.000
each time the user clicks on this is_known button.

43
03:01.780 --> 03:06.220
So the way that I'm going to do that is by using pandas again.

44
03:06.790 --> 03:10.900
I'm going to use pandas to create a new data frame.

45
03:11.530 --> 03:15.970
And that data frame is going to be created from our list to_learn.

46
03:17.230 --> 03:19.540
Now, I'm going to save this as our data,

47
03:20.170 --> 03:25.170
and then I can say data.to_csv in order to save it as a CSV file.

48
03:26.350 --> 03:31.350
So I'll call this words_to_learn.csv.

49
03:32.860 --> 03:37.860
So now, if I press my check mark

50
03:38.980 --> 03:41.740
and I take a look within my files,

51
03:41.770 --> 03:44.980
you can see that words_to_learn.csv is created.

52
03:45.490 --> 03:50.380
And at the moment it's got 89 entries. But if I keep clicking this a few times,

53
03:50.620 --> 03:52.570
it's going to reduce each time.

54
03:52.960 --> 03:57.250
So this is now a permanent storage of all the words that I have yet to learn.

55
03:57.940 --> 04:01.510
But I don't really want it randomly in the middle of my project.

56
04:01.750 --> 04:05.170
I want it to be saved alongside my french_words.csv.

57
04:05.740 --> 04:08.200
So I have to change this to a file path

58
04:08.230 --> 04:11.110
which is going to be data/words_to_learn.csv.

59
04:11.620 --> 04:16.620
So let's go ahead and delete this file and do refactor.

60
04:17.110 --> 04:18.760
And then we're going to run this again.

61
04:18.880 --> 04:22.630
And next time you'll see that it appears in the right place.

62
04:23.530 --> 04:26.590
Now, in addition to saving it to CSV,

63
04:26.620 --> 04:31.620
we also have to read from that CSV because instead of using the words from our

64
04:32.680 --> 04:35.860
original list which is our french_words.csv,

65
04:36.400 --> 04:41.400
I actually want to be able to read from my words_to_learn.csv instead,

66
04:42.100 --> 04:45.460
because that way, every single time I rerun the app,

67
04:45.760 --> 04:49.570
it's always going to give me all the words that I've yet to learn.

68
04:50.020 --> 04:53.320
So currently we're on 87. If I run this again,

69
04:53.560 --> 04:55.840
you can see that the first time I click on this,

70
04:55.930 --> 04:59.560
it's going to go to 86 instead of going back to 100.

71
05:00.220 --> 05:01.750
But there is a problem here though

72
05:02.080 --> 05:07.080
because if I delete this file, words_to_learn.csv, and I go ahead and run this, the first

73
05:09.250 --> 05:13.540
time this runs it's going to crash and we have that familiar file

74
05:13.540 --> 05:16.810
not found error. So we know how to deal with that.

75
05:16.840 --> 05:21.280
We need to catch this exception. The exception occurs right here.

76
05:21.940 --> 05:26.940
It happens when we try to get hold of this piece of data

77
05:27.640 --> 05:32.290
which may or may not exist. So if it does not exist,

78
05:32.470 --> 05:36.820
so we're going to catch this except file not found error. Well,

79
05:36.820 --> 05:40.390
in that case, we're going to be using the original data

80
05:41.050 --> 05:45.970
which comes from the french_words.csv. So we'll say pandas.

81
05:46.000 --> 05:47.050
read_csv

82
05:47.140 --> 05:52.090
and then we're going to read this file, data/french_words.csv.

83
05:52.660 --> 05:56.890
So this is always going to be there because it's preloaded with our project.

84
05:57.500 --> 06:01.070
But if we do manage to find our words_to_learn.csv,

85
06:01.130 --> 06:05.810
then that means this program has been run before and we've removed some of the

86
06:05.810 --> 06:10.610
words we already know. So we can catch that with an else statement and we can say,

87
06:10.610 --> 06:11.900
well, in this case,

88
06:12.260 --> 06:16.130
we're going to set a global variable called to_learn

89
06:16.310 --> 06:18.770
which starts out as an empty dictionary.

90
06:19.250 --> 06:23.780
And we set it to the data that comes from this CSV.

91
06:24.620 --> 06:27.620
However, if that file was not found,

92
06:27.950 --> 06:32.950
then this to_learn is going to be set to the original data.to_dict

93
06:34.790 --> 06:39.410
and we're going to orient according to records as well.

94
06:43.510 --> 06:43.660
<v 1>Okay.</v>

95
06:43.660 --> 06:47.590
<v 0>Now, when we run our code, you can see we have no errors.</v>

96
06:48.010 --> 06:49.870
And the first time that it runs,

97
06:49.900 --> 06:54.310
it's going to pick from our french_words.csv. Now,

98
06:54.310 --> 06:58.480
once I've started saying I've learned this, I've learned this,

99
06:58.480 --> 07:03.130
I've learned this, then it's going to generate a words_to_learn.csv.

100
07:03.580 --> 07:05.560
And the next time I run this code,

101
07:05.860 --> 07:10.860
it's gonna start using that words_to_learn.csv and start where I left off.

102
07:11.800 --> 07:15.430
Now, there is one slight weird bug to this.

103
07:15.820 --> 07:19.060
When you take a look at the words_to_learn.csv,

104
07:19.750 --> 07:23.590
it adds the record number to the first column

105
07:23.950 --> 07:27.850
every single time I run the code. So if I run this code again,

106
07:28.210 --> 07:31.960
you can see now I have three columns of all of the records.

107
07:32.500 --> 07:37.330
So basically when we read from this particular CSV,

108
07:37.780 --> 07:42.580
we generate a data frame. And when I read from that data frame,

109
07:42.940 --> 07:47.800
so let's delete this and run our code again,

110
07:49.090 --> 07:53.860
you can see that pandas is automatically added in these record numbers.

111
07:54.280 --> 07:59.280
So that gets then added back into our words_to_learn.csv when it saves it into

112
08:02.860 --> 08:06.790
the file. If we want to get rid of these record numbers,

113
08:07.000 --> 08:10.330
all we have to do is when we save it to the CSV,

114
08:10.870 --> 08:15.870
we can set a property called index to false. This way

115
08:16.150 --> 08:20.800
it just doesn't add the index numbers to our newly created list

116
08:21.160 --> 08:22.990
and now if I rerun this code,

117
08:23.440 --> 08:26.260
you can see that our words_to_learn.csv

118
08:26.560 --> 08:31.560
doesn't actually include an index. It only includes the actual records or

119
08:31.660 --> 08:34.480
the actual words and their English translations.

120
08:36.700 --> 08:37.060
That's

121
08:37.060 --> 08:41.770
<v 0>basically it. That's all the solutions to our capstone project.</v>

122
08:42.310 --> 08:47.140
And you've now built a fully fleshed flashcard that you can use to learn

123
08:47.140 --> 08:48.160
languages.

124
08:48.190 --> 08:52.370
So you could have the front with whatever language you want, and the back with

125
08:52.440 --> 08:56.790
English translations, or you can use it to learn Programming facts.

126
08:56.790 --> 09:01.500
You could have the front with a Programming word and the back as the meaning of

127
09:01.500 --> 09:02.520
the Programming word.

128
09:02.780 --> 09:05.540
<v 2>You could use it to study History or Geography.</v>

129
09:06.050 --> 09:10.520
<v 0>And this program is going to help you manage all the things that you don't</v>

130
09:10.520 --> 09:15.520
remember and keep showing it to you until you do. Have fun modifying this

131
09:16.520 --> 09:20.300
flashcard app and I hope it will be helpful in your studies as well.

132
09:21.170 --> 09:24.080
If you've built something particularly interesting, again,

133
09:24.140 --> 09:28.370
be sure to share with us in the Q/A so that we can all admire and

134
09:28.370 --> 09:30.350
congratulate you on your hard work.