Flask is a minimalist web framework. It helps the developer to build and maintain websites without having to reuse code or go to extra effort.
The key takeaway from that description is “minimalist”. Flask is quite powerful but doesn’t come overloaded with features out of the box. Taking advantage of its full potential requires turning to third party plugins and extensions.
Because of this, my advice is to use Flask (which I love) for simpler projects that only need a limited feature set. Anything more complicated, turn to Django.
This post is a technical walkthrough for setting up Flask on a Macbook Pro. It assumes you have already installed:
Any line preceeded by a “$” is intended as a terminal command.
Lines to insert into files will be shown as a code snippet. For example:
filename:
line you should copy into file
Anytime you see “project_name”, change it to the name of your project.
$ mkvirtualenv -p python3 project_name
$ pip install flask
$ pip freeze > requirements.txt
$ touch .gitignore
.gitignore:
__pycache__
*.pyc
$ mkdir app
$ mkdir app/static
$ mkdir app/templates
$ mkdir app/templates/includes
$ touch run.py
run.py:
import sys`
from app import app`
app.run(debug=True)`
$ touch __init__.py
__init__.py:
from flask import Flask
app = Flask(__name__)
from app import views
from app import filters
$ touch app/static/styles.css
$ app/templates/base.html
app/templates/base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<header>something</header>
{% block content %}
{% endblock content %}
</body>
<footer></footer>
</html>
$ touch app/templates/index.html
app/templates/index.html:
{% extends "base.html" %}
{% block content %}
<h1>Hello World</h1>
{% endblock content %}
$ touch app/views.py
app/views.py
import os
from flask import render_template
from app import app
@app.route("/")
@app.route("/index")
def index():
return render_template("index.html")
$ touch app/filters.py
app/filters.py
from app import app
$ pip install wtforms
$ pip freeze > requirements.txt
$ touch app/forms.py
app/forms.py
from wtforms import Form, StringField, RadioField, validators, ValidationError
class FormName (Form):
attribute1 = StringField ("Something", validators=[validators.DataRequired()])
attribute2 = RadioField ( "Name",
default = "option",
choices = [
("choice1", "choice1",),
("choice2", "choice2",),
]
)