Anvil: Web apps with nothing but Python Transcripts
Chapter: Consuming the API with Fitnessd Local app
Lecture: Creating the client app: Fitnessd Local

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this next chapter we're going to dive in
0:02 to building the client-side app.
0:04 We've already built the API
0:06 but what fun is an API if you can't use it?
0:09 So we're going to go ahead and build out a Python application.
0:12 And this is the first time that we're going to deviate
0:14 from just being on the web, on Anvil.
0:17 We're going to write this code locally, so that'll be
0:20 a little bit of a different experience, as well.
0:21 So that's going to bring us over to our Anvil repository
0:25 on our GitHub repository.
0:27 So let's go ahead and create a new folder
0:29 called source or something like that
0:32 and in here we'll create a folder
0:34 called fitnessed_client or something like that.
0:38 So what I want to do is create a virtual environment here
0:42 and then I'm going to open this in PyCharm.
0:44 You can use whatever editor you want.
0:45 You probably should use PyCharm or Visual Studio Code
0:48 with a Python extension, but you know
0:51 feel free, whatever makes you happy, use that.
0:53 Let's go over here
0:55 and we're going to create a virtual environment.
0:57 We'd normally type python3 -m venv
0:58 or something like that.
1:02 But I have a shortcut which I will show you.
1:07 Which is to do that but also to activate it
1:10 and then to make sure we have the latest tools
1:12 'cause these are almost always out of date.
1:14 Super big pet peeve of mine.
1:16 So I'm just going to type venv and let that run for a sec.
1:20 There we go. And now we get asked which Python on Windows
1:23 You ask where Python and it has the right one.
1:27 Okay, so we're done with the command line.
1:28 We're going to open this in PyCharm and
1:31 we're going to create our main little application
1:34 which is going to be fitnessed like that
1:38 Here and let's just double check in the terminal.
1:41 So there we go.
1:42 Check in the ternimal and notice that Pycharm
1:45 automatically found our Python again
1:49 So that's good, that means it's going to use this one to run it
1:52 So we can just run our little app
1:53 it's not going to do anything but just get everything set up.
1:56 We can use the hot keys
1:58 or this little play button
1:59 to run our app as many times as we want.
2:02 Okay, so our app is up and running.
2:04 We're going to need to talk to that end point.
2:08 So let me just write that down
2:10 and let's call this base_url over to Postman
2:15 and we want this, this much of it.
2:21 That's going to be this like so
2:23 and were just going to add on, like, authorize
2:25 or add_measurement or whatever.
2:28 Now I want to call this somehow and Python does have
2:31 URL load built in and some other stuff
2:34 that is not super duper amazing.
2:36 So what we're going to do is use some other libraries
2:38 and for that we're going to add a new
2:41 requirements.txt file
2:43 This is where we specify our requirements.
2:45 I'm going to start just using requests
2:48 and see PyCharm wants to install this for us
2:50 I could click here and it would do that
2:52 but I'll show you how to do it here as well
2:53 So I'll say pip install -r requirements
2:58 like that and that's going to install all the things that we need
3:02 and make PyCharm happy as well. Okay, so this
3:06 is the app that we're going to use
3:07 We're going to get started with that.
3:10 Now, What we need to do is build some kind of interaction
3:13 and I don't like having all the code just crammed in here
3:16 so I'm going to create some methods.
3:17 I'm going to create a method called main
3:20 like this and I'm going to use this convention called
3:24 I don't know what it's called but dunder name
3:27 convetion I'm going to call it, I'm going to say if the name
3:29 is main then we're going to run the main method.
3:32 I like this convention 'cause it lets me put
3:35 sort of work flow at the top and the utility functions below
3:39 in that order. So what are we going to do over here?
3:42 We need to get the user input that we're going to submit
3:46 So data = get_user_data()
3:50 their measurements and stuff like that.
3:52 And then we're going to somehow
3:55 result save measurement
3:57 It'll pass that data over like that
3:59 and then it'll print. Done. Super!
4:03 Well you can see there's two problems:
4:05 This doesn't exist, does it.
4:06 So let's go write that next
4:08 and we'll do the other one as well
4:10 just so everybody's happy.
4:16 If we put a little type annotation on here
4:17 not required, but very nice
4:19 we can get dictionary behaviors
4:22 and we can also get this to say
4:25 it returns a dictionary.
4:29 PyCharm is just saying you're not actually returning
4:31 anything from here so this is not probably how you
4:33 use it. This just 'cause we haven't written it yet.
4:36 We're going to go through a couple iterations on this.
4:38 First thing we're going to do is
4:39 just write a real simple version
4:41 that we type in everything every time
4:44 get our information in
4:46 and then submit it.
4:47 Then we're going to use another one
4:49 where we save the authentication
4:51 so we basically save the API key and the email
4:55 and then we're going to use that
4:56 and then finally we're going to actually build
4:58 a GUI version of this application
5:00 Don't get super excited, it's a simple GUI
5:02 but it'll still be really fun
5:03 to build a GUI Python application.
5:05 All right, so let's do iteration one where we just get this
5:08 maybe iteration zero
5:10 we just get the information from the user.
5:13 So what do we actually need to get back?
5:15 Well we got to ask them for their email and password.
5:19 So we can just start going down the line and put
5:22 What is your email? What is your password?
5:30 And we say rate or let's do this, we say print nothing
5:35 and then print entering measurement like so
5:37 it'll say resting heart rate, weight in pounds and
5:48 let's just say we're going to have the recorded date be
5:50 right now. PyCharm import that at the top
5:54 date, today, .isoformat there.
6:01 Like that. Okay.
6:02 So this will give us the ISO formatted string
6:04 and then we're just going to return this information as email
6:10 and so on. Okay let's just see that this is working
6:15 and down here we're just going to print
6:17 would have sent this to the server
6:21 Alright, run it again. Oh perfect!
6:24 what is your email?
6:25 michael@talkpython.fm and remember my fancy password
6:29 is also this. Let's enter our measurements: 71, 172
6:35 done. Look at that.
6:37 So, email, password, rate, weight
6:41 look those are as strings
6:42 that's not good.
6:43 We want to fix that.
6:45 Recorded though, it looks fine.
6:47 So over here let's do
6:49 and we're not going to put data validation here.
6:52 Obviously you should have it
6:53 and you can put it in your real one
6:55 but it's not really the point of this app.
6:56 We already did some validation earlier
6:58 so you can see about it.
7:00 So let's do this one more time.
7:02 Perfect. So, rate and weight are integers.
7:05 We've got our data