Getting started with Flask

The below gets you the route to which a definition is routed to.

from filename import appapp.url_map

Routing:

Simple page rendering

@app.route('/')
@app.route('/index')

Structure

app.py - server file
static - contains css, js, images and library files
template - contains html files

Post request/Get request

from flask import request
@app.route('/form-example', methods=['GET', 'POST'])

Get parameters

request.args[]
request.args.get()

request.form[]
request.form.get()
request.get_json()

Default Routing

Static route is a default route where you can place all the images. Run python as usual, go to localhost:8080/static/flasklogo.png to see the static file (flasklogo.png).

Error route handling

@app.errorhandler(404)

Debug Mode

app.run(debug=True)

MVC model

Models (“M” in MVC): Databases

Template (“Views” in MVC): Jinja2

Views (“Controller” in MVC): Generates Http request and URL mapping

Add URL links

<link rel="stylesheet" type="text/css" href="{{url_for('static', filename='css/mycss.css')}}"><a href="{{ url_for('link') }}">LINK</a>

Templating

{% block title%} 
#common styling exported from view.html, you can only deal with content here
{% endblock %}

Consultant