+ All Categories
Home > Documents > Department of Computer and Information Science, Linköping...

Department of Computer and Information Science, Linköping...

Date post: 25-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
61
1 1
Transcript
Page 1: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

1 1

Page 2: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

Page 3: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

3

Page 4: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

••

••

4

variable = new XMLHttpRequest();

Page 5: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

• open() send()

• send() send(myText)

5

var xmlhttp=new XMLHttpRequest();

xmlhttp.open("GET", "course_info.txt", true);

xmlhttp.send();

xmlhttp.open("POST", "course_post.asp", true);

xmlhttp.send();

Page 6: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

• open() true

• xmlhttp.open("POST", "course_post.asp", true);•onreadystatechange

6

xmlhttp.onreadystatechange=function()  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200) {

    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

    }

  }

xmlhttp.open("GET", "course_info.txt", true);

xmlhttp.send();

Page 7: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

• readyState

7

readyState

Page 8: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

•• responseText responseXML

• responseText

• responseXML

8

document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

xmlDoc=xmlhttp.responseXML;txt="";

x=xmlDoc.getElementsByTagName(”COURSES");

for (i=0;i<x.length;i++)

{

txt=txt + “<div>” + x[i].childNodes[0].nodeValue + "</div>";

}

document.getElementById("myDiv").innerHTML=txt;

Page 9: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

• setRequestHeader•

• abort() •

9

xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

Page 10: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••••

10

Page 11: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

• route()

11

@app.route('/')def index():

return 'Index Page'

@app.route('/hello')

def hello():

return 'Hello World'

@app.route('/user/<username>')def show_user_profile(username):

# show the name of a user

return 'User %s' % username

Page 12: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

•• templates

• render_template()

12

from flask import render_template

@app.route('/hello/')

@app.route('/hello/<name>')

def hello(name=None):

return render_template('hello.html', name=name)

Page 13: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

13

<!doctype html><title>Hello from Flask</title>

{% if name %}

<h1>Hello {{ name }}!</h1>

{% else %}

<h1>Hello World!</h1>

{% endif %}

Page 14: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

•••

•••

••

14

Page 15: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

15

import sqlite3

from flask import g

def connect_db():

return sqlite3.connect("mydatabase.db")

def get_db():

db = getattr(g, 'db', None)

if db is None:

db = g.db = connect_db()

return db

def init():

c = get_db()

c.execute("drop table if exists entries")

c.execute("create table entries (id integer primary key, name text, message text)")

c.commit()

def add_message(name,message):

c = get_db()

c.execute("insert into entries (name,message) values (?,?)", (name,message))

c.commit()

def close():

get_db().close()

Page 16: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

••

16

HTTP

WebSockets

port 80

Page 17: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

••

••

17

Page 18: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

1.2.3.

18

Page 19: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

1.2.

19

Page 20: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

1.2.

20

Page 21: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

21

GET /mychat HTTP/1.1

Host: server.example.com

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==

Sec-WebSocket-Protocol: chat

Sec-WebSocket-Version: 12

Origin: http://example.com

HTTP/1.1 101 Switching Protocols

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=

Sec-WebSocket-Protocol: chat

Once the connection is established, the client and server can send WebSocket data or text frames back and forth in full-duplex mode.

Page 22: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

22

var connection = new WebSocket('ws://html5rocks.websocket.org/echo');

// When the connection is open, send some data to the server

connection.onopen = function () { connection.send('Ping'); // Send the message 'Ping' to the server};// Log errorsconnection.onerror = function (error) { console.log('WebSocket Error ' + error);};// Log messages from the serverconnection.onmessage = function (e) { console.log('Server: ' + e.data);};

Page 23: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

23

// Sending Stringconnection.send('your message');

Page 24: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

pip install Flask-Sockets

24

from flask import Flaskfrom flask_sockets import Sockets

app = Flask(__name__)

sockets = Sockets(app)

@sockets.route('/echo')

def echo_socket(ws):

while True:

message = ws.receive()

ws.send(message)

@app.route('/')

def hello():

return 'Hello World!’

Page 25: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

•••

25

Page 26: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

26

from geventwebsocket.handler import WebSocketHandlerfrom gevent.pywsgi import WSGIServerfrom flask import Flask, request, render_template app = Flask(__name__) @app.route('/')def index(): return render_template('index.html') @app.route('/api')def api(): if request.environ.get('wsgi.websocket’): ws = request.environ['wsgi.websocket'] while True: message = ws.receive() ws.send(message) return if __name__ == '__main__': http_server = WSGIServer(('',5000), app, handler_class=WebSocketHandler) http_server.serve_forever()

Page 27: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

27

@run_with_reloaderdef run_server():

app.debug = True

http_server = WSGIServer(('',5000), app, handler_class=WebSocketHandler)

http_server.serve_forever()

if __name__ == "__main__":

run_server()

Page 28: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

28

def signIn(email, password): c = get_db() res = c.execute("SELECT * FROM users WHERE email='"+email+"' AND password='"+password+"' LIMIT 1") res = res.fetchone() if not res: # Not logged in return json.dumps({"success": False, "message": "Invalid email or password"}) else: # Logged in return json.dumps({"success": True, "message": "You are now signed in", "data": token}) return None

@app.route("/signin", methods=["POST"])

def sign_in(): return signIn(request.json["email"], request.json["password"])

Page 29: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

29

> python -VPython 2.7.6

> virtualenv test

New python executable in test/bin/python

Installing setuptools….........done.

Installing pip...............done.

> cd test

> bin/pip install flask

Downloading/unpacking flask...

> pip install gevent

> pip install gevent-websocket

Page 31: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••••

••••

31

Page 32: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••••••

••

32

Page 33: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

JSON is Like XML Because● Both JSON and XML are "self describing" (human readable)● Both JSON and XML are hierarchical (values within values)● Both JSON and XML can be parsed and used by lots of programming languages● Both JSON and XML can be fetched with an XMLHttpRequest

JSON is Unlike XML Because● JSON doesn't use end tag● JSON is shorter● JSON is quicker to read and write● JSON can use arrays

33

Page 34: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

34

Page 35: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

35

{

"code": "TDDD97",

”title": ”Web programming",

"credits": 6

}

{

"courses": [

{"code":"TDDD24" , "credits":4 },

{"code":"TDDD97" , "credits":6 }

]

}

Page 36: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

36

{ "firstName": "John",

"lastName": "Doe",

"age": 22,

"address": {

"streetAddress": "Drottinggatan 1",

"city": ”Linköping”,

"postalCode": ”58183"

},

"phoneNumber": [

{ "type": "home", "number": "013-123456" },

{ "type": "mobile", "number": "070-123456" }

],

"newSubscription": false,

"companyName": null

}

Page 37: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

37

>>> import json>>> data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 } ]

>>> print 'DATA:', repr(data)

DATA: [{'a': 'A', 'c': 3.0, 'b': (2, 4)}]

>>>

>>> data_string = json.dumps(data) #jsonify(data)

>>> print 'JSON:', data_string

JSON: [{"a": "A", "c": 3.0, "b": [2, 4]}]

>>>

Page 38: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

•• import json

•• json.dump

•• json.dumps

•• json.load

•• json.loads

38

Page 39: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••••

39

Page 40: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

•••

40

Page 41: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

41

var xml = new XMLHttpRequest();

xml.onreadystatechange = function() {

  if (xml.readyState==4 && xml.status==200) {

var serverResponse = JSON.parse(xml.responseText);

...

   }

};

xml.open("GET", "test", true);

xml.send(null);

Page 42: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

42

Page 43: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

••

•••••••••••••

43

Page 44: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

44

Page 45: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••••

••

45

Page 46: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

••

46

Page 47: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

47

Alternative 1

Providing Live Data Presentation

Use case:Stock market apps, Analytics and in general where the data set is produced by a third party and it needs to be observed visually instantaneously.

Example:http://www.jscharts.com/exampleshttps://www.dailyfx.com/usd-sek

Page 48: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

48

Alternative 2

Use of HTML5 for Drag and Drop

Use case:Almost can be used in any Graphical User Interface.

Example:https://html5demos.com/drag/#

Page 49: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

49

Alternative 3

Performing Client side Routing + Overriding Back/Forward buttons using the History API

Use case:The most common usage is in Single Page Applications where the application is composed of one web-page but multiple views.

Example:http://www.camedin.com

Page 50: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

50

Alternative 4

Third -Party Authentication Using OpenID/OAuth 2.0

Advantages:Decreasing risk, cost and complexity.

Example:https://www.camedin.com

Page 51: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

51

Alternative 5

Applying Further Security Measures

Use case:Banking apps and generally where security is a high priority.

Example:Token hashing and salting with other data in the request.

Page 52: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

52

Alternative 6

Testing Using Selenium

Advantages:http://alvinalexander.com/testing/automated-gui-testing-benefits-toolshttps://www.youtube.com/watch?v=juKRybHPMwE

Page 53: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

53

Alternative 7

Client -side Templating Using a Third Party API

Advantages:Code reusability by defining a view structure for showing changing information. Mostly used in SPAs while being combined with asynchronous calls.

Example:www.camedin.com

Page 54: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

54

Alternative 8

Media Streaming

Advantages:No need to download the whole media completely before being able to view it.

Example:www.youtube.comwww.facebook.com

Page 55: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

55

Alternative 9

Styling and Responsive Design

Advantages:One GUI which adapts itself to different screen resolutions instead of having different GUI layouts for different screen resolutions.

Example:www.bbc.co.uk

Page 56: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

56

Alternative 7

Client -side Templating Using a Third Party API

Advantages:Code reusability by defining a view structure for showing changing information. Mostly used in SPAs while being combined with asynchronous calls.

Example:www.camedin.com

Page 57: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

57

Alternative 8

Media Streaming

Advantages:No need to download the whole media completely before being able to view it.

Example:www.youtube.comwww.facebook.com

Page 58: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

58

Alternative 9

Styling and Responsive Design

Advantages:One GUI which adapts itself to different screen resolutions instead of having different GUI layouts for different screen resolutions.

Example:www.bbc.co.uk

Page 59: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

59

Alternative 10

Deploy Your Solution on Heroku

Advantages:Your web application shall be available to the world. At the same time, you can use a broad range of different tools and libraries available on Heroku.

Example:https://www.heroku.com/partners-app-showcase

Page 60: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

60

Alternative 11

Geolocation

Advantages:Users don’t need to enter their location manually which shall improve user experience.

Example:www.foodora.se

Page 61: Department of Computer and Information Science, Linköping ...TDDD97/lectures/2020/Lecture04_TDDD97.pdf · One GUI which adapts itself to different screen resolutions instead of having

61

Alternative 12

Recover your password

Advantages:It’s a must! It’s very common that users forget their passwords and need to recover them.

Example:https://www.facebook.com/login/identify/?ctx=recover&ars=royal_blue_bar


Recommended