This document discusses Flask, a Python-based web application framework. It provides an overview of Flask fundamentals like backend development, virtual environments, routes, templates, and error handling. It also covers additional Flask features like extensions, MongoDB integration, and building REST APIs. The document uses code samples and file structure examples to demonstrate how to structure and deploy a Flask application on Heroku.
10. BLUEPRINT
api - www.mywebpage.com/api
admin - www.mywebpage.com/admin
mobile - www.mywebpage.com/mobile
•Modular apps
from . import main
from flask import render_template
@main.route('/')
def index():
return "It's working
@main.route('/hello/<name>')
def hello(name):
return render_template('hello.html', name=name)
11. STATIC FILES + TEMPLATES
from flask import render_template
from . import main
@main.route('/hello/<name>')
def hello(name):
return render_template('hello.html', name=name)
•static files - app/static
•templates - app/templates
12. JINJA2 - TEMPLATING
<title>{% block title %}{% endblock %}</title>
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
13. JADE
<!DOCTYPE html>
<html lang="en">
<head>
<title>Jade</title>
<script type="text/javascript">
if (foo) {
bar(1 + 5)
}
</script>
</head>
<body>
<h1>Jade - node template engine</h1>
<div id="container" class="col">
<p>You are amazing</p>
<p>
Jade is a terse and simple
templating language with a
strong focus on performance
and powerful features.
</p>
</div>
</body>
</html>
doctype html
html(lang="en")
head
title= pageTitle
script(type='text/javascript').
if (foo) {
bar(1 + 5)
}
body
h1 Jade - node template engine
#container.col
if youAreUsingJade
p You are amazing
else
p Get on it!
p.
Jade is a terse and simple
templating language with a
strong focus on performance
and powerful features.
15. ERROR HANDLING
from flask import render_template
from . import main
@main.app_errorhandler(404)
def page_not_found(e):
return render_template('404.jade'), 404
•404 Not Found
•403 Forbidden
•410 Gone
•500 Internal Server Error
16. EXTENSÕES
Flask-Script - scripts externos
Flask-RESTful - REST APIs fácil
Flask-JWT - autenticação por token JWT
Flask-Weasyprint - HTML as PDF
Flask-Mail - email para Flask
Flask-Login - gerenciamento de sessão de usuário
18. REST
GET /api/tarefa - retorna todas as tarefas
GET /api/tarefa/:id - retorna a tarefa :id
PUT /api/tarefa/:id - atualiza a tarefa :id
DELETE /api/tarefa/:id - delete/remove tarefa :id
POST /api/tarefa - cria uma nova tarefa