• Aucun résultat trouvé

Web Service 
 and Open Data

N/A
N/A
Protected

Academic year: 2022

Partager "Web Service 
 and Open Data"

Copied!
5
0
0

Texte intégral

(1)

Web Service 
 and Open Data

By Lélia Blin - ProgRes lelia.blin@lip6.fr

Thanks to Quentin Bramas

Create an API with Python

Create an API

• Django: Powerful web framework with a lot of modules. Great to build a complete website.

• Flask: Small Framework to build simple website.

Bottle: Similar to Flask, but even simpler. Perfect to build an API

Available library/framework in python:

Create an API

The Bottle Framework (single file module, no dependencies)

• Routing: Requests to function-call mapping with support for clean and dynamic URLs.

• Templates: Fast and pythonic built-in template engine

• Utilities: Convenient access to form data, file uploads, cookies, headers and other HTTP-related metadata.

• Server: Built-in HTTP development server and

support for other WSGI capable HTTP server. (WSGI is the

Web Server Gateway Interface, which is a specification for web server in python)

(2)

Create an API

from bottle import route, run

@route('/hello') def hello():

return 'Hello world'

run(host='localhost', port=8080)

Hello world example:

>python3 07_Hello.py

Bottle v0.12.13 server starting up (using WSGIRefServer())...

Listening on http://localhost:8080/

Hit Ctrl-C to quit.

http://localhost:8080/hello

Id in URL

from bottle import route, run, template

@route('/hello/<name>') def hello(name):

return 'Hello ' + name

run(host='localhost', port=8080)

File: 08_HelloName.py

URL: http://localhost:8080/hello/Marie

Id in URL

Exemple: The user chose the langage: english, french, spanish

File: 09_HelloPL.py

(3)

Variables in URL

from bottle import Bottle, run, view, request app = Bottle()

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

return "Je mesure " + request.params.taille + " cm"

run(app, host='localhost', port=8080)#, reloader=True)

File: 11_Taille.py

URL: http://localhost:8080/jemesure?taille=133

Variables in URL

The response depends on the content of the variable

File: 10_Taille.py

URL: http://localhost:8080/jemesure?taille=133

Static content

#!/usr/bin/env python

# -*- coding: utf-8 -*-

from bottle import Bottle, run, static_file app = Bottle()

@app.route('/static/<filename:path>') def server_static(filename):

return static_file(filename, root='.')

run(app, host='localhost', port=8080, reloader=True)

File: 12_Img.py

URL: http://localhost:8080/static/cube.png

Template

Bottle comes with a fast, powerful and easy to learn built-in template engine called SimpleTemplate or stpl for short.

page.tpl

<!doctype html>

<!-- page.tpl -->

<HTML lang="fr">

<HEAD>

<TITLE>{{title}}</TITLE>

<meta charset="UTF-8">

</HEAD>

<body>

<h1>{{title}}</h1>

{{!body}}

<hr/>

<font size="-1"><i>Page réalisée avec Bottle</i></font>

</body>

</html>

La présence du ! Devant Body permet d'indiquer à bottle de ne pas échapper les caractères de balisage HTML dans la chaîne body.

Elle pourra donc contenir des balises.

(4)

Template

import bottle import datetime

@bottle.route("/time")

@bottle.view("page.tpl") def index() :

heure = datetime.datetime.now().strftime("<p>Nous sommes le %d/%m/

%Y, il est %H:%M:%S</p>")

return {"title":"Horloge", "body" : heure}

bottle.run(bottle.app(), host='localhost', port=8080, debug= True, reloader=True)

exo14

Template

@bottle.route("/qui")

@bottle.view("page.tpl") def qui() :

stri = """

<form method='post' action='bonjour'>

<input type='text' name='nom' placeholder='Votre nom ?'/>

<input type='submit' value='Bonjour bottle !'/>

</form>

"""

return {"title":"Présentez-vous", "body" : stri}

@bottle.route("/bonjour", method='POST')

@bottle.view("page.tpl") def bonjour() :

nom = bottle.request.forms.get('nom')

stri = "Bonjour mon(a) che(è)r(e) {}".format(nom) return {"title":"Bonjour", "body" : stri}

Accents

# -*- coding: utf-8 -*- import bottle import requests

from html.entities import codepoint2name def htmlCoding(stringToCode):

return ''.join( '&%s;' % codepoint2name[ord(oneChar)]

if ord(oneChar) in codepoint2name else oneChar for oneChar in stringToCode )

@bottle.route("/qui") def qui() : stri = """

<form method='post' action='bonjour'>

<input type='text' name='nom' placeholder='Votre nom ?'/>

<input type='submit' value='Validez !'/>

</form>

"""

return stri

@bottle.route("/bonjour", method='POST') def bonjour() :

nom = bottle.request.forms.nom return "Bonjour " + htmlCoding(nom)

bottle.run(bottle.app(), host='localhost', port=8080, debug= True, reloader=True)

Open Data

(5)

Mapping

https://leafletjs.com/

Et

Folium

Example of marker

import folium

coords = (48.846448, 2.357307)

map = folium.Map(location=coords, tiles='OpenStreetMap', zoom_start=16) folium.Marker(location=coords,popup='We are here').add_to(map)

map.save(outfile='map.html')

Exo15

Example of path

import folium coordinates = [ [48.846235, 2.354593], [48.846530, 2.354499], [48.847514, 2.353198], [48.849308, 2.356020], ]

m = folium.Map(location=[48.847402, 2.356878], zoom_start=17) folium.PolyLine(

locations=coordinates, color='#FF0000', weight=5 ).add_to(m)

m.save('page.html') Exo16

GeoJson

Is an open standard format designed for representing simple geographical features, along with their non-spatial attributes.

It is based on JSON, the JavaScript Object Notation.

import folium

coords = (48.7453229,2.5073644)

map = folium.Map(location=coords, tiles='OpenStreetMap', zoom_start=9)

#style function

sf = lambda x :{'fillColor':'#E88300', 'fillOpacity':0.5, 'color':'#E84000', 'weight':1, 'opacity':1}

folium.GeoJson(

data='communes-75-paris.geojson', name='Paris',

style_function= sf ).add_to(map)

map.save(outfile=‘map75.html')

Exo17

Références

Documents relatifs

• Server: Built-in HTTP development server and support for other WSGI capable HTTP server.. WSGI is the Web Server

• Let’s Encrypt : automated check (ACME protocol) and signature of an HTTPS certificate. •

JSP Integrating Java and a Web server (e.g., Apache Tomcat) node.js Chrome’s JavaScript engine (V8) plus a Web server Python Web frameworks: Django , CherryPy, Flask. Ruby

When the PRINT program is executed, either directly from the command line or from the server command menu, the option specifications from the Defaults.Text file

The adapters (External Source and Sink Connectors, External Processor) are running as wrappers for the processing modules, taking care of the interaction with Kafka

How do different types of fragment metadata affect the relation between interface cost and utility with regard to client-side query execution.. In this respect, we also formulate

In this paper we present a novel approach towards ephemeral Web personalization consisting in a client- side semantic user model built by aggregating RDF data encountered by the user

The OAI2LOD Server han- dles these shortcomings by republishing metadata originat- ing from an OAI-PMH endpoint according to the principles of Linked Data.. As the ongoing