Hi and welcome back.
In this video, let's set up our project for the REST API.
I have opened the Visual Studio Code editor.
You can use the editor of your choice.
And all I have in this folder that I've called Recording, but you can call it
rest API project or whatever you want.
All I have in here is this .vscode folder, which has some editor settings.
Things like zoom level and things like that.
Basically this is empty.
What I'm gonna start with is I'm going to create a virtual environment.
So I'm gonna do python3.10 -m venv .venv.
This runs the Python 3.10 executable, and it asks it to use the venv module
that comes with Python to create a new virtual environment called .venv.
And I'm just calling it .venv so it's a hidden folder and it doesn't
obscure our files and folders too much.
If you're using a later Python version, which I recommend you
should use the latest Python version, then I imagine it will be the same.
But if things change, just figure out how to create a virtual environment for
your project using venv or virtualenv.
Now that we've got that, we have to tell Visual Studio Code or your editor,
if you're using PyCharm or something else, which virtual environment to use.
So in visual studio code, you do a command shift P and then you
find Python: Select interpreter.
And here we're going to select this one that has the path
to the venv we've created.
So that's in the current folder .venv/bin/python.
If you're not sure how to select the interpreter for your editor,
a quick Google search, I'm sure will give you some results.
So once you've got that, you've told Visual Studio Code the Python
interpreter to use, but this terminal needs to be restarted in order for
it to use the virtual environment.
So I'm just gonna bin the terminal and open another terminal, which
I'm gonna do with command J.
And now you can see that the terminal has changed slightly and Visual Studio Code
has activated the virtual environment before letting me run anything.
And now we've got this venv inside brackets there that tells us that
the virtual environment is active.
The next thing is to install the requirements, pip install flask.
Once that's done, we've got Flask installed inside this virtual environment.
And you should be able to see here inside lib, various things
like Flask and Jinja2, et cetera.
Now let's create a new file, which I'm gonna call app.py.
And in here, we're gonna write some pretty standard boilerplate:
from flask import Flask, and app is Flask with __name__ as the argument.
This creates a Flask app and that does a lot of stuff for us,
including being able to run the app.
If we define any endpoints in this app that we'll do in a moment, that
can accept data and return a response, then running the app will make those
endpoints available to a client.
So running the app is always the thing we want do in order to use Flask.
We're gonna do that with flask run, making sure that the terminal has the
virtual environment activated and you are in the folder that contains app.py.
flask run is gonna look for a file called app.py.
And inside app.py, it's gonna look for a variable called app.
So it's important that the file name and the variable name are correct.
So run flask run, and you should see something like this.
The warning here just means that when you deploy this application to
share it with your users, which we will do later on in this course, you
shouldn't share it by typing flask run.
You'll use something else.
And I'll teach you everything you need to know about that when we get to that stage.
But for now you can see the app is running on 127.0.0.1:5000.
And that means that when we make any requests, this flask app, we're
going to make them on this address.
When we send data to this address, that data is going to be forwarded
to the Flask app, which if we tell it to, will be able to intercept
that data and do something with it.
But for now, because we haven't told it what to do with any incoming data,
it won't be able to do anything.
And any request that we make to this address will return a 404 not found,
which is a pretty common thing as I'm sure you'll have seen on the internet.
So that's it.
The app is running.
We can stop it with control C.
And now we're ready to work on our first API endpoint.