What can you do with the Flask CLI? How can you configure it? Get answers along with hands-on exercises and sample code.
- [Instructor] When you install Flask, it comes with a simple command line interpreter to run your code with various configuration options set. To get started, change into the start subdirectory of the exercises files directory for this movie and set up the virtual environment there. I've done most of that in my terminal already, I just need to run the pip install command to grab the libraries. The basic usage for Flask run is to set the name of your application in the environment and run the application. Let's do that now.
Set the environment variable Flask app to the name of the file. So export flask_app equals basic.py. On Windows you would use set instead of export. So now we run the Flask server, flask run, and now there's a Basic server running on port 5000 of localhost; let's take a look at it in the browser. So localhost5000.
It says learning Flask. And again if we do the user Fred, then it would say hello to Fred. Now that we've got that working, let's see what else can be done with the command line argument. Frequently I want to turn on debugging during development, so whenever to do that you set a debug flag, export Flask underscore debug equals one in the environment. If you run the server again you'll see much more information about what's happening in the environment. When using Flask run, you can also set the host and port to run on.
By default, Flask runs on 127.0.0.1, which will respond only to requests from localhost. This is great during development, but not good for production. You can set it to 0.0.0.0 to respond to requests from other hosts as well. So then that would look like, let's go back to the terminal, flask run dash dash host 0.0.0.0, dash dash port 5200.
Of course you can set these within the configuration of your application instead, but we'll see that if we go now to 5200, we have the application running. Sometimes you'll find older Flask applications out in the wild, and they'll have a section if name equals main app.run. This is an artifact of older versions of Flask which didn't have the command line interface and so it's not longer needed. Now one of the cool thing about the Flask CLI is if you want to interact with your application from the command line, there's a Flask shell.
By default this opens up an interactive Python shell with the correct application context and access to your app object. You can extend this access by setting up a flask makeshell context method returning a dictionary defining what you'd like to access within the shell. But let's go ahead and set up a CLI instance. We're going to make a new file, and we'll call it cli.py, put it in the correct directory.
Now we're going to write a little application that makes a very simple command line interface. So import click, from flask import Flask. The capital Flask is very important here. When you use the lowercase it's not going to find the library you're looking for. So app equals Flask name, this is the same as the other programs that we've looked at already.
But now we have app.cli.command, and we'll do a def of sayhi, and that will do a click.echo, with hello. This is a very simple little application, and as you can imagine it can be extended to do all sorts of things with your application, but let's see how it works.
So change back to your terminal. Export Flask_app, again, you'll want to use set if you're in Windows, cli.py, and then say flask sayhi, and it says hello. By default these commands are called with the app context. We'll cover context a little bit more later, but you can override that if you like. So using the new Flask CLI makes it much easier to interact with your application from the command line, and has the flexibility to support whatever level of interaction you want.
- 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