SlideShare a Scribd company logo
The Role of Python in SPAs
(Single-Page Applications)
David Gibbons
Python Ireland Meetup
July 2018
Software engineer at OpenApp.
Working with Python for over six years, previously with Cylon and Ammeon.
Server and client-side Web development - Linux, Postgres, Python, JavaScript, CSS, HTML.
Based on customer requirements, create bespoke Web applications.
About Me
2
@davgibbs
@davgibbs
About OpenApp
Founded in 2002, currently employ 40 staff.
Web applications using Python for Healthcare industry.
Main areas:
● Patient Registries
● Clinical Patient Management Systems - European Reference Network
● Geo Spatial Analytics
● Health Data Analytics
Always interested in interesting CVs (developers, PMs, QA and test automation, etc).
hr@openapp.ie
3
About Talk
Python on the Web
Introduction to SPAs
Python SPA Architecture
“Movie Gallery” Application
API Design for SPA
Demo
4
Python on the Web
5
Python on the Web
Simplicity and Scalability.
Much built-in functionality and extension frameworks.
Many Web companies including:
6
https://stackshare.io/
Python Developer Survey October 2017
9,500 respondents in 150 countries.
50% said also use JavaScript and 49% also use HTML/CSS.
The primary type of development done by Python developers:
7
Introduction to SPAs
8
What is a SPA?
9
SPAs (Single-Page Applications) are Web applications that load a single HTML page and
dynamically update that page as the user interacts with the application.
SPA in a nutshell
No Browser refreshes
- Views are portions of the DOM (Document Object Model) that make up screen.
Presentation logic in the client
- Combining HTML template and data is done in the client.
Server transactions
- Only data is sent and received during transactions after initial load.
10
11
12
SPA Advantages
User experience - no full page reload means faster page load after the initial page load. Less
‘flicker’ during transitions.
Decoupled presentation layer - allows client-side and server-side code to be maintained and tested
separately.
13
SPA Advantages and Disadvantages
User experience - no full page reload means faster page load after the initial page load. Less
‘flicker’ during transitions
Decoupled presentation layer - allows client-side and server-side code to be maintained and tested
separately
SEO - Web crawlers, used by search engines, have more difficulty analysing pages created
dynamically.
JavaScript dependency - needs to be enabled.
14
Screen Views
15
Views are swapped in and out of HTML container div. For example “Home” and “About” content.
Screen Regions
16
Divide the screen into regions with associated an associated view.
Screen Regions - Gmail
17
Header region
Content
region
Sidebar
region
AJAX - Core of SPAs
Asynchronous JavaScript and XML
It is a set of Web development techniques involving a means of unobtrusively requesting data
from the server, combined with JavaScript’s power to update the HTML DOM.
Makes use of XMLHttpRequest (XHR) API transfers data between web browser and server.
Term Ajax first publicly used in 2005 based on techniques in Google pages.*
18
*http://adaptivepath.org/ideas/ajax-new-approach-web-applications/
Python SPA Architecture
19
Set up a Python API which can be queried by client-side JavaScript code.
Python Frameworks: handle database integration, session management and template generation.
Help organise code.
SPA Application Programming Interface (API)
20
Client Server
API
JSON
AJAX
● RESTful API creation (Django REST framework)
● Sending Email
● CSV creation
● PDF creation (Reportlab)
● Asynchronous tasks (Celery)
● WebSockets (Channels)
● Complex computation (NumPy)
Server-side tasks
21
“Movie Gallery” Application
22
“Movie Gallery” Application
Built to demonstrate SPA concepts.
Movie information stored along with an image and rating.
Can search and paginate through list of movies.
Login to add, edit and delete movies. If not authenticated, then view-only movies.
Python (Django) on the server-side and JavaScript (AngularJS) on client-side.
23
24
25
Screen Regions
“Header” and “Footer” regions stay the same, while “Content” region is updated:
API Design for SPA
26
27
movies/models.py
from django.core.validators import MaxValueValidator
from django.db import models
from django.contrib.auth.models import User
class MovieGenre(models.Model):
name = models.CharField(max_length=100, unique=True)
class Movie(models.Model):
title = models.CharField(max_length=100, unique=True)
summary = models.TextField(max_length=400, blank=True, default='')
release_year = models.PositiveIntegerField(default=2016)
director = models.CharField(max_length=100, blank=True, default='')
genre = models.ForeignKey(MovieGenre, related_name='movie_genre', default=1)
image = models.ImageField(upload_to='movies/', default='movies/Movie.jpg')
rating = models.PositiveIntegerField(validators=[MaxValueValidator(5)], default=3)
created_date = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
28
spa_movies/urls.py
from django.conf import settings
from django.conf.urls import url, include, static
from django.contrib import admin
from django.views.generic import TemplateView
from movies.urls import router
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='movies/index.html')),
url(r'^api/', include(router.urls)),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^admin/', admin.site.urls)
] + static.static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
29
Define the root URLs for the application - example.com will direct to movies/index.html.
RESTful API
In order to create a testable and maintainable API, a RESTful API was setup for the JavaScript
client to query.
REST (REpresentational State Transfer) is an architecture style. URLs built around each resource
(movies, etc).
An architecture that uses a 4 main HTTP actions: GET, POST, PUT, DELETE.
Django REST framework used - browsable API, good documentation.
To handle the authentication, django-rest-auth library is used - default Session Authentication.
30
URL HTTP Verb Result
1 /api/movies GET Returns all movies
2 /api/movies POST Create a new movie
3 /api/movies/:id GET Returns single movie
4 /api/movies/:id PUT Updates an existing movie
5 /api/movies/:id DELETE Deletes an existing movie
6 /api/movie-genres GET Returns all movie genres
7 /api/user-status/ GET Returns true if user logged-in
8 /rest-auth/login/ POST Empty or error message
9 /rest-auth/user/ GET Returns user info
10 /rest-auth/logout/ POST Logs user out 31
movies/serializers.py
from rest_framework import serializers
from .models import MovieGenre, Movie
class MovieGenreSerializer(serializers.ModelSerializer):
class Meta:
model = MovieGenre
fields = ('id', 'name')
class MovieSerializer(serializers.ModelSerializer):
genre_obj = MovieGenreSerializer(source='genre', read_only=True)
user_obj = UserSerializer(source='user', read_only=True)
class Meta:
model = Movie
fields = ('id', 'title', 'summary', 'release_year', 'director', 'genre',
'genre_obj', 'image', 'rating', 'user', 'user_obj')
32
“ModelSerializer” will generate a set of fields and validators based on the model:
movies/views.py
“ModelViewSet” combines the logic for a set of related views in a single class for models
from rest_framework import viewsets, permissions
from rest_framework.decorators import permission_classes
from .permissions import IsOwnerOrReadOnly, IsReadOnly
from .serializers import MovieGenreSerializer, MovieSerializer
from .models import Movie, MovieGenre
class MovieGenreViewSet(viewsets.ModelViewSet):
permission_classes = (IsReadOnly, )
queryset = MovieGenre.objects.all().order_by('name')
serializer_class = MovieGenreSerializer
class MovieViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly)
queryset = Movie.objects.all().order_by('title')
serializer_class = MovieSerializer
33
movies/urls.py
The “DefaultRouter” class is imported and used to create standard APIs.
Get and CRUD (CReate, Update and Delete) URLs for movies and movies genre models set up:
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter(trailing_slash=False)
router.register(r'movies-genres', views.MovieGenreViewSet)
router.register(r'movies', views.MovieViewSet)
34
Demo
35
36
Testing
“Code without tests is broken by design”
- Jacob Kaplan-Moss, one of original Django developers
37
Testing
“Code without tests is broken by design”
- Jacob Kaplan-Moss, one of original Django developers
Automated testing allows easier code maintenance.
Use the Django REST framework “APIClient” class to create client used to run tests.
Run code coverage tool at same time to test Python code coverage:
coverage3 run --source='./apps' --omit='*migrations*' apps/manage.py test movies
movies/tests.py
from django.test import TestCase
from rest_framework.test import APIClient
from movies.models import Movie
class MovieTestCase(TestCase):
def test_add_movie(self):
self.assertEqual(len(Movie.objects.all()), 0)
user = User.objects.create_user('admin', 'myemail@test.com', 'password123')
client = APIClient()
client.login(username='admin', password='password123')
response = client.post('/api/movies', {'title': 'Lion King',
'summary': 'Lion Movie',
'release_year': '1994',
'rating': 5,
'director': 'Roger Allers'})
self.assertEqual(response.status_code, 201)
self.assertEqual(len(Movie.objects.all()), 1)
38
Summary
Python on the Web
Introduction to SPAs, dividing screen into regions and views, use of AJAX.
Python SPA architecture and server-side tasks.
“Movie Gallery” application with Python API to demo SPA.
Built a powerful and testable API with very few lines of code.
SPA JavaScript code still needed.
Complete code available at https://github.com/davgibbs/movies-spa
39
References
1. SPA Design and Architecture. Understanding single-page
web applications by Emmit A. Scott, Jr.:
www.manning.com/books/spa-design-and-architecture
2. Sitepoint: Creating a CRUD App in Minutes with
Angular’s $resource:
ww.sitepoint.com/creating-crud-app-minutes-angulars-reso
urce/
3. Single-Page Applications: Build Modern, Responsive Web
Apps with ASP.NET:
https://msdn.microsoft.com/en-us/magazine/dn463786.aspx
4. Python Developers Survey 2017 Results:
https://www.jetbrains.com/research/python-developers-sur
vey-2017/
40
Thank you!
41

More Related Content

What's hot (20)

PDF
Oleh Zasadnyy "Progressive Web Apps: line between web and native apps become ...
IT Event
 
PDF
Vaadin Components
Joonas Lehtinen
 
PDF
Building Universal Web Apps with React ForwardJS 2017
Elyse Kolker Gordon
 
PDF
Vaadin & Web Components
Joonas Lehtinen
 
PDF
Workshop: Building Vaadin add-ons
Sami Ekblad
 
PDF
Web Components for Java Developers
Joonas Lehtinen
 
PDF
AngularJS Project Setup step-by- step guide - RapidValue Solutions
RapidValue
 
PDF
Java Web Programming on Google Cloud Platform [1/3] : Google App Engine
IMC Institute
 
PDF
Flash Platformアップデート
Mariko Nishimura
 
PDF
Modular Test-driven SPAs with Spring and AngularJS
Gunnar Hillert
 
PDF
Hackathon - Building vaadin add on components
Joonas Lehtinen
 
PDF
How to Build ToDo App with Vue 3 + TypeScript
Katy Slemon
 
PPTX
Grails Advanced
Saurabh Dixit
 
PDF
webcomponents (Jfokus 2015)
Hendrik Ebbers
 
PDF
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch
 
PPTX
Plug in development
Lucky Ali
 
PDF
Introduction to React for Frontend Developers
Sergio Nakamura
 
TXT
Upload[1]
mirjana stojanova
 
PDF
The web - What it has, what it lacks and where it must go - Istanbul
Robert Nyman
 
PDF
AtlasCamp 2015: Using add-ons to build add-ons
Atlassian
 
Oleh Zasadnyy "Progressive Web Apps: line between web and native apps become ...
IT Event
 
Vaadin Components
Joonas Lehtinen
 
Building Universal Web Apps with React ForwardJS 2017
Elyse Kolker Gordon
 
Vaadin & Web Components
Joonas Lehtinen
 
Workshop: Building Vaadin add-ons
Sami Ekblad
 
Web Components for Java Developers
Joonas Lehtinen
 
AngularJS Project Setup step-by- step guide - RapidValue Solutions
RapidValue
 
Java Web Programming on Google Cloud Platform [1/3] : Google App Engine
IMC Institute
 
Flash Platformアップデート
Mariko Nishimura
 
Modular Test-driven SPAs with Spring and AngularJS
Gunnar Hillert
 
Hackathon - Building vaadin add on components
Joonas Lehtinen
 
How to Build ToDo App with Vue 3 + TypeScript
Katy Slemon
 
Grails Advanced
Saurabh Dixit
 
webcomponents (Jfokus 2015)
Hendrik Ebbers
 
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch
 
Plug in development
Lucky Ali
 
Introduction to React for Frontend Developers
Sergio Nakamura
 
The web - What it has, what it lacks and where it must go - Istanbul
Robert Nyman
 
AtlasCamp 2015: Using add-ons to build add-ons
Atlassian
 

Similar to The Role of Python in SPAs (Single-Page Applications) (20)

PDF
Design Patterns in Automation Framework.pdf
ArunVastrad4
 
PDF
بررسی چارچوب جنگو
railsbootcamp
 
PDF
GDG Addis - An Introduction to Django and App Engine
Yared Ayalew
 
PPTX
Web development with django - Basics Presentation
Shrinath Shenoy
 
PPTX
Googleappengineintro 110410190620-phpapp01
Tony Frame
 
PDF
A Basic Django Introduction
Ganga Ram
 
PPTX
SharePoint Saturday Atlanta 2015
Pushkar Chivate
 
PDF
James Jara Portfolio 2014 Part 1
James Jara
 
ODP
Portafolio
James Jara
 
ODP
Gears User Guide
Muthuselvam RS
 
PDF
Django for mobile applications
Hassan Abid
 
KEY
Introduction Django
Wade Austin
 
PPT
2310 b 15
Krazy Koder
 
PPT
2310 b 15
Krazy Koder
 
PPTX
The Basics Angular JS
OrisysIndia
 
PDF
django_introduction20141030
Kevin Wu
 
PPTX
8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated
dioduong345
 
PPTX
Selenium.pptx
Pandiya Rajan
 
PPTX
DEVICE CHANNELS
Assaf Biton
 
PDF
Python Ireland Nov 2009 Talk - Appengine
Python Ireland
 
Design Patterns in Automation Framework.pdf
ArunVastrad4
 
بررسی چارچوب جنگو
railsbootcamp
 
GDG Addis - An Introduction to Django and App Engine
Yared Ayalew
 
Web development with django - Basics Presentation
Shrinath Shenoy
 
Googleappengineintro 110410190620-phpapp01
Tony Frame
 
A Basic Django Introduction
Ganga Ram
 
SharePoint Saturday Atlanta 2015
Pushkar Chivate
 
James Jara Portfolio 2014 Part 1
James Jara
 
Portafolio
James Jara
 
Gears User Guide
Muthuselvam RS
 
Django for mobile applications
Hassan Abid
 
Introduction Django
Wade Austin
 
2310 b 15
Krazy Koder
 
2310 b 15
Krazy Koder
 
The Basics Angular JS
OrisysIndia
 
django_introduction20141030
Kevin Wu
 
8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated
dioduong345
 
Selenium.pptx
Pandiya Rajan
 
DEVICE CHANNELS
Assaf Biton
 
Python Ireland Nov 2009 Talk - Appengine
Python Ireland
 
Ad

Recently uploaded (20)

PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Dipole Tech Innovations – Global IT Solutions for Business Growth
dipoletechi3
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Dipole Tech Innovations – Global IT Solutions for Business Growth
dipoletechi3
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Ad

The Role of Python in SPAs (Single-Page Applications)

  • 1. The Role of Python in SPAs (Single-Page Applications) David Gibbons Python Ireland Meetup July 2018
  • 2. Software engineer at OpenApp. Working with Python for over six years, previously with Cylon and Ammeon. Server and client-side Web development - Linux, Postgres, Python, JavaScript, CSS, HTML. Based on customer requirements, create bespoke Web applications. About Me 2 @davgibbs @davgibbs
  • 3. About OpenApp Founded in 2002, currently employ 40 staff. Web applications using Python for Healthcare industry. Main areas: ● Patient Registries ● Clinical Patient Management Systems - European Reference Network ● Geo Spatial Analytics ● Health Data Analytics Always interested in interesting CVs (developers, PMs, QA and test automation, etc). [email protected] 3
  • 4. About Talk Python on the Web Introduction to SPAs Python SPA Architecture “Movie Gallery” Application API Design for SPA Demo 4
  • 6. Python on the Web Simplicity and Scalability. Much built-in functionality and extension frameworks. Many Web companies including: 6 https://stackshare.io/
  • 7. Python Developer Survey October 2017 9,500 respondents in 150 countries. 50% said also use JavaScript and 49% also use HTML/CSS. The primary type of development done by Python developers: 7
  • 9. What is a SPA? 9 SPAs (Single-Page Applications) are Web applications that load a single HTML page and dynamically update that page as the user interacts with the application.
  • 10. SPA in a nutshell No Browser refreshes - Views are portions of the DOM (Document Object Model) that make up screen. Presentation logic in the client - Combining HTML template and data is done in the client. Server transactions - Only data is sent and received during transactions after initial load. 10
  • 11. 11
  • 12. 12
  • 13. SPA Advantages User experience - no full page reload means faster page load after the initial page load. Less ‘flicker’ during transitions. Decoupled presentation layer - allows client-side and server-side code to be maintained and tested separately. 13
  • 14. SPA Advantages and Disadvantages User experience - no full page reload means faster page load after the initial page load. Less ‘flicker’ during transitions Decoupled presentation layer - allows client-side and server-side code to be maintained and tested separately SEO - Web crawlers, used by search engines, have more difficulty analysing pages created dynamically. JavaScript dependency - needs to be enabled. 14
  • 15. Screen Views 15 Views are swapped in and out of HTML container div. For example “Home” and “About” content.
  • 16. Screen Regions 16 Divide the screen into regions with associated an associated view.
  • 17. Screen Regions - Gmail 17 Header region Content region Sidebar region
  • 18. AJAX - Core of SPAs Asynchronous JavaScript and XML It is a set of Web development techniques involving a means of unobtrusively requesting data from the server, combined with JavaScript’s power to update the HTML DOM. Makes use of XMLHttpRequest (XHR) API transfers data between web browser and server. Term Ajax first publicly used in 2005 based on techniques in Google pages.* 18 *http://adaptivepath.org/ideas/ajax-new-approach-web-applications/
  • 20. Set up a Python API which can be queried by client-side JavaScript code. Python Frameworks: handle database integration, session management and template generation. Help organise code. SPA Application Programming Interface (API) 20 Client Server API JSON AJAX
  • 21. ● RESTful API creation (Django REST framework) ● Sending Email ● CSV creation ● PDF creation (Reportlab) ● Asynchronous tasks (Celery) ● WebSockets (Channels) ● Complex computation (NumPy) Server-side tasks 21
  • 23. “Movie Gallery” Application Built to demonstrate SPA concepts. Movie information stored along with an image and rating. Can search and paginate through list of movies. Login to add, edit and delete movies. If not authenticated, then view-only movies. Python (Django) on the server-side and JavaScript (AngularJS) on client-side. 23
  • 24. 24
  • 25. 25 Screen Regions “Header” and “Footer” regions stay the same, while “Content” region is updated:
  • 26. API Design for SPA 26
  • 27. 27
  • 28. movies/models.py from django.core.validators import MaxValueValidator from django.db import models from django.contrib.auth.models import User class MovieGenre(models.Model): name = models.CharField(max_length=100, unique=True) class Movie(models.Model): title = models.CharField(max_length=100, unique=True) summary = models.TextField(max_length=400, blank=True, default='') release_year = models.PositiveIntegerField(default=2016) director = models.CharField(max_length=100, blank=True, default='') genre = models.ForeignKey(MovieGenre, related_name='movie_genre', default=1) image = models.ImageField(upload_to='movies/', default='movies/Movie.jpg') rating = models.PositiveIntegerField(validators=[MaxValueValidator(5)], default=3) created_date = models.DateTimeField(auto_now_add=True) user = models.ForeignKey(User, on_delete=models.CASCADE, default=1) 28
  • 29. spa_movies/urls.py from django.conf import settings from django.conf.urls import url, include, static from django.contrib import admin from django.views.generic import TemplateView from movies.urls import router urlpatterns = [ url(r'^$', TemplateView.as_view(template_name='movies/index.html')), url(r'^api/', include(router.urls)), url(r'^rest-auth/', include('rest_auth.urls')), url(r'^admin/', admin.site.urls) ] + static.static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 29 Define the root URLs for the application - example.com will direct to movies/index.html.
  • 30. RESTful API In order to create a testable and maintainable API, a RESTful API was setup for the JavaScript client to query. REST (REpresentational State Transfer) is an architecture style. URLs built around each resource (movies, etc). An architecture that uses a 4 main HTTP actions: GET, POST, PUT, DELETE. Django REST framework used - browsable API, good documentation. To handle the authentication, django-rest-auth library is used - default Session Authentication. 30
  • 31. URL HTTP Verb Result 1 /api/movies GET Returns all movies 2 /api/movies POST Create a new movie 3 /api/movies/:id GET Returns single movie 4 /api/movies/:id PUT Updates an existing movie 5 /api/movies/:id DELETE Deletes an existing movie 6 /api/movie-genres GET Returns all movie genres 7 /api/user-status/ GET Returns true if user logged-in 8 /rest-auth/login/ POST Empty or error message 9 /rest-auth/user/ GET Returns user info 10 /rest-auth/logout/ POST Logs user out 31
  • 32. movies/serializers.py from rest_framework import serializers from .models import MovieGenre, Movie class MovieGenreSerializer(serializers.ModelSerializer): class Meta: model = MovieGenre fields = ('id', 'name') class MovieSerializer(serializers.ModelSerializer): genre_obj = MovieGenreSerializer(source='genre', read_only=True) user_obj = UserSerializer(source='user', read_only=True) class Meta: model = Movie fields = ('id', 'title', 'summary', 'release_year', 'director', 'genre', 'genre_obj', 'image', 'rating', 'user', 'user_obj') 32 “ModelSerializer” will generate a set of fields and validators based on the model:
  • 33. movies/views.py “ModelViewSet” combines the logic for a set of related views in a single class for models from rest_framework import viewsets, permissions from rest_framework.decorators import permission_classes from .permissions import IsOwnerOrReadOnly, IsReadOnly from .serializers import MovieGenreSerializer, MovieSerializer from .models import Movie, MovieGenre class MovieGenreViewSet(viewsets.ModelViewSet): permission_classes = (IsReadOnly, ) queryset = MovieGenre.objects.all().order_by('name') serializer_class = MovieGenreSerializer class MovieViewSet(viewsets.ModelViewSet): permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly) queryset = Movie.objects.all().order_by('title') serializer_class = MovieSerializer 33
  • 34. movies/urls.py The “DefaultRouter” class is imported and used to create standard APIs. Get and CRUD (CReate, Update and Delete) URLs for movies and movies genre models set up: from rest_framework.routers import DefaultRouter from . import views router = DefaultRouter(trailing_slash=False) router.register(r'movies-genres', views.MovieGenreViewSet) router.register(r'movies', views.MovieViewSet) 34
  • 36. 36 Testing “Code without tests is broken by design” - Jacob Kaplan-Moss, one of original Django developers
  • 37. 37 Testing “Code without tests is broken by design” - Jacob Kaplan-Moss, one of original Django developers Automated testing allows easier code maintenance. Use the Django REST framework “APIClient” class to create client used to run tests. Run code coverage tool at same time to test Python code coverage: coverage3 run --source='./apps' --omit='*migrations*' apps/manage.py test movies
  • 38. movies/tests.py from django.test import TestCase from rest_framework.test import APIClient from movies.models import Movie class MovieTestCase(TestCase): def test_add_movie(self): self.assertEqual(len(Movie.objects.all()), 0) user = User.objects.create_user('admin', '[email protected]', 'password123') client = APIClient() client.login(username='admin', password='password123') response = client.post('/api/movies', {'title': 'Lion King', 'summary': 'Lion Movie', 'release_year': '1994', 'rating': 5, 'director': 'Roger Allers'}) self.assertEqual(response.status_code, 201) self.assertEqual(len(Movie.objects.all()), 1) 38
  • 39. Summary Python on the Web Introduction to SPAs, dividing screen into regions and views, use of AJAX. Python SPA architecture and server-side tasks. “Movie Gallery” application with Python API to demo SPA. Built a powerful and testable API with very few lines of code. SPA JavaScript code still needed. Complete code available at https://github.com/davgibbs/movies-spa 39
  • 40. References 1. SPA Design and Architecture. Understanding single-page web applications by Emmit A. Scott, Jr.: www.manning.com/books/spa-design-and-architecture 2. Sitepoint: Creating a CRUD App in Minutes with Angular’s $resource: ww.sitepoint.com/creating-crud-app-minutes-angulars-reso urce/ 3. Single-Page Applications: Build Modern, Responsive Web Apps with ASP.NET: https://msdn.microsoft.com/en-us/magazine/dn463786.aspx 4. Python Developers Survey 2017 Results: https://www.jetbrains.com/research/python-developers-sur vey-2017/ 40