#100DaysOfCode in Python Transcripts
Chapter: Days 43-45: Consuming HTTP services
Lecture: Demo: Building the program structure
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So it's time to get started writing our search app that's going to talk to the movie search API. Now over here in our repository,
0:07
I've got Day 10 to 12 selected and I've opened up the terminal here. We want to open this in PyCharm. You don't have to use PyCharm,
0:17
but I'm going to open in in PyCharm. I'm going to go through a couple of steps. Whenever you depend upon external packages,
0:22
it's a good idea to have a virtual environment. In order to create one of those over here, let's call this app, let's make a folder really quick.
0:31
So call this Movie Search. And go in here. Then, I'm going to create a virtual environment here. If we call it .env or venv,
0:41
then PyCharm will automatically detect it and use it, so might as well call it that. Now open this in PyCharm,
0:47
can't seen anything because dot creates hidden files on Mac and Linux, but we're going to open this
0:53
on Mac and Linux, you have to say file, open, directory and Mac, you can just drop it here. Let's go ahead and tell PyCharm about about git,
1:03
so everything I create is going to be put into the git repository for you. And that'll be a little easier here,
1:09
go ahead and also tell it to ignore this directory. Maybe someday it'll do that on it's own. Now I want to create two files,
1:16
I don't like to cram everything into one Python file. When I'm creating an application I want to partition it into pieces so let's go over here
1:24
and create two things. We're going to create a program and we're going to create an API. And go ahead and tell PyCharm it can add stuff to git.
1:35
Okay so what we're going to do is we're going to write the code to access the service in the API file and we're going to work with it
1:42
from the user interaction perspective and orchestrate it from over here. So let's define a quick method over here, main. And we'll just have pass.
1:51
Now we want to follow the convention for running this program if and only if it's the target of execution. And in PyCharm, the way that,
1:59
or, in Python, the way that you do that is you use this if dunder name equals dunder main, then that means it's supposed to run
2:06
and I'll just call this method here. This is a little bit set up and ready to go. We're going to use something over in the API
2:15
so we're going to create a method here that's going to take a string and it's going to return some search results. So create.
2:22
And say find movie by title, let's say, because that's what we're actually searching for here. And the keyword, and it's going to do some stuff
2:32
and return some movies, that's going to be great. How do we do that? Well first we have to work with requests.
2:37
So we'll come over here and say import requests. Now let me go ahead and run this, it's not going to work out so well,
2:44
we're going to right click and create a run configuration in PyCharm, or you just type Python 3, you know, program.py.
2:52
That didn't actually import it so nothing actually happened. But let's do, if we come over here and say import api
2:57
as we're going to need to, now we try that again, boom, there is no requests. So of course we have to install requests.
3:04
Now if you look in our terminal, notice we have the virtual environment activated here, so we could say, see what's in there.
3:12
And there's nothing really in there. So one of the conventions people like to follow is when they have dependencies
3:20
they'll have a file called requirements.txt. And in there, they'll put library names like requests. requests, like this.
3:30
And notice, PyCharm already knows, hey, you need to say pip install requests, or you can just click this and that'll do it for you.
3:38
If you wait for a second down here and then we do the pip list again, there should be more stuff including requests, our program should now run.
3:45
Ta-dah, okay. So we have just the basic structure of this thing hanging together, right? We've installed requests, we've created the program,
3:53
and the api separation, we've got our requirements, and it's all kind of hanging together. Next up, we actually have to write
4:00
our method here, about how we find movies.