Flask – Python Framework
In this article, You’ll learn how to get started with one of the most commonly used Python framework Flask. So if you just beginning with flask then it’s a great place to get start. Hang on tight warriors!
Table Of Contents
What is Flask?
In simple words, Flask is classified as the lightweight microweb framework of Python. Now why microweb framework? Answer to this question lies in its architecure. Flask framework does not contain database layer, form validation, user authentication in itself. Flask only provides you the instance of the framework as app. All the services such as database layer, form validation, Authentication system and many more are used as external libraries in the flask framework. These libraries connect with the instance of the flask app and act accordingly.
Unlike Django – Another popular framework of Python in the market, which contains all the libraries such as database layer, form validation, Autthenticatio, Admin panel in the Django Library itself. If you want to learn to get started with Django. We have written a very easy to follow article for the beginners here Getting started with Django
Now another question that can pop into your head is why another framework when we already had Django? If you visit Frameworks of Python then you’d see that there are more frameworks and each framework was designed with a purpose or due to the limitation in other framework. You’d get a clear picture and understanding why there are so many frameworks for Python.
Requirements
Anaconda – Anaconda is a free and open-source distribution of the Python and R programming languages for scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system conda. Or you could install python manually from their official website
Python – Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). For more information, you can visit https://www.python.org
Flask– “Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks”.
Conda – Conda is an open source package management system and environment management system for installing multiple versions of software packages and their dependencies and switching easily between them. It works on Linux, OS X and Windows, and was created for Python programs but can package and distribute any software.
Editor and Terminal – You are free to choose any editor of your choice and you will use your normal command terminal.
Setup
Let’s setup flask and get our hands dirty with code.
Install Anaconda – The reason for installing Anaconda is to create an environment specifically targeting your project need without disturbing other project needs. We can use Python Virtual Env library to create also. In this project we are using Anaconda. Go to Anaconda official sites indivisual section and download Anaconda.
Create your Environment – Follow Anaconda official website steps to create your very first environment. Create a directory, let’s say flask_app and open your terminal inside that directory. And type the following
G:/home/projects/pythonwarrior/flask_app> conda create -n yourenvname python=3.7
It’d download the necessary packages that you’d need to create your environment. Notice that I have strictly told conda to download python version 3.7. Otherwise it’d download the latest version.
Activate the environment – Since your environment is created , Let’s activate it.
G:/home/projects/pythonwarrior/flask_app> conda activate pythonwarriors (pythonwarriors) G:/home/projects/pythonwarrior/flask_app>
Install flask using pip – Now download flask using pip.
G:/home/projects/pythonwarrior/flask_app> conda activate pythonwarriors (pythonwarriors) G:/home/projects/pythonwarrior/flask_app> pip install flask Collecting flask Collecting flask Downloading https://files.pythonhosted.org/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl (94kB) |████████████████████████████████| 102kB 364kB/s Collecting itsdangerous>=0.24 (from flask) Using cached https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl Collecting click>=5.1 (from flask) Downloading https://files.pythonhosted.org/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl (82kB) |████████████████████████████████| 92kB 143kB/s Collecting Werkzeug>=0.15 (from flask) Downloading https://files.pythonhosted.org/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298kB) |████████████████████████████████| 307kB 504kB/s Collecting Jinja2>=2.10.1 (from flask) Downloading https://files.pythonhosted.org/packages/30/9e/f663a2aa66a09d838042ae1a2c5659828bb9b41ea3a6efa20a20fd92b121/Jinja2-2.11.2-py2.py3-none-any.whl (125kB) |████████████████████████████████| 133kB 187kB/s Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask) Downloading https://files.pythonhosted.org/packages/65/c6/2399700d236d1dd681af8aebff1725558cddfd6e43d7a5184a675f4711f5/MarkupSafe-1.1.1-cp37-cp37m-win_amd64.whl Installing collected packages: itsdangerous, click, Werkzeug, MarkupSafe, Jinja2, flask Successfully installed Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.2 itsdangerous-1.1.0
create a file – let’s create a file called app.py
, now there is a reasson behind naming this file as app.py
. By default flask searches for app.py
file for itself. But if you want to name it differently you can go ahead and do it. Now after creating the file write the following code in it.
from flask import Flask app = Flask(__name__) @app.route(‘/’) def hello_world(): return ‘Hello, World!’
Now go to your terminal move to the directory where your file is stored. You might be wondering what would this small piece of code would do. I mean its hardly 10 lines. Well to answer this question and to answer the question why flask, Just go to your terminal and type the following
G:/home/projects/pythonwarrior/flask_app> flask run
if you have named your file as app.py. Otherwise do this
G:/home/projects/pythonwarrior/flask_app> export FLASK_APP=file_name_you_chose.py flask run
Magic
Now you might be able to see something like this in your terminal.
* Serving Flask app "file_name_you_chose.py" * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
So in just less 10 lines of code you were able to start your server and not just server, you sent the most important part of a coder’s life message to the browser. Go to the browser and see for yourself. That you my warrior friend have begun your fight with flask in style.
Now flask is very easy to understand and use. In the next tutorial, you’d be learning about routing ways in flask. It’s done in different ways such as by using methods and bu using REST APIs. If you want to know more about flask, visit Flask official site here.