desbravando a web com python - matheus lima
DESCRIPTION
Desbravando a Web com Python. Palestra apresentada no RuPy Sergipe 2014. Nessa palestra falei um pouco sobre Python e quais frameworks podem nos ajudar nesse desafio.TRANSCRIPT
![Page 1: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/1.jpg)
Matheus Limagithub.com/matheusslfb.com/[email protected]
Por onde começar?
Desbravando a Web com Python
segunda-feira, 24 de março de 14
![Page 2: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/2.jpg)
Apresentação
• Desenvolvedor web;
• Pythonista há 4 anos;
• matheussl no github;
• Python > Django > Web2py > Flask
segunda-feira, 24 de março de 14
![Page 3: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/3.jpg)
Roteiro
• Python
• Frameworks web
• Fabric
• Estudo de caso
segunda-feira, 24 de março de 14
![Page 4: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/4.jpg)
Pai o que?!
Pyton
Pynton
Phyton
phython
Pithon
Python
segunda-feira, 24 de março de 14
![Page 5: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/5.jpg)
Pai o que?!
Pyton
Pynton
Phyton
phython
Pithon
Python
segunda-feira, 24 de março de 14
![Page 6: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/6.jpg)
Por que Python?
• Linguagem simples e com resultados rápidos.
• Escalável
• Baterias incluídas.
segunda-feira, 24 de março de 14
![Page 7: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/7.jpg)
Baterias incluídas?
import osprint os.path.getsize(__file__), 'bytes'>>> 50 bytes
segunda-feira, 24 de março de 14
![Page 8: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/8.jpg)
Baterias incluídas?
file = open(__file__)print file.read()file.close()
segunda-feira, 24 de março de 14
![Page 9: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/9.jpg)
Só isso?
segunda-feira, 24 de março de 14
![Page 10: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/10.jpg)
Indo um pouco além...import urllibimport jsonfrom pprint import pprint
url = 'https://graph.facebook.com/matheeusLimaaa'response = urllib.urlopen(url).read()data = json.loads(response)pprint(data)
{u'first_name': u'Matheus', u'gender': u'male', u'id': u'100001639854566', u'last_name': u'Lima', u'link': u'http://www.facebook.com/MatheeusLimaaa', u'locale': u'en_US', u'name': u'Matheus Lima', u'username': u'MatheeusLimaaa'}
segunda-feira, 24 de março de 14
![Page 11: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/11.jpg)
Um pouco mais...import urllib2import json # https://developers.facebook.com/tools/explorergraph = 'https://graph.facebook.com/search'token = 'CAACEdEose0cBACMVnp2DQs3NgZCy1GUS0U1JJvaiMAEr1aD4qmJF5DFL1rc5eYT4HQoUfo8UeScUZAdMUu2wNIKa2dAwY48ujEeEO792lmn7SLlmdr2YXZAdzPi5JRutETVRfIbDepZAEU7XxEHFGPtt8n3AjdsnXQEdvsRDRtVtvJd74M9rAV6oDMuqYgu2N9qYo7lpUgZDZD'
def search(text): url = '%s?q=%s&type=post&access_token=%s' % (graph, text, token) response = urllib2.urlopen(url).read() data = json.loads(response.decode('utf-8')) return data['data']
for result in search('RuPy Sergipe'): if 'message' in result: print '%s: %s\n' % (result['from']['name'], result['message'])
segunda-feira, 24 de março de 14
![Page 12: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/12.jpg)
Resultado
Breno Moura: Já fez sua inscrição no RuPy Sergipe?
Inscreva-se pelo site oficial: http://rupysergipe.github.io/site
ou pelo eventick: http://eventick.com.br/rupy-sergipe
segunda-feira, 24 de março de 14
![Page 13: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/13.jpg)
E o que mais?• Packages
• easy_install
• pip
• Frameworks
• Django
• Flask
• Pyramid
• Plone
• Deploy
• Fabric
• Heroku
• Webserver
• GUnicorn
segunda-feira, 24 de março de 14
![Page 14: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/14.jpg)
Quem usa Python?
segunda-feira, 24 de março de 14
![Page 15: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/15.jpg)
segunda-feira, 24 de março de 14
![Page 16: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/16.jpg)
O que falam?
• “Python é uma das cinco mais importantes linguagens que todo programador deve conhecer” (Bjarne Stroustup, criador do C++)
• “[...] nossa filosofia aqui é “Python sempre que pudermos, C++ se necessário”” (Alex Martelli, Líder Técnico, Sistemas de Produção, Google)
segunda-feira, 24 de março de 14
![Page 17: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/17.jpg)
O que falam?
• “Python é rápido o suficiente para nosso site e nos permite produzir características de fácil manutenção em tempos recordes, com um mínimo de desenvolvedores.”
(Cuond Do, Arquiteto de Software, Youtube.com)
segunda-feira, 24 de março de 14
![Page 18: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/18.jpg)
Quero usar Python na web,por onde começo?
segunda-feira, 24 de março de 14
![Page 19: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/19.jpg)
Frameworks
• Flask
• Web2py
• Django
segunda-feira, 24 de março de 14
![Page 20: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/20.jpg)
Flask
• Micro-famework;
• Poucas linhas de código;
• Surgiu de uma brincadeira;
segunda-feira, 24 de março de 14
![Page 21: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/21.jpg)
Hello Flask!from flask import Flaskapp = Flask(__name__)
@app.route('/')def hello_world(): return 'Hello World!'
if __name__ == '__main__': app.run()
$ python hello.py * Running on http://127.0.0.1:5000/
segunda-feira, 24 de março de 14
![Page 22: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/22.jpg)
Web2py
• Um pouco mais robusto;
• Interface web de desenvolvimento;
• Simples desde a instalação;
• DAL;
segunda-feira, 24 de março de 14
![Page 23: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/23.jpg)
Django
Django é um framework web de alto nível.
• Escrito em Python
• Estimula o desenvolvimento rápido e limpo (DjangoProject.com)
segunda-feira, 24 de março de 14
![Page 24: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/24.jpg)
Sim, mas como ele pode agilizar meus projetos?
segunda-feira, 24 de março de 14
![Page 25: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/25.jpg)
Arquitetura
• Model-Template-View (MTV)
• Foco na regra de negócio
• O template é do designer
segunda-feira, 24 de março de 14
![Page 26: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/26.jpg)
Arquitetura
• URL dispatcher, middlewares e handlers são do framework!
segunda-feira, 24 de março de 14
![Page 27: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/27.jpg)
Arquitetura de uma app
segunda-feira, 24 de março de 14
![Page 28: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/28.jpg)
Models
• Os models compreendem a camada de domínio da aplicação
• Numa abordagem botton-up, os models são feitos primeiro.
segunda-feira, 24 de março de 14
![Page 29: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/29.jpg)
Models
class Person(models.Model): name = models.CharField(max_length=100) cpf = models.CharField(max_length=11) photo = models.ImageField(uploads_to='person/photo') birth_date = models.DateField(null=True) active = models.BooleanField()
segunda-feira, 24 de março de 14
![Page 30: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/30.jpg)
Models
• Agora conheceremos um pouco da mágica do ORM do Django.
segunda-feira, 24 de março de 14
![Page 31: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/31.jpg)
Modelsjohn = Person( name = 'John', cpf = '12345678900', birthday = date(year=1990, month=5, day=28),)john.save()
if john.is_birthday: print 'Congratulations!!'
# activate
john.activate()john.save()
if john.active: print 'active user'else: print 'inactive user'
segunda-feira, 24 de março de 14
![Page 32: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/32.jpg)
Views
• As views são responsáveis por selecionar os objetos que serão exibidos e executar algumas ações
segunda-feira, 24 de março de 14
![Page 33: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/33.jpg)
Views
• Já temos o model “Person”. Agora, como exibir a lista de pessoas cadastradas?
segunda-feira, 24 de março de 14
![Page 34: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/34.jpg)
Views
def person_list(request): persons = Person.objects.all() context = {'perons': persons} return render(request, 'person_list.html', context)
segunda-feira, 24 de março de 14
![Page 35: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/35.jpg)
Dá trabalho pra publicar?
segunda-feira, 24 de março de 14
![Page 36: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/36.jpg)
$> fab production deploy
Só isso? Como?!
segunda-feira, 24 de março de 14
![Page 37: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/37.jpg)
Fabric
Fabric é uma ferramenta que automatiza tarefas em servidores remotos.
• Deploy;
• Upload/download;
• Qualquer tarefa;
segunda-feira, 24 de março de 14
![Page 38: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/38.jpg)
MeuEstagio.com
• O site foi reescrito em Django;
• Meu novo recorde: 2h
• Recorde anterior: 4h (web2py)
• Desafio do minicurso!
segunda-feira, 24 de março de 14
![Page 39: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/39.jpg)
segunda-feira, 24 de março de 14
![Page 40: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/40.jpg)
MeuEstagio.com
segunda-feira, 24 de março de 14
![Page 41: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/41.jpg)
Código sem burocracia!
segunda-feira, 24 de março de 14
![Page 42: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/42.jpg)
Dúvidas?
segunda-feira, 24 de março de 14
![Page 43: Desbravando a web com python - Matheus Lima](https://reader030.vdocuments.pub/reader030/viewer/2022020711/549652aaac7959222e8b509d/html5/thumbnails/43.jpg)
Matheus Limagithub.com/matheusslfb.com/[email protected]
Por onde começar?
Desbravando a Web com Python
segunda-feira, 24 de março de 14