CSCI 2133 Rapid Programming Techniques for Innovation A Python - - PowerPoint PPT Presentation

csci 2133 rapid programming techniques for innovation
SMART_READER_LITE
LIVE PREVIEW

CSCI 2133 Rapid Programming Techniques for Innovation A Python - - PowerPoint PPT Presentation

CSCI 2133 Rapid Programming Techniques for Innovation A Python based framework Django Crash Lesson CSCI 2133 2 Previous Lecture Python Data Manipulation CSCI 2133 Agenda Django Overview Learn Django by examples


slide-1
SLIDE 1

CSCI 2133

CSCI 2133 – Rapid Programming Techniques for Innovation

A Python based framework Django Crash Lesson

slide-2
SLIDE 2

CSCI 2133

Previous Lecture

  • Python Data Manipulation

2

slide-3
SLIDE 3

CSCI 2133

Agenda

  • Django Overview
  • Learn Django by examples
  • http://docs.python.org
  • https://docs.djangoproject.com

3

slide-4
SLIDE 4

CSCI 2133

Why Django

4

  • ORM
  • Automatic admin

interface

  • Regex-based URLdesign
  • T

emplating system

  • Cacheinfrastructure
  • Internationalization
  • Command-line job

framework

slide-5
SLIDE 5

CSCI 2133

Lets get started

5

django-admin startproject myshop

slide-6
SLIDE 6

CSCI 2133

It starts to work immediately

  • python manage.py runserver

6

slide-7
SLIDE 7

CSCI 2133

Wher ere e those v e views come f e from

  • First of all, download a suitable IDE to view files

under project:

7

slide-8
SLIDE 8

CSCI 2133

Add a home page

python m manage.py star artap app home me

8

slide-9
SLIDE 9

CSCI 2133

Create a template

  • M a k e a t e m p l a t e s d i r e c t o r y u n d e r h o m e :

m k d i r t e m p l a t e s

  • P u t a n H T M L fi l e i n t h e t e m p l a t e s d i r e c t o r y

W e b u i l t a v e r y s i m p l e h t m l p a g e c a l l e d “ h o m e . h t m l ”

  • A d d a “ u r l s . p y ” u n d e r h o m e , a n d i n c l u d e i t i n y o u r r o o t

u r l s . p y

9

from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('home.urls', namespace='home')), ]

slide-10
SLIDE 10

CSCI 2133

Fill in views.py and urls.py under “home” app

10

from django.http import HttpResponse from django.template.loader import render_to_string def base(request): rendered = render_to_string('base.html') return HttpResponse(rendered)

Views.py is the web UI interface to help Django find template.

from django.conf.urls import url from . import views app_name = 'home' urlpatterns = [ url(r'^$', views.base, name='base'), ]

Urls.py is the configuarion file to route the http request to corresponding temaple (here we refer to the base function in views.py). And base function points at the template we specified.

slide-11
SLIDE 11

CSCI 2133

Final working home structure

11

slide-12
SLIDE 12

CSCI 2133

Lab and Exercises

  • Create a template for the homepage
  • Create a view that will respond with the rendered

template

  • Connect the / URL to the view
  • You could refer to

https://git.cs.dal.ca/gang/django-tutorials as example.

12

slide-13
SLIDE 13

CSCI 2133

Agenda

  • Django REST connection
  • Twitter API demo
  • Django templates language

13

slide-14
SLIDE 14

CSCI 2133

Django consume REST service

14

  • Using cURL

cURL (pronounced 'curl') is a computer software project providing a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997. The name stands for "Client URL".The original author and lead developer is the Swedish developer Daniel Stenberg.

  • ------------------- from wiki
  • curl is a command-line tool for transferring data

and supports about 22 protocols including

  • HTTP. This combination makes it a very good ad-

hoc tool for testing our REST services.

slide-15
SLIDE 15

CSCI 2133

Com Command-line O Options

  • Verbose:

curl -v https://web.cs.dal.ca/~gang/csci2133-project/step-db/login- all.php

  • Output

curl -o out.json https://web.cs.dal.ca/~gang/csci2133-project/step- db/login-all.php

  • HTTP Methods with curl

Get Same as output

curl -o https://web.cs.dal.ca/~gang/csci2133-project/step-db/login-rest- service.php? username=test1&password=12345

Post

curl -d 'username=test1&password=12345' https://web.cs.dal.ca/~gang/csci2133-project/step-db/login-rest- service.php [{"cus_num":11,"username":"test1","email":"test1@company.com"}]

15

slide-16
SLIDE 16

CSCI 2133

Continue…

  • PUT

curl -d @request.json -H 'Content-Type: application/json’ -X PUT http://localhost:8082/spring-rest/foos/9

  • DELETE

curl -X DELETE http://localhost:8082/spring-rest/foos/9

  • Authentication

If 401 – Unauthorized HTTP response code and an associated WWW-Authenticate header: curl --user baeldung:secretPassword http://example.com/

16

slide-17
SLIDE 17

CSCI 2133

Combine with Python

  • The easiest way to execute curl in python like this:

import os

  • s.system("curl -v.........")

But the result depends on work environment. Somehow exception happens, such as “curl command not found” Next.... Let us find elegant way.

17

slide-18
SLIDE 18

CSCI 2133

Using module “Request”

  • GET

From

curl -v https://web.cs.dal.ca/~gang/csci2133-project/step- db/login-all.php

To

import requests response = requests.get('https://web.cs.dal.ca/~gang/csci2133- project/step-db/login-all.php')

18

slide-19
SLIDE 19

CSCI 2133

  • POST

From

curl -d 'username=test1&password=12345' https://web.cs.dal.ca/~gang/csci2133-project/step-db/login-rest- service.php

To

import requests data = { 'username': 'test1', 'password': '12345' } response = requests.post('https://web.cs.dal.ca/~gang/csci2133- project/step-db/login-rest-service.php', data=data)

19

slide-20
SLIDE 20

CSCI 2133

  • Delete

From

curl -X DELETE http://localhost:8082/spring-rest/foos/9

To

import requests response = requests.delete('http://localhost:8082/spring- rest/foos/9')

20

slide-21
SLIDE 21

CSCI 2133

A sample code of posting Rest

21

import requests data = { 'username': 'test1', 'password': '12345' } response = requests.post('https://web.cs.dal.ca/~gang/csc i2133-project/step-db/login-rest-service.php', data=data) print(response.json())

slide-22
SLIDE 22

CSCI 2133

Twitter Connection

  • Get client Key and Secret from Twitter

Generate Access Token to Authenticate ( aka Login) to Twitter API Once this is done you will need to get:

  • Consumer Key (API Key)
  • Consumer Secret (API Secret)
  • Access Token
  • Access Token Secret
  • Oauth2 authentication

import base64 import requests client_key = ‘Y**********9' client_secret = 'Bsqx*********ExI' key_secret = '{}:{}'.format(client_key, client_secret).encode('ascii') b64_encoded_key = base64.b64encode(key_secret) b64_encoded_key = b64_encoded_key.decode('ascii')

22

slide-23
SLIDE 23

CSCI 2133

  • The next step is to make a post request to the authentication

endpoint to obtain a Bearer Token to be included in subsequent API requests.

base_url = 'https://api.twitter.com/' auth_url = '{}oauth2/token'.format(base_url) auth_headers = { 'Authorization': 'Basic {}'.format(b64_encoded_key), 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } auth_data = { 'grant_type': 'client_credentials' } auth_resp = requests.post(auth_url, headers=auth_headers, data=auth_data, verify=False) print(auth_resp.json()) access_token = auth_resp.json()['access_token']

23

slide-24
SLIDE 24

CSCI 2133

Making Q Quer eries es t through t twi witter er

  • The reference documentation for the API can be

found at: https://dev.twitter.com/rest/reference

  • We will be making a search request for the latest 2

tweets with the terms ‘General Election’.

  • The options for search parameters can be found at:

https://dev.twitter.com/rest/reference/get/search/t weets

24

slide-25
SLIDE 25

CSCI 2133

Get first 2 tweets by using access_token

25 search_headers = { 'Authorization': 'Bearer {}'.format(access_token) } search_params = { 'q': 'General Election', 'result_type': 'recent', 'count': 2 } search_url = '{}1.1/search/tweets.json'.format(base_url) search_resp = requests.get(search_url, headers=search_headers, params=search_params, verify=False) tweet_data = search_resp.json() for x in tweet_data['statuses']: print(x['text'] + '\n')

slide-26
SLIDE 26

CSCI 2133

Exercise

  • Playback generation of last 2 tweets by yourself.

26

slide-27
SLIDE 27

CSCI 2133

Django templates

  • Views.py
  • Http request/response modules
  • Integrate with template file
  • Templates languages:
  • Variables
  • Condition
  • Loop

27

slide-28
SLIDE 28

CSCI 2133

Combine with Django

import requests from django.http import HttpResponse def base(request):

data = {

'username’: request.GET[‘username’], 'password’: request.GET[‘password’]

} response = requests.post('https://web.cs.dal.ca/~gang/csci2133-project/step-db/login-rest-service.php', data=data) django_response = HttpResponse( content=response.content, status=response.status_code, content_type=response.headers['Content-Type'] )

return django_response

28

slide-29
SLIDE 29

CSCI 2133

Views.py matters http service call

  • In Django, views have to be created in the app

views.py file:

from django.http import HttpResponse def hello(request): text = """<h1>welcome to my app !</h1>""" return HttpResponse(text)

29

slide-30
SLIDE 30

CSCI 2133

HttpRequest and HttpResponse

  • Views.py always takes HttpRequest as first

parameter.

  • Several important HttpRequest members:
  • HttpRequest.scheme
  • HttpRequest.GET / HttpRequest.POST

Example: request.GET[‘key’] / request.POST[‘key’]

  • HttpRequest.method

Example: if request.method == ‘GET’

30

slide-31
SLIDE 31

CSCI 2133

Continue..

  • HttpResponse objects
  • In contrast to HttpRequest objects, which are created

automatically by Django, HttpResponse objects are your responsibility. Usage:

from django.http import HttpResponse >>> response = HttpResponse("Here's the text of the Web page.") >>> response = HttpResponse("Text only, please.", content_type="text/plain")

31

slide-32
SLIDE 32

CSCI 2133

Now talk with your template

  • Instead of hard code response text in views.py

from django.shortcuts import render def base(request): return render(request, “base.html", {})

  • Combine with what we learn from Python REST

service call.

32

slide-33
SLIDE 33

CSCI 2133

Continue…

33 import requests from django.http import HttpResponse from django.shortcuts import render def base(request): data = { 'username': request.GET.get('username'), 'password': request.GET.get('password') } response = requests.post('https://web.cs.dal.ca/~gang/csci2133-project/step- db/login-rest-service.php', data=data) respData = response.json() print(respData) html = render(request, 'base.html', {"theUser" : respData, "title": "track a user", "h2": "Search Result"}) return HttpResponse(html)

slide-34
SLIDE 34

CSCI 2133

Template

  • Your template should be always located in

templates folder under your app.

  • Catch the variable from views.py

34

slide-35
SLIDE 35

CSCI 2133

Template languages

  • Filters
  • They help you modify variables at display time. Filters structure

looks like the following: {{var|filters}}.

  • Some examples −
  • {{string|truncatewords:80}}
  • {{string|lower}}
  • Tags
  • Tags lets you perform the following operations: if condition, for

loop, template inheritance and more.

35

slide-36
SLIDE 36

CSCI 2133

Tags

  • IF

Example:

{% if theUser|length < 1 %} <h5>No Result!</h5> {% elif %} ….. {% endif %}

  • FOR

<table> {% for user in theUser %} <tr> {% for key,value in user.items %} <td> {{value}} </td> {% endfor %} </tr> {% endfor %} </table>

36

slide-37
SLIDE 37

CSCI 2133

Playback

  • Templates file: https://git.cs.dal.ca/gang/django-

tutorials/blob/master/home/templates/base.html

  • Views.py: https://git.cs.dal.ca/gang/django-

tutorials/blob/master/home/views.py

  • Create an Django app on your local, access service

https://web.cs.dal.ca/~gang/csci2133-project/step- db/login-rest-service.php by using GET method to send username and password. Display result on web.

37

slide-38
SLIDE 38

CSCI 2133

Models

  • Database setup
  • Create Models in App
  • Manipulate Data through Models

38

slide-39
SLIDE 39

CSCI 2133

How models benefit your dev

  • An ORM allows for simple manipulation of data

without having to write complex SQL

39

Pictures credit to https://djangobook.com/mdj2-models/

slide-40
SLIDE 40

CSCI 2133

So benefits of modals are:

  • Simplicity.
  • Consistency.
  • Avoids Introspection.
  • Version Control.
  • Advanced Metadata.

40

slide-41
SLIDE 41

CSCI 2133

How modals work in Django

41

Pictures credit to https://djangobook.com/mdj2-models/

slide-42
SLIDE 42

CSCI 2133

Moreover…

42

Pictures credit to https://djangobook.com/mdj2-models/

slide-43
SLIDE 43

CSCI 2133

Lets get our feet wet

  • Follow simple steps to have your own models:
  • Configure database engine in settings.py
  • Create a py class in a file called models.py in your app
  • Update underlying DB by “migration” command
  • That is it…

43

slide-44
SLIDE 44

CSCI 2133

Configure database engine in settings.py

  • If you are not using SQLite as your database, additional settings

such as USER, PASSWORD, HOST must be added. For more details, see the reference documentation for DATABASES.

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'DB_NAME', 'USER': 'DB_USER', 'PASSWORD': 'DB_PASSWORD', 'HOST': 'localhost', # Or an IP Address that your DB is hosted on 'PORT': '3306', } }

You might want to install DB module if your python doesn’t have:

pip3 install mysqlclient If you fail command above, try old version: pip install mysqlclient==1.3.12

44

slide-45
SLIDE 45

CSCI 2133

Now create a class in models.py

  • Supposedly I want to create a teacher table ( you

don’t have to directly miniplate database), instead you create model class in Django:

45

from django.db import models # Create your models here. class TeacherB0012345(models.Model): name = models.CharField(max_length=100) address = models.CharField(max_length=100, blank=True) city = models.CharField(max_length=50) province = models.CharField(max_length=2) email = models.EmailField(max_length=100, blank=True) class Meta: verbose_name_plural = 'Teacher' class CourseB0012345(models.Model): teacher = models.ForeignKey(TeacherB0012345, on_delete=models.CASCADE) name = models.CharField(max_length=42) code = models.CharField(max_length=4) website = models.URLField(max_length=200, null=True, blank=True) content = models.TextField() last_update = models.DateTimeField(auto_now_add=True)

slide-46
SLIDE 46

CSCI 2133

Now run migrate command

  • First, we must package up our model changes into

individual migration files using the command

  • makemigrations. :

python manage.py makemigrations

46

slide-47
SLIDE 47

CSCI 2133

Finally you can commit your changes to DB

47

slide-48
SLIDE 48

CSCI 2133

Now double check changes in DB

  • https://www.db4free.net/phpMyAdmin

user="csci2133_gang", passwd="12345678",

  • Note: the generated table name by Django will be

formed by “app name” + “_” + “class name in models”

48

slide-49
SLIDE 49

CSCI 2133

Exercise in Class

Giving the database connection:

host="db4free.net", user="csci2133_gang", passwd="12345678", database="csci2133_login“

Please create a class (name is student_[your banner id]) in your models.py, and this class will be mapping to a table structure like following:

Id int primary key Name varchar 50 Banner varchar 50 Homeaddress varchar 250 Emailaddress varchar 250

49

slide-50
SLIDE 50

CSCI 2133