What is Flask? What is the architecture? How do modules work? Get answers to these questions and more.
- [Instructor] What is Flask? Flask is a microframework for creating web applications, but what does that mean? The creators call it a microframework not because it's restricted in what it can do, but because it is designed for expansion. At its core, the library is very compact and only requires a couple of other libraries, one for the web server and one for a templating engine. For anything else, Flask makes it easy for you to add functionality via extensions. These extensions include just about anything you can imagine.
You can pick from available Flask extensions, use general Python libraries or even write your own. There are extensions to help with site and user administration and authentication. SQLAlchemy integrates Flask with many different database engines. You can add external email or shopping cart functionality. You can even override the base libraries used by Flask. But, what are those two libraries anyhow? Flask depends on two libraries. A WSGI toolkit, Werkzeug, and a templating engine Jinja2.
While you can extend Flask in many ways, out-of-the-box it's ready to go. You get a built-in development server and a RESTful-based request response framework. The Jinja2 templating engine means you don't have to write raw HTML, and secure cookie handling and support for testing round out a solid core offering. Let's take a look at a basic Flask application to get an idea how simple the interaction can be. If you want to explore this example, you can find it in the exercise files directory for this movie.
The first section creates the Flask application. Flask will run on port 5,000 by default, so we don't need to define that, but it still needs to know what to do when a request comes in, so we can jump right into defining routes. In the first route, you can see the basic structure of the route definition. The definition uses a decorator with the at sign to specify the route being defined. The slash means that Flask will serve this page from the root URL of the server. If you are running this yourself, it would be served at local host colon 5,000 slash.
Next, a function is defined called index which returns an HTML h1 element of learning Flask. The second route is more interesting. We're defining a variable based on the second part of the URL defined in using angle brackets. So when this URL is visited, the response will have that part of the path included as a variable for our function to work with. The function here is named user, and the name is passed along from that path variable. Note that we're using Python formatting to create this string along with the format function from the templating engine.
So, let's start up the server. The first thing I'm going to do is I'm going to change into the directory that has my exercise files, so it's cd desktop exercise files, and I want to be in chapter one oh one, oh one. The next thing I need to do is set up my virtual environment, so virtualenv venv.
And then, I'm going to activate the virtual environment with source venv bin activate. Now, when I use pip to do the installation here, it's going to install it for the virtual environment, so I don't have to worry about permissions on my system. So, pip install -r requirements.txt. Okay, it's installed all of the libraries that I need in my virtual environment. Note if I have other virtual environments, they're not affected by what I just did, so you can have a nice clean development environment.
Now, we're going to export FLASK APP=flask basic.py, the file, and then flask run. It says it's running on my local host on port 5,000, so let's take a look at that, and it says learning Flask. Let's go ahead and play with the variable that we put together, so user Kirsten, and now it says hello, Kirsten.
I can put any name in there, and that's what it'll reflect back, so this is a very simple example of how the interaction can work for Flask. But as you can see, the code is quite simple for this application. Flask provides a great deal of functionality in the core system. Moving forward, we'll see some of the things that are possible with those extensions.
- Working with the Flask command line
- Using response templates
- Creating web forms
- Connecting to a database
- Authenticating users
- Uploading images to an S3 pipeline
- Deploying Flask applications to Heroku with Git