The aim of this video is to present how to create a Flask development environment, how to install Flask, how to create a simple web API, and how to run the project.
- [Teacher] Hi and welcome to Getting Up to Speed with Flask. In this section, we will cover the steps to create a new development environment for Python and the Flask web development framework. We will create our first Flask application and we will learn how to configure custom routing rules inside Flask. After that we will define and use templates and at the end of the section, we will cover Flask flash messages and how to set up custom error pages which deals with setting up a Flask development environment. We will use Pip and Virtualenv and we will create our first Flask Web API application.
In this video, we are going to take a look at the Flask framework in more detail, and we will check what are its main components. Then we will configure our development environment using Python's Virtualenv and we will install Flask using Pip, one of Python's very known package management systems. When all these are set up we will go through the Web API application code together. The application will be accessible through http get requests and will return some random, funny quotes. Flask is a micro framework used for website and Web API development.
Being a micro framework means it has a simple core and it is easily extendable. You will see it is very easy and fast to create applications using Flask. Because of it simplicity, it is widely adopted among web developers and the Python community. Flask is using the Jinja2 template engine with works with WSGI tookit. WSGI stands for Web Server Gateway Interface. A standard interface between web servers and web applications. Flask is a framework with batteries included. In many cases, even Python is mentioned as a programming language with batteries included.
This means the Flask framework supports building extensions and thanks to the modular architecture and the Python community, there are a lot of useful extensions available for flask. Now, we will create a development environment for our first Flask project. We will start with opening up a new terminal window and creating a new folder called funny api. Inside this folder we will host all the code necessary for our application. Plus, this is the folder where we will create the virtual environment. We will install Flask using Pip into this virtual environment, so we should go to the funny api folder and let's create the new folder called venv.
We can initialize the virtual environment inside that venv folder the Virtualenv keyword and passing in the folder as parameter. You can see on the console, that Python has been added to this venv bin folder, plus all the necessary setup tools plus Pip was installed there. Now, we need to activate the virtual environment. We can activate that by typing in source and giving it as root the activate command. In front of the console you can see the name of the virtual environment which is in our case is venv inside of parentheses.
This signifies that the virtual environment is active. Now, we can install Flask using Pip. This can be done using the Pip install Flask comment. On the screen, you can see highlighted that Flask was successfully installed along with the Werkzeug and Jinja2 template engines. Now, we are ready to start developing our first Flask application. Inside our funny api folder, I created two Python files app.py and quotes.py.
App.py is our main application file. We declared the Flask and jsonify imports from the Flask framework. Next, we import a random module. This will be used for generating random numbers when we are serving the funny quotes. Random numbers will be used to select quotes from the ones which we stored in the quotes.py file. Next, we declare a new Flask application and we pass an experiment of the current file's name. We define the serve funny quote function, and we are assigning this function to the /api/funny root.
Inside the function we load the quotes from the quotes.py file and we select random quotes from these. The quotes are stored as a list of Python dictionaries. Each dictionary has author and quote as keys. In the last line of the serve funny quote function, we return a JSON data structure using the jsonify Flask matter. At the end of the script we only start the Flask application if the Spy-Dos script is started separately.
The debug parameter of a starter application in debug mode which means we will get more detail added messages in case you made mistakes during the coding. The debug mode also will start the application once we change something in the code. Now, let's take a look at the application and try to start it. Let's go back to the terminal window and we have to make sure that our virtual environment is active. We can start the application by typing in Python app.py.
If the Flask framework started correctly, then we should see a local host 5000 port opened and the application is listening on this port. So, if we go to a browser, and we navigate to localhost5000/api/funny, the root which we mapped our serve funny quote matter to, then we should get some JSON response back. If we refresh the page, then we should get different responses.
In this video, we covered the main building blocks of Flask, the installation steps of Flask and the new development environment. We created a Flask Web API application. We served http requests and returned funny quotes. In the next video, we will take a deeper look into routing configuration, how to map http request parameters in Flask routing configuration and how to handle different http request methods like get and post. Thank you for watching and see you in the next video.
Regardless of platform, you will need to build APIs to serve data between different client applications and endpoints. Good APIs are a necessity for web and mobile projects, especially with the modern, mobile-first approach to development. This course delivers the fundamental knowledge required to enable highly connected interactions between applications via RESTful web APIs. Follow along with Gergo Bogdan and learn how to build up and structure an effective web API that can be used by any client application accessing it over HTTP, using Flask, the Python microframework.
Find out how to implement CRUD operations using SQLAlchemy and MySQL as the data store. Get an understanding of how REST works relative to APIs, and learn how to test APIs written in Python with the support of Flask. Explore token-based authentication and find out how to store passwords securely in your database. Along the way, Gergo introduces best practices and design guidelines when building large applications.
- Setting up Flask
- Using Flask templates and routing
- Implementing CRUD operations
- Implementing pagination
- Configuring RESTful authentication
- HTTP caching
- Securing web APIs
- Testing the application