Getting Started — REST APIs with Flask and Python part 1
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. It makes the process of designing a web application simpler. Flask lets us focus on what the users are requesting and what sort of response to give back.
Learn more about microframeworks.
Flask is a simple, easy-to-use microframework for Python that can help build scalable and secure web applications. Here are a few reasons why Flask is great for beginners:
- It’s easy to set up
- It’s supported by an active community
- It’s well documented
- It’s very simple and minimalistic and doesn’t include anything you won’t use
- At the same time, it’s flexible enough that you can add extensions if you need more functionality
- Installing Python
If you don’t have Python installed on your computer, go ahead and install it now. To make sure your Python installation is functional, you can open a terminal window and type
python3, or if that does not work, just
python. Here is what you should expect to see:
The Python interpreter is now waiting at an interactive prompt, where you can enter Python statements. In future chapters, you will learn what kinds of things this interactive prompt is useful for. But for now, you have confirmed that Python is installed on your system. On the Linux versions of Python, you can exit the interpreter by pressing Ctrl-D.
- Installing Virtualenv
The next step is to install Flask, We’ll start by installing virtualenv, a tool to create isolated Python environments. We need to use virtual environments to keep the dependencies used by different Python projects separate, and to keep our global site-packages directory clean. We’ll go one step further and install virtualenvwrapper, a set of extensions that make using virtualenv a whole lot easier by providing simpler commands.
To create and activate a virtualenv, run the following commands:
Told you the commands were simple! We now have a virtualenv called
myenv, which we have activated and are currently working on. Now, any dependencies we install will be installed here and not globally. Remember to activate the virtualenv whenever you want to use or work on this project!
Next, let’s create a directory for our app. This is where all our files will go:
Finally, let’s install Flask:
How Does a Flask App Work?
The code lets us run a basic web application that we can serve as if it were a website.
This piece of code is stored in our test.py.
Line 1: Here we are importing the Flask module and creating a Flask web server from the Flask module.
Line 3: __name__ means this current file. In this case, it will be test.py. This current file will represent my web application.
We are creating an instance of the Flask class and calling it app. Here we are creating a new web application.
Line 5: It represents the default page. For example, if I go to a website such as “google.com/” with nothing after the slash. Then this will be the default page of Google.
Line 6–7: When the user goes to my website and they go to the default page (nothing after the slash), then the function below will get activated.
Line 9: When you run your Python script, Python assigns the name “__main__” to the script when executed.
If we import another script, the if statement will prevent other scripts from running. When we run test.py, it will change its name to __main__ and only then will that if statement activates.
Line 10: This will run the application. Having
debug=True allows possible Python errors to appear on the web page. This will help us trace the errors.
Let’s Try Running test.py
127.0.0.1 means this local computer. The main idea is that 127.0.0.1 and localhost refer to this local computer.
Go to that address and you should see the following:
When you are done playing with the server you can just press Ctrl-C to stop it.
Congratulations, you have completed the first big step to become a web developer!