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.
Installation
To install flask you can go here or just follow below steps:
Step1: Install virtual environment
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, install virtualenv
.
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
Step 2: Create an environment
Create a project folder and a venv
folder within:
mkdir myproject
cd myproject
python3 -m venv venv
On Windows:
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:
virtualenv venv
On Windows:
\Python27\Scripts\virtualenv.exe venv
Activate the environment
Before you work on your project, activate the corresponding environment:
. venv/bin/activate
On Windows:
venv\Scripts\activate
Your shell prompt will change to show the name of the activated environment.
Step 3: Install Flask
Within the activated environment, use the following command to install Flask:
$ pip install Flask
Flask is now installed: Check out the Quickstart or go to the Documentation.
Create a applcation
So, let's build the most simplest hello world
application.
Follow these steps:
-
As, you are already present in the
myproject
folder. 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.
@app.route('/') def hello_world(): 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
-m
switch with Flask. Before you can do that you need to tell your terminal the application to work with by exporting theFLASK_APP
environment variable:$ 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.
-
Top comments (12)
Hi Sahil,
I just wanted to add an error I ran into while attempting this tutorial.
I received an error 'NameError: name 'name' is not defined'
after checking stackoverflow, I found a solution and changed (name) to (
__name
__) and this worked for me.I hope this helps someone else with here,
Thanks!
Hi Dmitri,
I have written __ name __ but somehow it changed to 'name'.
Thank you for pointing out the mistake.
I'm getting an Error here -
(venv) Sandips-MacBook-Pro:Sandip_TSConsole_Coral_Prototype_GIT sanbaner$ export FLASK_APP=hello.py
(venv) Sandips-MacBook-Pro:Sandip_TSConsole_Coral_Prototype_GIT sanbaner$
(venv) Sandips-MacBook-Pro:Sandip_TSConsole_Coral_Prototype_GIT sanbaner$ flask run
Error: Could not import "hello".
(venv) Sandips-MacBook-Pro:Sandip_TSConsole_Coral_Prototype_GIT sanbaner$
Working fine now !
hi how did u solve this? i get the same error as well.
to see it in the network
Thank you for this. You may wish to add the command for Windows CMD and Windows Powershell in the last section (as you have in the previous sections). I was using Powershell so had to look it up when I realised what you had put was for Unix.
Windows CMD:
Windows PowerShell:
Hello,
When trying to do this: *venv\Scripts\activate * I get the error:
venv\Scripts\activate : File C:\Users\My\Desktop\CS50\project\venv\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
Pls, why is it disabled and am I safe to change execution policy?
Thank you.
It works!
Hi Sahil, thanks for sharing!
Why is it necessary to create a virtual environment to run Flask?
It is a good approach to create virtual environment whenever you start a new project. So that no two libraries cause conflict with each other.
For example:
If one project uses Django==1.0.1 and other uses Django==3.0.0. so you have to create the virtual environment for both the project so that you can run them smoothly else one of them will cause problem
When I want to install I get:
Requirement already satisfied: Flask in user / lib / python3 / dist-packages (1.1.2)
Help