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