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.
7. 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'
)
8. But wait! Data!
•
Write a service in Angular
•
Write an API endpoint in Python
•
Ew, hard-coded URLs? Fix!
9. But wait! Data!
angular.module('app', [])
!
.config(function (RestangularProvider) {
RestangularProvider.setBaseUrl('/api');
})
!
.factory('Exchange', function (Restangular) {
return {
getConversion: function () {
return Restangular.all('exchange');
}
};
})
10. 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!
11. But wait! Data!
from rest_framework import viewsets
from doge.models import DogeConversion
!
class DogeConversionViewSet(viewsets.ModelViewSet):
model = DogeConversion