Folder structure and file organisation for python flask

Two methods to create flask application

Case 1: a module:

/application.py
/templates
    /hello.html

Case 2: a package:

/application
    /__init__.py
    /templates
        /hello.html

For larger applications it’s a good idea to use a package instead of a module.

How to convert module into package

To convert that into a larger one, just create a new folder yourapplication inside the existing one and move everything below it. Then rename yourapplication.py to __init__.py. (Make sure to delete all .pyc files first, otherwise things would most likely break)

 

Now we can restructure the application a bit into multiple modules. The only thing you have to remember is the following quick checklist:

  1. the Flask application object creation has to be in the __init__.py file. That way each module can import it safely and the __name__ variable will resolve to the correct package.
  2. all the view functions (the ones with a route() decorator on top) have to be imported in the __init__.py file. Not the object itself, but the module it is in.

Here is sample folder structure

 


.
└── myproject
├── myapp1
│   ├── __init__.py
│   ├── static
│   ├── templates
│   │   ├── aboutus.html
│   │   ├── base_bkp.html
│   │   ├── base.html
│   │   ├── contactus.html
│   │   └── index.html
│   └── views.py
└── setup.py

Using Blueprint

In above example we have used very simple structure. Now let us look at creating multiple views and grouping them together using blueprint

A 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.

here is sample code for simple flask package with two blueprints


import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    # app.config.from_mapping(
    #     SECRET_KEY='dev',
    #     DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    # )

    from . import auth
    app.register_blueprint(auth.bp)

    from . import blog
    app.register_blueprint(blog.bp)

    # import myapp1.views

    # a simple page that says hello
    @app.route('/')
    def hello():
        return 'Hello, World!'

    return app

auth.py


import functools

from flask import (
    Blueprint, flash, g, redirect, render_template, request, session, url_for
)
from werkzeug.security import check_password_hash, generate_password_hash


bp = Blueprint('auth', __name__, url_prefix='/auth')

@bp.route('/')
def auth_home():
	return "this is auth base page"

@bp.route('/aboutus')
def aboutus():
	return render_template('aboutus.html', name="techtrekking.net")

@bp.route('/contactus')
def contactus():
	return render_template('contactus.html')

blog.py


import functools
from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for
)
from werkzeug.security import check_password_hash, generate_password_hash
bp = Blueprint('blog', __name__, url_prefix='/blog')
@bp.route('/')
def auth_home():
    return "this is blog base page"
@bp.route('/aboutus')
def aboutus():
    return render_template('aboutus.html', name="techtrekking.net")
@bp.route('/contactus')
def contactus():
    return render_template('contactus.html')

By using blueprint, you can segregate urls based on their functions. This is very usefull method to segregate the different sections of the website.

Using templates in Python Flask framework

Flask will look for templates in the templates folder. So if your application is a module, this folder is next to that module, if it’s a package it’s actually inside your package:

Currently our application is developed as module. We will see the difference between module and package later.

Adding Templates

Now let us add a folder called templates inside application directory and add required html files here. Update application file to use html files

from flask import Flask
from flask import render_template
app = Flask(__name__)

@app.route('/')
def index():
	return render_template('index.html')

@app.route('/aboutus')
def aboutus():
	return render_template('aboutus.html', name="techtrekking.net")

@app.route('/contactus')
def contactus():
	return render_template('contactus.html')

Please note  following changes to the base file:

  1. we have imported “render_template”
  2. usage of render_template for creating output
  3. Usage of parameter “name” while rendering the html file. This keyword will be accessible in template and will be displayed within {{}}

This is simple example  of using separate template files for each route. Here none of the code is shared across views

Advanced templates

Many times we have few fixed parts of the website, irrespective of the page, such as navigation bar and footer. If you have to keep this code in each html page, making any change in this code will be cumbersome process. Flask has a great feature to make this change once and apply this to all pages. This is achieved by creating base template and extending the base template for individual pages.

Here we will create a base template having navigation bar, footer and different section. When we have to render different pages, we will simple reuse this base template and replace the required section run time.

Here is sample folder structure


├── myapp.py
└── templates
     ├── aboutus.html
     ├── base.html
     ├── contactus.html
     └── index.html

Base file structure


<!doctype html>
<title>{% block title %}{% endblock %}</title>
<a href="/">home</a>
<a href="/aboutus">about us</a>
<a href="/contactus">contact us</a>

<header>
{% block header %}{% endblock %}
</header>
{% block content %}{% endblock %}

About us file is as below


{% extends 'base.html' %}

{% block title %} about us{% endblock %}

{% block header %}
<h1>About us header</h1>
{% endblock %}

{% block content %}
<h4>About us</h4>
<p>lorum ipsum text here</p>
<p>this is {{ name }}</p>
{% endblock %}

Now whenever you want to  change header or footer, you need to change only at one place.

Please note following

  • I have created blocks in base file. More the number of blocks, more the flexibility
  • Blocks will be replaced in the individual templates.
  • Base code outside the blocks gets copied as is.
  • If base block is not placed in individual template, base block will be displayed

“Hello World !” using python flask framework

In this article we will create a simplest application using pything flask. This will display nothing but simple ” hello world on the page.

Here is the code

from flask import Flask
app = Flask(__name__)

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

This is cleanest code for any framework that I have seen.

Now go to terminal and type following commands

conquistador@inspiron-3542:~/code/flask/tutorial$ export FLASK_APP=hello_world.py
conquistador@inspiron-3542:~/code/flask/tutorial$ flask run
* Serving Flask app "hello_world.py" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 189-606-216

Now go to http://127.0.0.1:5000/ and  you will see following page

Flask Hello world
Flask Hello world

Adding few more pages.

Let us add aboutus and contact us pages. To add routing, you simple need to add 3 line to the same file. You need to add @app.route(‘/contactus’)

Here is updated hello world file

from flask import Flask
app = Flask(__name__)

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

@app.route('/aboutus')
def aboutus():
   return 'This is about us page'

@app.route('/contactus')
def contactus():
   return 'This is contact us page'

Now you can access about us page at http://127.0.0.1:5000/aboutus and contact us page at  http://127.0.0.1:5000/contactus

Flask About us page
Flask About us page

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.

Flask Python Tutorial

Flask is a microframework for Python. “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.

Configuration vs Conventions

Flask has many configuration values, with sensible defaults, and a few conventions when getting started. By convention, templates and static files are stored in subdirectories within the application’s Python source tree, with the names templatesand static respectively. While this can be changed, you usually don’t have to, especially when getting started.

In this chain or articles we will learn flask. Here is overall flow

  • Getting started with Flask
  • Hello World in Flask
  • Templates
  • Databases
  • MVC in flask
  • Structuring your project

 

Working with django templates

Django ships with a built-in backend for its own template system – the Django Template Language (DTL).

the most basic way you can use Django’s template system

  1. Create a Template object by providing the raw template code as a string.
  2. Call the render() method of the Template object with a given set of variables (the context).

Django makes it possible to separate python and HTML, the python goes in views and HTML goes in templates. To link the two, Django relies on the render function and the Django Template language.

This function takes three parameters −

  • Request − The initial request.
  • The path to the template − This is the path relative to the TEMPLATE_DIRS option in the project settings.py variables.
  • Dictionary of parameters − A dictionary that contains all variables needed in the template. This variable can be created or you can use locals() to pass all local variable declared in the view

Let us update following in polls/urls.py

path('first/', views.first, name='first'),
path('second/', views.second, name='second'),

If you want to display simple message without using template, it can be done using as below

from django.http import HttpResponse

def first(request):
    return HttpResponse("Hello, world. You're at the polls index.")

If you want to implement same thing using dejango templates, you need to use code as below

add this into views.py

from django.shortcuts import render

def second(request):
    return render(request, 'polls/second.html')

create a template in this path ‘template/polls/second.html’

Hello, world. You're at the polls index. This is from templates/polls/second.html

and here is the output from http://127.0.0.1:8000/polls/first/

and here is the output from http://127.0.0.1:8000/polls/second/

Working with Django templates

When we create website, basic rule that we follow is DRY (Dont repeat yourself). In any website, few things are common in all pages, like navbar and footer. In such cases, in make sense to have this coded at one place and include it wherever required, this way, if we have to make any change, we need to change at only one place. In Django tenplates this is achived using template inheritance

Template inheritance

Template inheritance is one of the most useful feature of django templates. Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override.

Here is the base template. Its created in polls/templates/polls directory


<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}Mysite02 Django Tutorial{% endblock %}</title>
</head>

<body>
<div id="sidebar">
{% block sidebar %}
<ul>
<li><a href="/">Home</a></li>
<li><a href="/polls/first/">first</a></li>
<li><a href="/polls/second/">second</a></li>
<li><a href="/polls/third/">third</a></li>
<li><a href="/polls/fourth/">fourth</a></li>
</ul>
{% endblock %}
</div>

<div id="content">
{% block content %}
<h>This is content Block from base file </h>
{% endblock %}
</div>
{% block footer %}
<footer>
(C) all rights resevered. 2019
</footer>
{% endblock %}
</body>
</html>

Here is overriding template

{% extends "polls/base.html" %}

{% block content %}
    This is content Block from polls/fourth.html file 
{% endblock %}

{% block footer %}
    This is footer Block from polls/fourth.html file 
{% endblock %}

this is comment outside block from polls.fourth.html

Here is how base template looks like

Here is how template after over riding looks like

Here you can see content block and footer block is replaced by the template.

Also note that content outside of the block from overriding template is not displayed.

Accessing methods

Most method calls attached to objects are also available from within templates. This means that templates have access to much more than just class attributes (like field names) and variables passed in from views. For example, the Django ORM provides the “entry_set” syntax for finding a collection of objects related on a foreign key. Therefore, given a model called “comment” with a foreign key relationship to a model called “task” you can loop through all comments attached to a given task like this:

 

How to Install correct django version

django has multiple version which are supported. There are few differences which can cause confusion while using it. If you want to install specific version of django, follow below steps.

Step#1 Check what is the current version.

$ python3.6 -m django --version
2.0

OR

$ python3.6
Python 3.6.4 (default, Jan 13 2018, 12:02:51) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.__version__
'1.11'
>>> exit()

If current version is expected version, the you don’t need any of below steps.

Step#2 Uninstall current version

Please note that here you don’t have to put any version while uninstalling. whatever be the current version that will be uninstalled.

$ sudo python3.6 -m pip uninstall django
[sudo] password for user: 
Uninstalling Django-1.11:
  Would remove:
    /usr/local/bin/django-admin
    /usr/local/bin/django-admin.py
    /usr/local/lib/python3.6/site-packages/Django-1.11.dist-info/*
    /usr/local/lib/python3.6/site-packages/django/*
Proceed (y/n)? y
  Successfully uninstalled Django-1.11

Step#3 Install expected version

If you want to install latest version use following command

$ sudo python3.6 -m pip install django
Collecting django
  Downloading https://files.pythonhosted.org/packages/ab/15/cfde97943f0db45e4f999c60b696fbb4df59e82bbccc686770f4e44c9094/Django-2.0.7-py3-none-any.whl (7.1MB)
    100% |████████████████████████████████| 7.1MB 103kB/s 
Requirement already satisfied: pytz in /usr/local/lib/python3.6/site-packages (from django) (2017.3)
Installing collected packages: django
Successfully installed django-2.0.7

OR use below if you want to install any specific version

$ sudo python3.6 -m pip install django==2.0
Collecting django==2.0
  Downloading https://files.pythonhosted.org/packages/44/98/35b935a98a17e9a188efc2d53fc51ae0c8bf498a77bc224f9321ae5d111c/Django-2.0-py3-none-any.whl (7.1MB)
    100% |████████████████████████████████| 7.1MB 133kB/s 
Requirement already satisfied: pytz in /usr/local/lib/python3.6/site-packages (from django==2.0) (2017.3)
Installing collected packages: django
Successfully installed django-2.0

and you are all set. Please let me if you get any issue while installing expected version

What is Linear Regression

Linear regression attempts to model the relationship between two variables by fitting a linear equation to observed data. One variable is considered to be an explanatory variable, and the other is considered to be a dependent variable. The case of one explanatory variable is called simple linear regression. For more than one explanatory variable, the process is called multiple linear regression.

Linear regression models are often fitted using the least squares approach.

If there appears to be no association between the proposed explanatory and dependent variables (i.e., the scatterplot does not indicate any increasing or decreasing trends), then fitting a linear regression model to the data probably will not provide a useful model. A valuable numerical measure of association between two variables is the correlation coefficient, which is a value between -1 and 1 indicating the strength of the association of the observed data for the two variables.

There are many names for a regression’s dependent variable. It may be called an outcome variable, criterion variable, endogenous variable, or regressand. The independent variables can be called exogenous variables, predictor variables, or regressors.

 

Linear regression using python

Following are the ways to do linear regression using python

  1. statsmodels
  2. scikit-learn
  3. scipy

Linear Regression using statsmodels

Here is sample code

and here is the output

Linear Regression using scikit-learn

Here is the code

and output of this code is as below

Linear Regression using scipy

Sample code

and output

 

If you look at code, it seems finding linear regression using scipy is shortest and easiest to understand.

 

 

 

What is Correlation ?

Correlation is  used to indicate dependence or association is any statistical relationship, whether causal or not, between two random variables or bivariate data. It is a measure of relationship between two mathematical variables or measured data values, which includes the Pearson correlation coefficient as a special case.Correlation is any of a broad class of statistical relationships involving dependence, though in common usage it most often refers to how close two variables are to having a linear relationship with each other.

The strength of the linear association between two variables is quantified by the correlation coefficient.

Formula for correlation is as below

correlation

 

  • The correlation coefficient always takes a value between -1 and 1,
  • Value of 1 or -1 indicating perfect correlation (all points would lie along a straight line in this case).
  • A correlation value close to 0 indicates no association between the variables.The closer the value of r to 0 the greater the variation around the line of best fit.
  • A positive correlation indicates a positive association between the variables (increasing values in one variable correspond to increasing values in the other variable),
  • while a negative correlation indicates a negative association between the variables (increasing values is one variable correspond to decreasing values in the other variable).
Correlation values for different datasets

The square of the correlation coefficient, r², is a useful value in linear regression. This value represents the fraction of the variation in one variable that may be explained by the other variable. Thus, if a correlation of 0.8 is observed between two variables (say, height and weight, for example), then a linear regression model attempting to explain either variable in terms of the other variable will account for 64% of the variability in the data1

the least-squares regression line will always pass through the means of x and y, the regression line may be entirely described by the means, standard deviations, and correlation of the two variables under investigation.

Pearson correlation coefficient

Pearsons correlation coefficient is a measure of the linear correlation between two variables X and Y. It has a value between +1 and −1 2.t is obtained by dividing the covariance of the two variables by the product of their standard deviations.

Formula for Pearson Correlation Coefficient

Rank correlation coefficients

Spearman’s rank correlation coefficient

The Spearman correlation coefficient is defined as the Pearson correlation coefficient between the ranked variables.3

Spearman_fig5

Kendall rank correlation coefficient

the Kendall correlation between two variables will be high when observations have a similar (or identical for a correlation of 1) rank (i.e. relative position label of the observations within the variable: 1st, 2nd, 3rd, etc.) between the two variables, and low when observations have a dissimilar (or fully different for a correlation of -1) rank between the two variables.4

Goodman and Kruskal’s gamma

Goodman and Kruskal’s gamma is a measure of rank correlation, i.e., the similarity of the orderings of the data when ranked by each of the quantities. 5

You can find his report here

Now let us try to calculate these correlations using python, you can find code below

output is as below:

2.
Pearson_correlation_coefficient. wikipedia. https://en.wikipedia.org. Accessed July 14, 2018.
3.
Spearman’s rank correlation coefficient. wikipedia. https://en.wikipedia.org. Accessed July 14, 2018.
4.
Kendall_rank_correlation_coefficient. wikipedia. https://en.wikipedia.org/. Accessed July 14, 2018.
5.
Goodman and Kruskal’s gamma. wikipedia. https://en.wikipedia.org/wiki/Goodman_and_Kruskal%27s_gamma. Accessed July 14, 2018.

What is Regression and Types of Regression

2Regression is a set of statistical processes for estimating the relationships among variables. It includes many techniques for modeling and analyzing several variables, when the focus is on the relationship between a dependent variable (target) and one or more independent variables (or ‘predictors’).

Regression analysis helps one understand how the typical value of the dependent variable (or ‘criterion variable’) changes when any one of the independent variables is varied, while the other independent variables are held fixed.

Usage

It is used in variety of places such as forecasting, time series analysis etc. across industries.

  • Regression analysis is used characterize the variation of the dependent variable around the prediction of the regression function using a probability distribution
  • A function of the independent variables called the regression function is to be estimated
  • Regression analysis can be used to infer causal relationships between the independent and dependent variables. However this can lead to illusions or false relationships, so caution is advisable as correlation does not prove causation.

Types of regression:

  1. Linear Regression
    1. Simple Linear Regression
    2. multiple linear regression.
  2. Logistic Regression
    1. Simple Logistic Regression
    2. Multiple Logistic Regression
  3. Polynomial Regression
  4. Stepwise Regression
  5. Ridge Regression
  6. Lasso Regression
  7. ElasticNet Regression1
1.
Ray S. 7 Types of Regression Techniques you should know! analyticsvidhya. https://www.analyticsvidhya.com. Accessed July 14, 2018.
2.
Regression_analysis. wikipedia. https://en.wikipedia.org/wiki/Regression_analysis. Accessed July 14, 2018.