FLASK - BACKEND COM PYTHON
SIBELIUS SERAPHINI 2010-2015 Bacharel em Ciências de Computação 2015 Full Stack Developer
O QUE É BACKEND?
Backend Frontend Mobile Database
LET’S CODE
VIRTUALENV # Cria um virtualenv com nome hello com Python3 mkvirtualenv hello -p /usr/local/bin/python3 # Lista virtualenvs workon # inicia virtualenv hello workon hello # desativa virtualenv deactivate
#!/usr/bin/env python3 from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello SemComp!' if __name__ == '__main__': app.run() $ pip install Flask $ python hello.py * Running on http://localhost:5000/ git clone https://github.com/sibeliusseraphini/hello-flask.git
DEPLOY TO 1. Criar um app no Heroku - heroku apps:create semcomp18 2. Adicionar runtime.txt - python-3.4.2 3. Adicionar Procfile - web: uwsgi uwsgi.ini 4. Install uWSGI - pip install uwsgi 5. Adicionar configuração do uWSGI - uwsgi.ini 6. Criar requirements.txt - pip freeze > requirements.txt 7. Realizar release - git push heroku master [uwsgi] http-socket = :$(PORT) master = true processes = 8 threads = 4 die-on-term = true module = hello:app memory-report = true offline-threads = 4 harakiri = 500 harakiri-verbose = true
ESTRUTURA FLASK ├── Procfile ├── manage.py ├── requirements.txt ├── runtime.txt ├── semcomp │   ├── __init__.py │   ├── api │   │   ├── __init__.py │   │   ├── routes.py │   │   └── tarefa.py │   ├── main │   │   ├── __init__.py │   │   ├── errors.py │   │   └── views.py │   ├── models.py │   ├── static │   └── templates │   └── 404.jade │   └── base.html └── uwsgi.ini App Grande git clone https://github.com/sibeliusseraphini/semcomp-flask.git
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)
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
JINJA2 - TEMPLATING <title>{% block title %}{% endblock %}</title> <ul> {% for user in users %} <li><a href="{{ user.url }}">{{ user.username }}</a></li> {% endfor %} </ul>
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.
PYJADE (JINJA2 + JADE) app.jinja_env.add_extension(‘pyjade.ext.jinja.PyJadeExtension') pip install pyjade
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
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
MONGODB PyMongo - Mongoengine
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
REST Sem extensões
REST Com extensões

Flask - Backend com Python - Semcomp 18

  • 1.
    FLASK - BACKENDCOM PYTHON
  • 2.
    SIBELIUS SERAPHINI 2010-2015 Bacharelem Ciências de Computação 2015 Full Stack Developer
  • 3.
    O QUE ÉBACKEND?
  • 4.
  • 5.
  • 6.
    VIRTUALENV # Cria umvirtualenv com nome hello com Python3 mkvirtualenv hello -p /usr/local/bin/python3 # Lista virtualenvs workon # inicia virtualenv hello workon hello # desativa virtualenv deactivate
  • 7.
    #!/usr/bin/env python3 from flaskimport Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello SemComp!' if __name__ == '__main__': app.run() $ pip install Flask $ python hello.py * Running on http://localhost:5000/ git clone https://github.com/sibeliusseraphini/hello-flask.git
  • 8.
    DEPLOY TO 1. Criarum app no Heroku - heroku apps:create semcomp18 2. Adicionar runtime.txt - python-3.4.2 3. Adicionar Procfile - web: uwsgi uwsgi.ini 4. Install uWSGI - pip install uwsgi 5. Adicionar configuração do uWSGI - uwsgi.ini 6. Criar requirements.txt - pip freeze > requirements.txt 7. Realizar release - git push heroku master [uwsgi] http-socket = :$(PORT) master = true processes = 8 threads = 4 die-on-term = true module = hello:app memory-report = true offline-threads = 4 harakiri = 500 harakiri-verbose = true
  • 9.
    ESTRUTURA FLASK ├── Procfile ├──manage.py ├── requirements.txt ├── runtime.txt ├── semcomp │   ├── __init__.py │   ├── api │   │   ├── __init__.py │   │   ├── routes.py │   │   └── tarefa.py │   ├── main │   │   ├── __init__.py │   │   ├── errors.py │   │   └── views.py │   ├── models.py │   ├── static │   └── templates │   └── 404.jade │   └── base.html └── uwsgi.ini App Grande git clone https://github.com/sibeliusseraphini/semcomp-flask.git
  • 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> <scripttype="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.
  • 14.
    PYJADE (JINJA2 +JADE) app.jinja_env.add_extension(‘pyjade.ext.jinja.PyJadeExtension') pip install pyjade
  • 15.
    ERROR HANDLING from flaskimport 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 - scriptsexternos 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
  • 17.
  • 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
  • 19.
  • 20.