Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Add functionality to your app so the user can add books to the database and then view all books in the database.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back, Pythonistas.
0:00
We've got our database created, the books
from our CSV file added to our database,
0:02
and our app's initial work all finished.
0:06
In this video, we'll tackle
adding books to the database and
0:10
viewing all of the books in our database.
Pop down into our app function.
0:14
If the user chooses 1,
they want to add a book.
0:23
They'll need to input a title,
0:26
an author,
0:34
a date,
0:43
but remember,
we need this date in a specific format.
0:52
And we have a handy dandy
clean_date function already.
0:56
So if we look at our CSV file, we need
the dates to come in something like this.
1:01
So I'm actually just gonna copy that and
put it in here as an example.
1:06
That way,
the users know what we're looking for.
1:11
Colon and a space, and
then let's also do price = input('Price').
1:15
And I'm gonna do the exact
same thing because we also
1:22
need price to match what we already
have going on in the database.
1:27
Now let's test this out
with some edge cases.
1:40
That one clear, there we go.
1:44
And we wanna do for
a terminal output here, there we go.
1:46
We wanna do 1.
1:51
So title, I'm just gonna use nonsense.
1:53
Author's nonsense, published date,
let's do February.
1:56
I have to do February in my brain
to spell it right, 31, 2004.
2:02
Okay, that date does not exist.
2:07
So what happens?
2:09
Well, it just kinda takes it.
2:11
So we're definitely gonna need
to clean and check for errors.
2:13
What happens if I do 5.99
with the dollar sign?
2:17
Well, it looks like it took that, too.
2:20
So we're gonna need to make sure to clean
both the published date and the price.
2:24
So let's use our functions
we've already put in place.
2:29
And I'm gonna say date.
2:35
I now want this variable to be equal to
clean_date, and I'm gonna pass in date.
2:36
So essentially, what I'm saying
is I'm gonna grab your input,
2:41
save it to this variable.
2:46
Then I'm gonna clean your input and
2:47
I'm gonna set the same variable
equal to now the clean information.
2:50
Because I don't need
the not clean information.
2:55
I need the clean one.
2:59
And do the same thing down here,
price = clean_price(price).
3:01
And if this is messing with your
brain a bit, you can change this to
3:08
date_clean and price_clean if
that helps your brain out a bit.
3:14
Let's save, and I need to do a keyboard
interrupt, and run the file again.
3:22
And let me do this so
we can see our terminal a bit better.
3:29
Okay, so let's do 1,
choose nonsense, nonsense.
3:32
And let's try February 31, 2004.
3:36
Okay, sweet, I get an error.
3:43
It says, the day is out of range for
that month, which is correct.
3:45
It should give me an error, but
it also crashes the program,
3:50
which I don't like so much.
3:53
So we're going to need
to handle some errors.
3:55
Let me clear the console.
4:00
Actually, I'm gonna leave it here just so
we can see that it's a value error.
4:02
So let me go up into clean_date, and
4:08
we need to turn on a try block.
4:12
Let's do try/except/else, and
4:15
this is gonna pop up
a try/except/else block for me.
4:18
It's kind of a fun little quick enter.
4:22
And inside of our try block,
I'm going to move all three of these.
4:25
Cuz as we know, if we put in something
other than the correct information,
4:29
like let's say I can test it right now.
4:34
Let's say 1.
4:37
Let's say I did 01-13-2003.
4:40
If I do something like that,
a totally wrong date format,
4:44
it's also going to give me an error.
4:47
So I'm gonna move all three of these
4:50
inside our try block, tab, tab.
4:54
And if I scroll up a bit here,
you can see the datetime also
4:59
caused an error when we did
that February 31st date.
5:04
So let's move datetime also
inside of our try block here,
5:09
and let's give it
a return_date variable name.
5:15
And then else,
we can return return_date, awesome.
5:21
Now we need to handle the error it's
giving us, which is a ValueError.
5:27
And inside here, we need to give the user
a clear message as to what they did
5:34
wrong and how to fix it.
5:38
So let's do an input so that it'll pause,
and one, two, three, four, five, six.
5:40
That way, we can do multiple lines.
5:46
I'm gonna do \new line, and
I'm gonna make this kind
5:49
of obvious that it's an error, DATE ERROR.
5:53
And let's say the date format
6:01
should include a valid Month,
6:07
Day, Year from the past.
6:15
And then let's do \r and
let's give them an example.
6:21
And let's do January 13,
6:27
2003, and \r and
6:32
press enter to try again.
6:35
And then just to close it out,
I'm gonna add some stars
6:40
here to kind of help sandwich
it between these two lines.
6:45
Okay, now, another thing I'm going
to do is I'm going to add return,
6:52
and I'm not gonna put anything after it.
6:58
This is the same as doing return none.
7:01
You just return and it also returns
none if there's nothing there.
7:03
That way, Oops, can get rid of that now.
7:07
It's a little extra little piece of code,
there we go.
7:14
Now let's come back down here, okay.
7:19
Now where they're going to input the date,
we can create a while block.
7:23
Let's do date_error = True and
while date_error.
7:28
And this way,
we can tab these two pieces in here and
7:33
they'll continuously be asked for
7:38
a date until they provide
us with the correct date.
7:42
And we can check that by checking
the type of our variable here.
7:47
If it's a datetime object,
then they've correctly given us a date.
7:54
== datetime.date.
8:00
And if it is,
then we can set date_error = False.
8:06
And we're gonna do essentially
exact same thing with our price.
8:11
So since we're already in here,
let's start in here.
8:21
price_error = True.
8:25
And while price_error.
8:31
And then we can tab these over, tab, tab.
8:34
And if type of price is an integer,
8:42
then we can say price_error = False.
8:50
Perfect, now we need to pop
up into clean_price and
8:58
do the same try except block.
9:02
Oops, I think I went too far, there we go.
9:06
Okay, I'm going to take our price_float.
9:13
I'm gonna cut it, paste it.
9:18
Need this to be a ValueError.
9:22
And we're gonna give them,
I'm gonna copy this one and just edit it.
9:29
Ctrl+C, Ctrl+V, and
9:35
instead of DATE ERROR,
this is PRICE ERROR.
9:38
And the price should be a number
9:44
without a currency symbol.
9:50
And example, 10.99.
9:58
And then instead of pass,
we want to return.
10:03
And the reason I'm not putting this
integer switch in there as well is because
10:09
if this float works,
then it's already a float number,
10:13
so converting it to an integer
should automatically work.
10:16
So we have our clean_price and
we have our clean_date,
10:21
and we are using them
inside of our app function.
10:26
So let's give them a try.
10:31
Scroll up here, and let's do 1,
it's nonsense, nonsense.
10:41
Okay, let's see, if we do 01-13-2003,
I should get an error.
10:49
Date error, press enter to try again.
10:54
Enter, and we get to try again.
10:58
Okay, let's try February 31, 2004.
10:59
It is an error, try again.
11:06
Let's try January 22,
11:07
2021, awesome.
11:13
Okay, now let's try and
see if we can break our price to 5.99.
11:18
So it didn't like that, okay.
11:22
If we say it's twenty five, it's an error.
11:28
If we say it's 5.99, and
it takes it, awesome.
11:33
So that's all working for us now.
11:39
The last thing we need to do is to
actually add the book to our database.
11:42
So at the end, if our if choice for
11:46
adding a book, make sure you tab back over
so that you're outside of the while loop.
11:51
We're going to put in new_book = Book.
12:01
And the title is going to equal
our title variable above.
12:07
And the author is going to equal
the author variable above.
12:11
And the published_date is
going to equal the date above.
12:18
And the price is going to equal the price.
12:25
Awesome, now we can add
session.add(new_book),
12:30
and we can call session.commit, wonderful.
12:37
Save and
let's check this out in the console.
12:43
Let's scroll down here, and
I'm also gonna turn this back on.
12:45
And we can also turn this back on.
12:51
And I'm gonna actually have that
happen before our app.py goes.
12:54
Cool, save, and let's give this a run.
13:01
Okay, Python app.pi.
13:08
Let's add a new book.
13:12
Let's call it Jethro's Favorites,
and the author is my dog Jethro.
13:14
And the published date, January 22, 2021.
13:20
Price is 5.99.
13:27
Okay, Enter, and great,
it looks like everything went well.
13:31
Now let's exit, and
it was added to our database, fantastic.
13:36
There's just one small thing.
13:42
I'm going to add one small effect
to our code to help the user know
13:45
the book was added.
13:50
After the commit,
let's print out that the book was added.
13:51
Then at the top of the file,
we're going to import something,
13:57
lots of scrolling, called time.
14:03
And scroll back down.
14:10
And we're gonna call time.sleep(1.5).
14:17
And this is going to pause our program for
one and
14:24
a half seconds before continuing on.
14:27
Feel free to pause me and
play around with it.
14:31
Now that we've added quite a bit of code,
let's add it, commit it, and
14:35
push the changes up to our repo.
14:39
Here we go.
15:01
Pushing your code up every so
often is an excellent habit to get into.
15:04
If something were to happen and
you lost your code,
15:09
you would still have it up in your repo.
15:12
Next on the list is being able to see
all of the books in our database.
15:15
Let's try to create something nice and
readable.
15:19
For book in session.query(Book).
15:25
And I'm gonna do print cuz we
wanna see them in the console,
15:32
and f string, and let's do book.id.
15:37
Space pipe space book.title space
15:44
pipe space and book.author.
15:49
And if you wanna put the published date
and the price in there as well, go for it.
15:54
I'm gonna leave it with just the title and
author for now.
15:58
Then at the end, I'm gonna do an input,
16:03
input new line press enter
to return to the main menu.
16:08
And this is just so the user can take as
much time as they want scrolling through
16:16
the books before they automatically
get sent to the main menu again.
16:20
They have a little bit of control here.
16:25
Okay, so
let's see this in action in the console.
16:27
There we go.
16:39
That's what I wanna run, Python app.pi.
16:40
Let's run number 2, and awesome.
16:43
We see we have 11 books in the database,
their ID, the title, and
16:46
the author, perfect.
16:51
If I press Enter, I'm set back to
the main menu, press 5 to exit.
16:53
And that loop is still turned on, so
we'll see all of our books printed out.
16:58
Amazing, another task complete.
17:03
So go ahead and do another,
git add, git commit.
17:05
Put in able to view books,
and then git push.
17:18
And there it is, nice work, Pythonistas.
17:29
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up