Getting started with flask with Ubuntu 16.04

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. It is classified as a microframework because it does not require particular tools or libraries.[3] It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.

“Micro” does not mean that your whole web application has to fit into a single Python file (although it certainly can), nor does it mean that Flask is lacking in functionality. The “micro” in microframework means Flask aims to keep the core simple but extensible.

Flask Vs django

When I was learning python, I decided to choose one of the python framework for web application development. Obvious question was whether it should be django or flask ? django and flask both have their own use cases and I started with django instead of flask simply because of some of the big websites like instagram, pinterest are using django and I could not find any well known website using flask.

However, upon my further research I found that flask is more pythonic than django (IMHO). django is “batteries included” and very opinionated framework where as flask is un-opinionated framework and this suits many use cases.

Where flask is used.

fask is used as api provideer at pinterest twilio. falsk is also used to create dash

Install flask

Flask can be installed using following command

sudo python3.6 -m pip install Flask

First Python App

Basic application is as below

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
return 'Index Page'

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

Same this file as hello.py and run following commands

export FLASK_APP=hello.py
$flask run
 * Serving Flask app "hello_world.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)

and user application will be up and running

Getting started with flask
Getting started with flask

as a project get bigger, it becomes overwhelming to keep all the code in one file. Python projects use packages to organize code into multiple modules that can be imported where needed, and the tutorial will do this as well.

 

Blueprint is a way to organize a group of related views and other code. Rather than registering views and other code directly with an application, they are registered with a blueprint. Then the blueprint is registered with the application when it is available in the factory function.

The template files will be stored in the templates directory inside the flaskr package. Templates are files that contain static data as well as placeholders for dynamic data. A template is rendered with specific data to produce a final document. Flask uses the Jinja template library to render templates.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.