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

To implement the simplest template rendering, do following

add this statement in urls.py

path('contact/', views.contact, name='contact'),

add this into views.py

def contact(request):
    contact = {'name' : 'pravin','sname':'jd'}
    return render(request, 'polls/contact.html', contact)

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

<h2>This is contact us page </h2>
<h4>Authoris {{name}} {{sname}}</h4><h2>This is contact us page</h2>

and here is the output

Building blocks of django templating language

Variables

Variables look like this: {{ variable }}.When the template engine encounters a variable, it evaluates that variable and replaces it with the result.

When the template system encounters a dot, it tries the following lookups, in this order:

Dictionary lookup
Attribute or method lookup
Numeric index lookup

Filters

You can modify variables for display by using filters. Here are some examples

  • {{ name|lower }}
  • {{ name|upper }}
  • {{ bio|truncatewords:30 }}.

Tags

Tags look like this: {% tag %}. Tags are more complex than variables: Some create text in the output, some control flow by performing loops or logic, and some load external information into the template to be used by later variables.

For Tag

<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% endfor %}
</ul>

if, elif and else tag

{% if athlete_list %}
Number of athletes: {{ athlete_list|length }}
{% elif athlete_in_locker_room_list %}
Athletes should be out of the locker room soon!
{% else %}
No athletes.
{% endif %}

combining tags and filters

{% if athlete_list|length > 1 %}
Team: {% for athlete in athlete_list %} ... {% endfor %}
{% else %}
Athlete: {{ athlete_list.0.name }}
{% endif %}

Comments

To comment-out part of a line in a template, use the comment syntax: {# #}.

  • {#  this is  comment #}
  • {# {% if foo %}bar{% else %} #}

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

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css" />
<title>{% block title %}My amazing site{% endblock %}</title>
</head>

<body>
<div id="sidebar">
{% block sidebar %}
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blog/">Blog</a></li>
</ul>
{% endblock %}
</div>

<div id="content">
{% block content %}{% endblock %}
</div>
</body>
</html>

Here is overriding template

{% extends "base.html" %}

{% block title %}My amazing blog{% endblock %}

{% block content %}
{% for entry in blog_entries %}
<h2>{{ entry.title }}</h2>
<p>{{ entry.body }}</p>
{% endfor %}
{% endblock %}

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

Getting Started with django with ubuntu 16.04

Tag line for django says “The web framework for perfectionists with deadlines.” and most would agree. django is rediculously fast to implement, its fully loaded with lots of utilitis, its secure and at the same time its highly scalable. some of the most popular sites like instagram, pinterest are built with django.

Let us get our hands dirty by getting started with django instead of talking about django features.

Which django version should I use ?

Here are the details about djnago release. you can choose latest version, however, I will stick with LTS version

Which Python version should I use ?

This is the most common question and following table with clarify your query

Django version Python versions
  1.11 2.7, 3.4, 3.5, 3.6
  2.0 3.4, 3.5, 3.6, 3.7
  2.1,2.2 3.5, 3.6, 3.7

In this tutorial we are going to use python version 3.6 (3.6.4 to be very specifuc) and django version 1.11 (since this is LTS)

Install django version 1.11

$ sudo python3.6 -m pip install django==1.11
Collecting django==1.11
Downloading https://files.pythonhosted.org/packages/47/a6/078ebcbd49b19e22fd560a2348cfc5cec9e5dcfe3c4fad8e64c9865135bb/Django-1.11-py2.py3-none-any.whl (6.9MB)
100% |████████████████████████████████| 6.9MB 339kB/s 
Requirement already satisfied: pytz in /usr/local/lib/python3.6/site-packages (from django==1.11) (2017.3)
Installing collected packages: django
Successfully installed django-1.11

You can check the version as below

$ python3.6 -m django --version
1.11

Creating django project

$ django-admin startproject mysite

Now CD into directory “mysite” and run following command

$python3.6 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

July 18, 2018 - 17:43:11
Django version 1.11, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[18/Jul/2018 17:43:31] "GET / HTTP/1.1" 200 1716
Not Found: /favicon.ico

Now you can access django website at location http://127.0.0.1:8000/

here is the output

Creating application

Now that your environment – a “project” – is set up, you’re set to start doing work.

$ python3.6 manage.py startapp polls

after creating polls app, please add following code to polls/views.py


from django.http import HttpResponse

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

Now create a file urls.py and add following code


from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

Now add following code in mysite.urls.py


from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

Here is the output

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.