csci 2133 rapid programming techniques for innovation
play

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


  1. CSCI 2133 – Rapid Programming Techniques for Innovation A Python based framework Django Crash Lesson CSCI 2133

  2. 2 Previous Lecture • Python Data Manipulation CSCI 2133

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

  4. Why Django • Cacheinfrastructure • ORM • Internationalization • Automatic admin interface • Command-line job framework • Regex-based URLdesign • T emplating system 4 CSCI 2133

  5. Lets get started django-admin startproject myshop 5 CSCI 2133

  6. It starts to work immediately • python manage.py runserver 6 CSCI 2133

  7. Wher ere e those v e views come f e from • First of all, download a suitable IDE to view files under project: 7 CSCI 2133

  8. Add a home page python m manage.py star artap app home me 8 CSCI 2133

  9. 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 from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('home.urls', namespace='home')), ] 9 CSCI 2133

  10. Fill in views.py and urls.py under “home” app Views.py is the web UI interface to help Django find template. 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) 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. from django.conf.urls import url from . import views app_name = 'home' urlpatterns = [ url(r'^$', views.base, name='base'), ] 10 CSCI 2133

  11. Final working home structure 11 CSCI 2133

  12. 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 CSCI 2133

  13. Agenda • Django REST connection • Twitter API demo • Django templates language 13 CSCI 2133

  14. Django consume REST service • 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. 14 CSCI 2133

  15. 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 CSCI 2133

  16. 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 CSCI 2133

  17. Combine with Python • The easiest way to execute curl in python like this: import os os.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 CSCI 2133

  18. 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 CSCI 2133

  19. • 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 CSCI 2133

  20. • 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 CSCI 2133

  21. A sample code of posting Rest 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()) 21 CSCI 2133

  22. 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 CSCI 2133

  23. • 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 CSCI 2133

  24. 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 CSCI 2133

  25. Get first 2 tweets by using access_token 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') 25 CSCI 2133

  26. Exercise • Playback generation of last 2 tweets by yourself. 26 CSCI 2133

  27. Django templates • Views.py • Http request/response modules • Integrate with template file • Templates languages: • Variables • Condition • Loop 27 CSCI 2133

  28. 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 CSCI 2133

  29. 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 CSCI 2133

  30. 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 CSCI 2133

  31. 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 CSCI 2133

Recommend


More recommend