Hello! My name is John from Liquid Web
Today, I am going to show you how to get started with Flask
Flask is a Python Micro Framework
designed to be lightweight
and offering you the freedom to create web applications
without limitations on the modules
and packages you want to use,
and the methods and structure you want to follow.
For this tutorial I will be showing you how to build a Flask web application.
Building you Flask applications locally first,
will allow you to build and test
prior to moving to a production or development server and it also gives you a good sandbox for learning.
First, you will need Python 3 and pip3 installed.
We will be building this inside of a virtual environment
so virtualenv will need to be installed as well.
If you do not have it already,
virtualenv can be installed by running "pip3 install virtualenv" in your terminal.
Now let's get started.
All of the work demonstrated in this tutorial will be done inside a terminal.
These instructions are intended for MacOS or Linux based operating systems.
First, we will want to create a virtual environment to build the application inside of.
Now we are going to run the following command to create the virtual environment
virtualenv --python=python3 flask_tutorial
So in this command the
--python=python3 arguement is used to describe what Python interpreter we are going to use for the virtualenv
and the flask_tutorial is what the name of the virtual environment is going to be.
Now we are going to let that run
And what this is going to do is
create a directory,
named flask_tutorial
and that is going to be at the path that you are currently at.
So you can see here, that the flask_tutorial directory has been created
we will want to change directories into that directory
and activate that virtual environment.
cd flask_tutorial
To activate the virtual environment we'll need to run the command source bin/activate
and that's going to activate our virtual environment and you can see that it's activated.
Now that the virtual environment is activated,
let's install the flask package.
We are going to do this with pip.
pip install flask
Now that flask has been installed, let's get started building a flask web application.
I will be using vim as my text editor,
but any text editor will work for this.
I'll begin by opening a new file with vim;
vim flask-tut.py
At the top of this file, we will want to import the Flask package; from flask import Flask
now we will want to set up Flask by creating a variable "app" and set that equal to Flask; app = Flask(__name__)
app = Flask(__name__) and we will need to pass in name.
We are now ready to create our first route.
Routes in Flask use a decorator to define the route.
Once the route is defined,
below that we will create a function that is executed when the route is hit.
For this, we will be defining one route which is the root of the website.
When the root of the website is navigated to in a browser,
the function created below will be processed. @app.route("/")
and the slash here is indicating the root of the website. def index():
And since we are creating this at the root of the website, def index(): return "Hello from Flask!"
def index(): return "Hello from Flask!" I am just going to call this function index.
def index(): return "Hello from Flask!" We are just going to return a simple element,
and now that the route has been created we are going to need to add an if statement
to check if this is the main package on execution.
If the condition is true, run the flask development web server.
if __name__ == "__main__": app.run()
Now that is all we need in the file to create a basic Flask web application. Now let's save, and exit and run the application.
To start the development server, and start our Flask application
type the following command: python flack-tut.py
You can see the development server is now running
and it's showing what it is running on.
This is what you will need
to navigate to it in a browser to see the web application http://127.0.0.1:5000/
For me. you can see the application is running on 127.0.0.1 over port 5000.
So if we go to that in a browser,
we will be able to see the Python web application.
You can see in the terminal when going to the address,
the request and response codes are listed as they come in.
We made a request for slash
you can see that returned a 200 response.
There is also a request for favicon.ico,
that returned a 404 as it does not exist. This request was generated by Chrome.
That is all for now. We have successfully created a Flask web application.
From here you can build on this and create powerful web applications.
As always, thank you for watching this video. For more information and articles, be sure to visit http://www.liquidweb.com/kb