python para desarrolladores web
TRANSCRIPT
![Page 1: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/1.jpg)
http://creativecommons.org/licenses/by-nc-sa/3.0/
Leganés, 11 y 12 de
febrero
José Manuel Ortega
Leganés, 11 y 12 de febrero
José Manuel Ortega
@jmortegac
Python para desarrolladores web
![Page 3: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/3.jpg)
Stack,Django,Flask,Pyramid
Requests
Scraping(BeautifulSoap,Scrapy)
Bokeh
API Rest (Django-Rest-Framework)
Twitter API
3 Python para desarrolladores web
Agenda
![Page 4: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/4.jpg)
4 Python para desarrolladores web
Python Zen
![Page 5: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/5.jpg)
Stack
5 Python para desarrolladores web
![Page 6: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/6.jpg)
Django
6 Python para desarrolladores web
![Page 7: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/7.jpg)
Django
7
Most popular Python web framework
Full stack web framework Django-ORM
Template Engine
HTTP Request/Response
URL Routing Mechanism
Testing
Dev Server (WSGI)
Python para desarrolladores web
![Page 8: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/8.jpg)
Django
8
Model Template View Model → modelo de datos (models.py)
View →vistas de datos (views.py): qué datos se presentan
Template → plantillas HTML:cómo se presentan los datos
Python para desarrolladores web
![Page 9: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/9.jpg)
Django
9
django.contrib.auth Un sistema de autenticación
django.contrib.contenttypes Un framework para
tipos de contenidos
django.contrib.sessions Un framework para
manejar sesiones
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mysite.db',
}
}
Python para desarrolladores web
![Page 10: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/10.jpg)
Django ORM
10 Python para desarrolladores web
![Page 11: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/11.jpg)
Django Templates
11
Templates are composed by Blocks
Python para desarrolladores web
![Page 12: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/12.jpg)
Django example project
12 Python para desarrolladores web
![Page 13: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/13.jpg)
Django example project
13 Python para desarrolladores web
![Page 14: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/14.jpg)
Django-oscar
14
http://oscarcommerce.com
https://github.com/django-oscar/django-oscar
Python para desarrolladores web
![Page 15: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/15.jpg)
Flask
15
Microframework
Built in development server and debugger
Oriented to small applications with simpler
requirements.
Python para desarrolladores web
![Page 16: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/16.jpg)
Flask example
16 Python para desarrolladores web
![Page 17: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/17.jpg)
Flask
17 Python para desarrolladores web
![Page 19: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/19.jpg)
Pyramid
19
Python3 compatible
Templates with Chamaleon
Like django has his own bootstraping
toolpcreate
Python para desarrolladores web
![Page 20: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/20.jpg)
Pyramid
20
pcreate -s starter hello_pyramid
├── CHANGES.txt
├── development.ini
├── MANIFEST.in
├── production.ini
├── hello_pyramid
│ ├── __init__.py
│ ├── static
│ │ ├── pyramid-16x16.png
│ │ ├── pyramid.png
│ │ ├── theme.css
│ │ └── theme.min.css
│ ├── templates
│ │ └── mytemplate.pt
│ ├── tests.py
│ └── views.py
├── README.txt
└── setup.py
Python para desarrolladores web
![Page 21: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/21.jpg)
Pyramid
Learn REST API with Python in 90 minutes 21
![Page 22: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/22.jpg)
Pyramid
22 Python para desarrolladores web
Pserve development.ini
![Page 23: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/23.jpg)
Pyramid
23
https://badges.fedoraproject.org
https://github.com/fedora-infra/tahrir
Python para desarrolladores web
![Page 24: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/24.jpg)
Comparing web frameworks
24
Django Pyramid Flask
ORM Django-
ORM
Bootstrapping django-
admin
pcreate
Templates Chameleon
Migrations
Admin
interface
Visual debug
Python para desarrolladores web
![Page 25: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/25.jpg)
Requests Module
Python’s standard urllib2,httplib module provides most
of the HTTP capabilities you need, but the API is
thoroughly broken.
Python HTTP: Requests. Beautiful, simple, Pythonic.
25
http://www.python-requests.org
Python para desarrolladores web
![Page 26: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/26.jpg)
Use requests module
Install requests module (pip install requests) http://docs.python-requests.org/en/latest/user/install/#install
Use it interactive mode $ python
>>> import requests
>>> r = requests.get("http://httpbin.org/get")
26 Python para desarrolladores web
![Page 27: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/27.jpg)
Use requests module
27 Python para desarrolladores web
![Page 28: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/28.jpg)
Use requests module
28 Python para desarrolladores web
![Page 29: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/29.jpg)
Beautiful Soup
29
Librería que permite el parseo de páginas web
Soporta parsers como lxml,html5lib
Instalación
pip install lxml
pip instlal html5lib
pip install beautifulsoup4
Python para desarrolladores web
![Page 30: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/30.jpg)
Obtain links with bs4
30 Python para desarrolladores web
![Page 31: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/31.jpg)
Obtain links with bs4
31 Python para desarrolladores web
![Page 32: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/32.jpg)
Extract images with lxml
32
import requests
from lxml import html
Python para desarrolladores web
![Page 33: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/33.jpg)
Extract program
33 Python para desarrolladores web
![Page 34: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/34.jpg)
JSON in python
34
import json
json_dumps(data, sort_keys=True)
Maybe allow human readable output
json_dumps(data, sort_keys=True, indent=4)
Python para desarrolladores web
![Page 35: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/35.jpg)
Scrapy
35 Python para desarrolladores web
![Page 36: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/36.jpg)
Scrapy / pip install scrapy
Learn REST API with Python in 90 minutes 36
![Page 37: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/37.jpg)
Scrapy shell
37
scrapy shell <url>
from scrapy. import Selector
hxs = Selector(response)
Info = hxs.select(‘//div[@class=“slot-inner”]’)
Python para desarrolladores web
![Page 38: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/38.jpg)
Scrapy shell
38 Python para desarrolladores web
![Page 39: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/39.jpg)
Scrapy spiders
39 Python para desarrolladores web
![Page 40: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/40.jpg)
Scrapy spiders
40
$ scrapy crawl <spider_name> $ scrapy crawl <spider_name> -o items.json -t json
$ scrapy crawl <spider_name> -o items.csv -t csv
$ scrapy crawl <spider_name> -o items.xml -t xml
Python para desarrolladores web
![Page 41: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/41.jpg)
Twisted
41
Event-driven networking engine
Procesar eventos de forma asíncrona
https://twistedmatrix.com
Python para desarrolladores web
![Page 42: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/42.jpg)
Bokeh
42
Interactive, browser-based visualization for big
data, driven from Python
http://bokeh.pydata.org
Rich interactivity over large datasets
HTML5 Canvas
Integration with Google Maps
http://bokeh.pydata.org/en/latest/docs/gallery.html
Python para desarrolladores web
![Page 43: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/43.jpg)
Bokeh
43 Python para desarrolladores web
![Page 44: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/44.jpg)
Bokeh Google maps
44 Python para desarrolladores web
![Page 45: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/45.jpg)
Bokeh Google maps
45 Python para desarrolladores web
![Page 46: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/46.jpg)
Javascript code generation
46 Python para desarrolladores web
![Page 47: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/47.jpg)
Bokeh charts
47 Python para desarrolladores web
![Page 48: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/48.jpg)
API Rest in Python
48
Django Rest Framework (DRF)
pip install djangorestframework
Python para desarrolladores web
![Page 49: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/49.jpg)
Django Rest Framework
49
Serializers & Views
Python para desarrolladores web
class PostSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Post
fields = ('author', 'title', 'text', 'created_date','published_date')
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
from rest_framework import routers, serializers, viewsets
![Page 50: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/50.jpg)
Django Rest Framework
50
Urls
Python para desarrolladores web
![Page 51: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/51.jpg)
Django Rest Framework
51 Python para desarrolladores web
![Page 52: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/52.jpg)
Twitter API
52
https://apps.twitter.com
Oauth2
Consumer API (API KEY)
Consumer Secret (API Secret)
Access Token
Access Token Secret
Python para desarrolladores web
![Page 53: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/53.jpg)
Twitter API
53
Python-twitter
https://github.com/bear/python-twitter
Tweepy
https://github.com/tweepy/tweepy
Python para desarrolladores web
![Page 54: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/54.jpg)
Tweepy
54
import tweepy
from tweepy import OAuthHandler
consumer_key = 'YOUR-CONSUMER-KEY'
consumer_secret = 'YOUR-CONSUMER-SECRET'
access_token = 'YOUR-ACCESS-TOKEN'
access_secret = 'YOUR-ACCESS-SECRET'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
Python para desarrolladores web
![Page 55: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/55.jpg)
Tweepy
Learn REST API with Python in 90 minutes 55
Timeline
Tweet list
Trending topic
for status in tweepy.Cursor(api.home_timeline).items(10):
print(status.text)
for tweet in tweepy.Cursor(api.user_timeline).items():
process_or_store(tweet._json)
def getTrends():
trends = api.trends_place(1)[0]['trends']
return trends
![Page 56: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/56.jpg)
Python-twitter
56
import twitter
apiTwitter = twitter.Api(consumer_key="xxx", consumer_secret="xxx",
access_token_key="xxx", access_token_secret="xxx")
query = apiTwitter.GetSearch("#T3chFest2016“,count=50)
for result in query:
tweet = {}
tweet['text'] = result.text.encode('utf-8')
tweet['date'] = result.created_at.encode('utf-8')
tweet['favorite_count'] = result.favorite_count
tweet['lang'] = result.lang.encode('utf-8')
tweet['retweet_count'] = result.retweet_count
tweet['account'] = result.user.screen_name.encode('utf-8')
twitter_results.append(tweet)
Python para desarrolladores web
![Page 57: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/57.jpg)
57 Python para desarrolladores web
Python-twitter
![Page 58: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/58.jpg)
Python-twitter
58
outfile = open('twitter.json','wb')
for twitter_result in twitter_results:
line = json.dumps(twitter_result) + "\n"
outfile.write(line)
Python para desarrolladores web
![Page 59: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/59.jpg)
Python in the cloud
59 Python para desarrolladores web
![Page 60: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/60.jpg)
Python in the cloud
60 Python para desarrolladores web
![Page 61: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/61.jpg)
Python in the cloud
61 Python para desarrolladores web
![Page 62: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/62.jpg)
Git
https://github.com/jmortega/t3chfest_python_examples
62 Python para desarrolladores web
![Page 63: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/63.jpg)
References
Django project: http://www.djangoproject.com
Flask: http://flask.pocoo.org
Pyramid: http://www.pylonsproject.org
Requests python module: http://www.python-requests.org
BeautifulSoup: http://www.crummy.com/software/BeautifulSoup
Bokeh:http://bokeh.pydata.org
Django Rest:http://www.django-rest-framework.org
https://www.pythonanywhere.com
63 Python para desarrolladores web
![Page 64: Python para desarrolladores web](https://reader033.vdocuments.pub/reader033/viewer/2022042604/58a250c21a28abe8738b5b5b/html5/thumbnails/64.jpg)
Thank you! José Manuel Ortega <@jmortegac>