Presentation slides for a talk given at SF Python Meetup, 2014-02-12.
JavaScript frameworks are a really exciting tool for building super slick one-page apps. However, if you want them to play nice with a Python backend web framework (Django, Pyramid, Flask, etc.) you're going to have to flip ...
Presentation slides for a talk given at SF Python Meetup, 2014-02-12.
JavaScript frameworks are a really exciting tool for building super slick one-page apps. However, if you want them to play nice with a Python backend web framework (Django, Pyramid, Flask, etc.) you're going to have to flip a few of your design patterns and thought patterns.
Size: 95.41 KB
Language: en
Added: Feb 12, 2014
Slides: 15 pages
Slide Content
Python for AngularJS
AngularJS.$inject(Python)
Huh? Angular?
•JavaScript framework for SPAs
•“Extends” HTML vocabulary
•Controllers, services, templates
But wait! Data!
angular.module('app', [])
!
.factory('Exchange', function ($http) {
return {
getConversion: function () {
return $http.get('http://suchdoge.com/api/exchange/' );
}
}
})
!
.controller('TradeCtrl', function ($scope, Exchange) {
Exchange.getConversion().then( function (result) {
$scope.conversion = result.conversion;
});
});
But wait! Data!
•Write a service in Angular
•Write an API endpoint in Python
But wait! Data!
import json
from django.http import HttpResponse
from doge.models import DogeConversion
!
def exchange(request):
"""API endpoint for requesting current BTC-DOGE exchange rate."""
conversion = DogeConversion.objects. latest('timestamp')
response = {'conversion': conversion.rate}
return HttpResponse(
json.dumps(response),
content_type= 'application/json'
)
But wait! Data!
•Write a service in Angular
•Write an API endpoint in Python
•Ew, hard-coded URLs? Fix!
But wait! Data!
angular.module('app', [])
!
.config(function (RestangularProvider) {
RestangularProvider.setBaseUrl( '/api');
})
!
.factory('Exchange', function (Restangular) {
return {
getConversion: function () {
return Restangular.all('exchange');
}
};
})
But wait! Data!
•Write a service in Angular
•Write an API endpoint in Python
•Ew, hard-coded URLs? Fix!
•Ew, hand-written API views? Fix!
But wait! Data!
from rest_framework import viewsets
from doge.models import DogeConversion
!
class DogeConversionViewSet (viewsets.ModelViewSet):
model = DogeConversion