Flask Guide 101

Flask Guide 101

Heroku setup heroku toolbelt

Creating Environment

Conda Environment

1
2
conda create --name flask
activate flask

Virtualenv Environment

Installing pip

easy_install pip

Installing Virtualenv

pip install virtualenv

Creating virtualenv

virtualenv venv

Activating virtualenv

venv\Scripts\activate

Installing Flask

1
pip install flask

Sample App Flask

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Basic flask application 
# hello.py

from flask import Flask
app=Flask(__name__)




@app.route('/')
def index():
        return '<h1>Learning Flask</h1>'

@app.route('/user/<name>')
def user(name):
        return 'Hello, %s' % format(name)

Running Server

For Windows

1
2
set Flask_App=hello.py
flask run

For Linux

1
2
export Flask_App=hello.py
flask run

or for production flask run --host 0.0.0.0 --port 5200

Flask Commandline

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# filename: cli.py
import click

from flask import Flask
app = Flask(__name__)

@app.cli.command()
def sayhi():
    """Say Hello."""
    click.echo('Hello')
1
2
set Flask_App=cli.py
run sayhi

Response Template

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# filename: food.py

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

from flask import render_template

@app.route('/dinner/')
@app.route('/dinner/<food>')
def eat(food=None):
    return render_template('food.html', food=food)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 <!–– filename: food.html -->
 <!–– Location: base_dir\templates\food.html -->


<!doctype html>
<title>What's for dinner?</title>
{% if food %}
  <h1>I want {{ food }}!</h1>
{% else %}
  <h1>Anything is fine!</h1>
{% endif %}

Integrating Bootstrap

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# filename: food.py
# Location: base_dir\food.py
# Location: base_dir\templates\food.html

from flask import Flask
from flask import request
from flask_bootstrap import Bootstrap

app=Flask(__name__)

Bootstrap(app)
from flask import render_template

@app.route('/dinner/')
@app.route('/dinner/<food>')
def eat(food=None):
    return render_template('food.html', food=food, list=["pizza", "sushi", "quinoa"])
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!-- filename: food.html -->
<!--  Location: base_dir\templates\food.html -->

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

{% block title %}What's for dinner?{% endblock %}

{% block content %}

<div class="container">

    {% if food %}
      <div class="alert alert-success">
      <h1>I want {{ food }}!</h1>
    {% else %}
      <div class="alert alert-info">
      <h1>Anything is fine!</h1>
    {% endif %}
    </div>
    {% if list %}
    <p>Now we'll loop through the list</p>
        <ul>
        {% for n in list %}
            <li><a href="/dinner/{{n}}">{{n}}</li>
        {% endfor %}
        </ul>
    {% endif %}
</div>

{% endblock %}

Create Web Forms

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# filename: food.py
# Location: base_dir\hello.py
# Location: base_dir\templates\hello.html

from flask import Flask, render_template, flash, request
from wtforms import Form, TextField, TextAreaField, StringField, SubmitField
from flask_bootstrap import Bootstrap

# App config.
DEBUG = True
app = Flask(__name__)
Bootstrap(app)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = 'SuperSecretKey'

class NameForm(Form):
    name = TextField('Name:')

@app.route("/", methods=['GET', 'POST'])
def hello():
    form = NameForm(request.form)

    print (form.errors)
    if request.method == 'POST':
        name=request.form['name']
        print (name)

        flash('Hello ' + name)

    return render_template('hello.html', form=form)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!-- filename: food.html -->
<!--  Location: base_dir\templates\food.html -->

{% extends "bootstrap/base.html" %}
{% block title %} Form Demo {% endblock %}
{% block content %}
    <div class="container">
        <h1>Form Demo</h1>

        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                <ul>
                    {% for message in messages %}
                        <li>{{ message[0] }}</li>
                    {% endfor %}
                </ul>
            {% endif %}
        {% endwith %}
        <form action="" method="post">
                <div class="form-group">
                    {{ form.name.label }} {{ form.name(class="form-control",placeholder="What's your name?") }}
                </div>

                <div class="input submit">
                    <button type="submit" class="btn btn-success">Submit</button>
                </div>
            </form>
    </div>
{% endblock %}

CGPA Prediction System

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# filename: cgpaPrediction.py
# Location: base_dir\cgpaPrediction.py
# Location: base_dir\templates\index.html

from flask import Flask, render_template, flash, request
from wtforms import Form, TextField, TextAreaField, StringField, SubmitField
from flask_bootstrap import Bootstrap
import GPA_Prediction_Using_Two_File_Dataset

# App config.
DEBUG = True
app = Flask(__name__)
Bootstrap(app)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = 'SuperSecretKey'

class NameForm(Form):
    sat_score = TextField('SAT Score:')

@app.route("/", methods=['GET', 'POST'])
def cgpaPrediction():
    form = NameForm(request.form)

    if request.method == 'POST':
        sat_score = request.form['sat_score']
        sat_score = GPA_Prediction_Using_Two_File_Dataset.prediction(float(sat_score))

        flash('CGPA = ' + str(sat_score) )

    return render_template('index.html', form=form)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!-- filename: index.html -->
<!--  Location: base_dir\templates\index.html -->

{% extends "bootstrap/base.html" %}
{% block title %} CGPA Prediction {% endblock %}
{% block content %}
<div class="container">
    <h1>CGPA Prediction</h1>


    <form action="" method="post">
        <div class="form-group">
            {{ form.sat_score.label }} {{ form.sat_score(class="form-control",placeholder="What's your SAT Score?") }}
        </div>

        <div class="input submit">
            <button type="submit" class="btn btn-success">Submit</button>
        </div>
    </form>


    {% with messages = get_flashed_messages(with_categories=true) %}
    {% if messages %}
        <br>
        {% for message in messages %}
        <div class="alert alert-info" role="alert">{{ message[1] }}</div>
        {% endfor %}

    {% endif %}
    {% endwith %}
</div>
{% endblock %