El documento presenta una introducción a Python para desarrolladores web, resumiendo varios frameworks como Django, Flask y Pyramid, así como librerías como Requests, Beautiful Soup y Scrapy. También cubre temas como APIs REST, visualización de datos con Bokeh y el uso de Python en la nube.
7. 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
8. Django
8
Model TemplateView
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
9. 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
15. Flask
15
Microframework
Built in development server and debugger
Oriented to small applications with simpler
requirements.
Python para desarrolladores web
24. 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
25. 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
26. 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
29. 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
42. 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
48. API Rest in Python
48
Django Rest Framework (DRF)
pip install djangorestframework
Python para desarrolladores web
49. 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
52. 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
53. Twitter API
53
Python-twitter
https://github.com/bear/python-twitter
Tweepy
https://github.com/tweepy/tweepy
Python para desarrolladores web
54. 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
55. Tweepy
Learn REST API with Python in 90 minutes55
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
56. 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