There are tons of Python web frameworks and Flask is one of them but it is not a full stack web framework.
It is “a microframework for Python based on Werkzeug, Jinja 2 and good intentions.” Includes a built-in development server, unit tesing support, and is fully Unicode-enabled with RESTful request dispatching and WSGI compliance.
To install flask you can go here or just follow below steps:
If you are using Python3 than you don't have to install virtual environment because it already come with venv module to create virtual environments.
If you are using Python 2, the venv module is not available. Instead,
On Linux, virtualenv is provided by your package manager:
//Debian, Ubuntu $ sudo apt-get install python-virtualenv //CentOS, Fedora $ sudo yum install python-virtualenv //Arch $ sudo pacman -S python-virtualenv
If you are on Mac OS X or Windows, download get-pip.py, then:
$ sudo python2 Downloads/get-pip.py $ sudo python2 -m pip install virtualenv
On Windows, as an administrator:
\Python27\python.exe Downloads\get-pip.py \Python27\python.exe -m pip install virtualenv
Create a project folder and a
venv folder within:
mkdir myproject cd myproject python3 -m venv venv
py -3 -m venv venv
If you needed to install virtualenv because you are on an older version of Python, use the following command instead:
Activate the environment
Before you work on your project, activate the corresponding environment:
Your shell prompt will change to show the name of the activated environment.
Within the activated environment, use the following command to install Flask:
$ pip install Flask
So, let's build the most simplest
hello world application.
Follow these steps:
As, you are already present in the
myprojectfolder. Create a file `hello.py' and write the below code.
Import the Flask class. An instance of this class will be our WSGI application.
from flask import Flask
Next we create an instance of this class. The first argument is the name of the application’s module or package. If you are using a single module (as in this example), you should use name because depending on if it’s started as application or imported as module the name will be different ('main' versus the actual import name). This is needed so that Flask knows where to look for templates, static files, and so on.
app = Flask(name)
We then use the route() decorator to tell Flask what URL should trigger our function.The function is given a name which is also used to generate URLs for that particular function, and returns the message we want to display in the user’s browser.
return 'Hello, World!'
Make sure to not call your application flask.py because this would conflict with Flask itself.
To run the application you can either use the flask command or python’s
-mswitch with Flask. Before you can do that you need to tell your terminal the application to work with by exporting the
$ export FLASK_APP=hello.py
$ flask run
//Or you can use
$ export FLASK_APP=hello.py
$ python -m flask run
- Go to http://127.0.0.1:5000/ to see your project running.
Need to keep up with a fast-moving software industry?