Getting Started with Flask Part I

  • Flask is a micro web framework written in Python.
  • Flask depends on the Jinja template engine and the Werkzeug Web Server Gateway Interface (WSGI) toolkit.
  • Jinja is a modern and designer-friendly templating language for Python
  • Werkzeug is a comprehensive WSGI web application library.
  • WSGI is an interface specification by which server and application communicate.

Installation

Flask supports Python 2.7, Python 3 and newer. Recommended to use Python 3 version or newer.

The below distributions will be installed automatically when installing Flask.

  • Werkzeug implements WSGI, the standard Python interface between applications and servers.
  • Jinja is a template language that renders the pages your application serves.
  • MarkupSafe comes with Jinja. It escapes untrusted input when rendering templates to avoid injection attacks.
  • ItsDangerous securely signs data to ensure its integrity. This is used to protect Flask’s session cookie.
  • Click is a framework for writing command line applications. It provides the flask command and allows adding custom management commands.

Step 1: Install Virtual environment

Create virtual environment to manage the dependencies for your project. If we have virtual environments packages installed for one project will not affect other projects.

Python 3 has venv module to create virtual environments.

C:\Users\Girish>pip install virtualenv
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/8b/12/8d4f45b8962b03ac9efefe5ed5053f6b29334d83e438b4fe379d21c0cb8e/virtualenv-16.7.5-py2.py3-none-any.whl (3.3MB)
     |████████████████████████████████| 3.3MB 2.2MB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.5

Step 2: Create a project folder and a virtual environment myvenv folder within.

Step 3: Then activate the virtual environment from Scripts folder

Step 4: Then install Flask on the virtual environment.

C:\Users\Girish>mkdir myflaskproject

C:\Users\Girish>cd myflaskproject

C:\Users\Girish\myflaskproject>python -m venv myvenv

C:\Users\Girish\myflaskproject\myvenv\Scripts>activate

(myvenv) C:\Users\Girish\myflaskproject\myvenv\Scripts>pip install Flask
Collecting Flask
  Downloading 
.................
Installing collected packages: click, MarkupSafe, Jinja2, Werkzeug, itsdangerous, Flask
Successfully installed Flask-1.1.1 Jinja2-2.10.3 MarkupSafe-1.1.1 Werkzeug-0.16.0 click-7.0 itsdangerous-1.1.0

Step 5: Writing Code

Now create the file home.py inside the project folder and write the below code.

  • Here we imported Flask class and created an instance of this class.
  • __name__ argument is the name of the application’s module or package.
  • Used route() decorator to inform Flask what URL should trigger the function.
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'

Step 6: Run the application

  • set FLASK_APP=home.py
  • flask run
  • App will run on http://127.0.0.1:5000/ (Press CTRL+C to quit), see the output in browser.
(myvenv) C:\Users\Girish\myflaskproject>set FLASK_APP=home.py

(myvenv) C:\Users\Girish\myflaskproject>flask run
 * Serving Flask app "home.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

References

  • https://flask.palletsprojects.com/en/1.1.x/installation/#
  • https://flask.palletsprojects.com/en/1.1.x/quickstart/

Learn more about Flask Web Application Framework features like Routing, URL Binding, HTTP Methods, Static Files, Rendering Templates, Accessing Request Data , Data Access from database, Cookies, Sessions, Redirects and Error Handling, SQLite, Deployment in the upcoming Flask Tutorial Series.

Happy Learning!