This document provides an overview of Flask, a microframework for Python. It discusses that Flask is easy to code and configure, extensible via extensions, and uses Jinja2 templating and SQLAlchemy ORM. It then provides a step-by-step guide to setting up a Flask application, including creating a virtualenv, basic routing, models, forms, templates, and views. Configuration and running the application are also covered at a high level.
In this document
Powered by AI
Presentation by Max Claus Nunes, including contact information and links to personal blog and GitHub.
Introduction to Flask as a micro-framework for Python. Key features include simplicity, excellent documentation, extensibility, and being open-source.
Setting up a coding environment and creating a basic Flask application with 6 lines of code. Instructions for installation and running the app.
Encouragement to expand the application developed in prior slides.
Introduction to SQLAlchemy, a Python SQL ORM. Instructions for installation and defining database models.
Introduction to forms in Flask using Flask-WTF for validation. Example of creating forms.
Creating base templates and specific templates for news listing and creation using Jinja2.
Setting up the core Flask app and configuration for SQLAlchemy and other settings.
Instructions on how to initialize the database and run the application.
Advice on debugging the app and preparing it for publishing.
Providing helpful links related to Flask, SQLAlchemy, and additional resources.
Thanking the audience and concluding the presentation.
FLASK IS
• Flaskis a micro-framework for Python
• Easy to code
• Easy to configure
• Flask won’t make many decisions for
you, such as what database to use.
• Has an excellent documentation
• RESTful
• Testable
4.
FLASK IS
BASED ON WERKZEUG AND JINJA 2
WSGI Template Engine
5.
FLASK IS
EXTENSIBLE AND KEEPS THE CORE SIMPLE
• Flask-Admin • Flask-Restless
• Flask-Cache • Flask-
• Flask-OpendID SQLAlchemy
• Flask-Mail • Flask-Testing
• Flask- • Flask-WTF
MongoAlchemy • Flask-Uploads
SQLALCHEMY
Installing pip install Flask-Sqlalchemy
Coding the Model
from braviapp import db
bravibraviappmodels.py
class News(db.Model):
# Define the properties mapped to database columns
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(100), nullable = False)
text = db.Column(db.Text, nullable = False)
def __init__(self, title, text):
self.title = title
self.text = text
def __repr__(self):
return '<News %r>' % self.title
WTFORMS
Installing pip install Flask-WTF
Coding the Form
bravibraviappmodels.py
# third party imports
from flask.ext.wtf import Form, TextField, TextAreaField,
Required
class NewsCreateForm(Form):
title = TextField('Title', [Required()])
text = TextAreaField('Text', [Required()])
VIEWS
from flask import request, flash, redirect, url_for, render_template
from braviapp import braviapp, db
from braviapp.forms import NewsCreateForm
from braviapp.models import News
@braviapp.errorhandler(404)
def not_found(error):
flash('You tried access a page that not exists')
return redirect(url_for('all'))
bravibraviappviews.py
@braviapp.route('/')
def all():
#from news_model import News
news = News.query.all()
return render_template('news_list.html', news=news)
@braviapp.route('/create/', methods=['GET', 'POST'])
def create():
form = NewsCreateForm(request.form)
# make sure data are valid
if form.validate_on_submit():
news = News(form.title.data, form.text.data)
# save on database
db.session.add(news)
db.session.commit()
flash('The news has been created successfully')
return redirect(url_for('all'))
return render_template('news_create.html', form=form)
23.
CORE APP
bravibraviapp__init__.PY
# third party imports
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
# Initialize the app from Flask
braviapp = Flask(__name__)
braviapp.config.from_object('settings')
db = SQLAlchemy(braviapp)
# local application imports
from braviapp import views
24.
SETTINGS FILE
import os
_basedir= os.path.abspath(os.path.dirname(__file__))
DEBUG = False
bravisettings.py
ADMINS = frozenset(['youremail@yourdomain.com'])
SECRET_KEY = 'SECRET_KEY_FOR_SESSION_SIGNING'
# Define the path of our database inside the root
application, where 'app.db' is the database's name
SQLALCHEMY_DATABASE_URI = 'sqlite:///' +
os.path.join(_basedir, 'app.db')
DATABASE_CONNECT_OPTION = {}
CSRF_ENABLED = True
CSRF_SESSION_KEY = 'SOMETHING_IMPOSSIBLE_TO_GUEES'
25.
SQLALCHEMY
Helper to resetthe database file
braviinitialize_db.py
from app import db
# Drop all tables from db file
db.drop_all()
# Create all tables on db file,
# copying the structure from the definition on the Models
db.create_all()
Running python initialize_db.py
26.
RUNNING
Helper to initializethe application
braviinitialize_app.py
from braviapp import braviapp as application
application.run(debug=True,port=8080)
Running python initialize_app.py
LETS TRY