Top Banner
Episode X: The Python Way Of The Force May the web be with you…
40

HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Mar 20, 2017

Download

Education

Anton Tibblin
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Episode X: The Python Way Of The ForceMay the web be with you…

Page 2: HT15, DA354A - Introduktion till Webbprogrammering - Bottle
Page 3: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Dagens agenda• Demo på vad vi ska bygga• Bygga webbsidor med Python• Webbserver med Python• Server => Python• Klient => HTML/CSS• Bottle som mikroramverk• Vad är ett mikroramverk?• Routes => Funktioner• Bygga vår webbplats

Page 4: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Vad vi ska byggahttp://tibbelit.pythonanywhere.com/

Page 5: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Webbserver?

Page 6: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

En webbserver är antingen ett datorprogram som

tillhandahåller webbsidor för en viss webbplats eller en serverdator på

vilken sådan programvara körs.

I vårt fall ett datorprogram – skrivet i Python

Page 7: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Webbserver• Ett webbserverprogram har som uppgift att

tillhandahålla webbsidor och andra filer via datakommunikationsprotokollet HTTP eller HTTPS, vanligen över Internet.

• Det förekommer också att en enskild användare kör en webbserver för eget lokalt bruk på sin egen dator. Vanligen kommunicerar användaren med webbservern med hjälp av en webbläsare.

• Användaren väljer webbsidor och webbläsaren beställer webbsidorna från webbservern och visar dem på användarens datorskärm. Webbläsaren är klient till webbservern.

Page 8: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Internet

Förfrågan

Svar

Klient

Klient

Server

Page 9: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

No place like 127.0.0.1, there is

Page 10: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

python -m SimpleHTTPServer 8000Att starta en webbserver från en specifik mapp genom Python (från konsolen/terminalen)

Page 11: HT15, DA354A - Introduktion till Webbprogrammering - Bottle
Page 12: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Demo – SimpleHTTPServer

Page 13: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Men – hur mycket Python var detta egentligen?

Feel the power of the server side!

Page 14: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Behöver vi bygga allt själva?

A simpler way, there is

Page 15: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Mikroramverk• En samling funktioner för att bygga webbplatser, med fokus på• Snabbt• Smidigt• Resurssnålt• Exempel på mikroramverk• Bottle (Python)• Flask (Python)• Slim (PHP)• Silex (PHP)• Camping (Ruby)• Sinatra (Ruby)

Page 16: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Bottle – Ett mikroramverk i Python• Bottle har många inbyggda funktioner, men fokuserar på:• Routing Mappa URL:r mot funktioner• Templates Skapa mallar (HTML) för att presentera innehåll• Utilities Tillgång till data, ladda upp filer, cookies, HTTP-möjligheter,

m.m.• Server Inklulderar en inbyggd HTTP-server

• Bottle är endast en enda fil• Bottle använder bara standard-biblioteket för Python

• Man installera bottle genom PIP, easy_install, eller laddar hem py-filen.

Page 17: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Bottle - Routes• Vi vill kunna mappa URL till olika funktioner i Python• /home => def home():• /contact => def contact():• /about => def about():

127.0.0.1/homeStarwarsvote.com/homeThedarkside.com/home

Page 18: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Kom igång med bottle!

Page 19: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Hello <name>

Page 20: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Att använda sig utav flera routes…

Page 21: HT15, DA354A - Introduktion till Webbprogrammering - Bottle
Anton Tibblin
Page 22: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Demo – olika routes

Anton Tibblin
Page 23: HT15, DA354A - Introduktion till Webbprogrammering - Bottle
Page 24: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Templates -Mallar för vår information

Join the dark side, we have the prettiest web sites!

Page 25: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Bottle - Templates• Templates är mallar för hur vi ska presentera vår information• HTML• CSS• JavaScript

• Vi kan skicka data (bearbetad utav Python) till våra malla för att skapa dynamiska webbsidor

Page 26: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Bottle – Templates och statiska filer• Alla templates ska:• Ligga i mappen ”view”• Ha filendelsen *.tpl

• Statiska filer (bilder, css, js, etc.) ska:• Ligga i mappen ”static”

Page 27: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Hur ser detta ut i bottle?

Page 28: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Demo - templates

Page 29: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Nu till vårt projekt

Page 30: HT15, DA354A - Introduktion till Webbprogrammering - Bottle
Page 31: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Routes => Vilka behöver vi?Route Syfte MetodTemplate/ Till vår startsida GET index /vote Rösta på en sida POST index /disqus Visa gästbok GET disqus /disqus-post Skriv ett inlägg POST disqus

*error404* En sida som inte finns Alla error

Page 32: HT15, DA354A - Introduktion till Webbprogrammering - Bottle
Page 33: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Testa så att allt fungerar

Let the battle begin

Page 34: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

1. Fixa röstningen2. Fixa gästboken

Page 35: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Röstningen• Spara alla röster i filen ”votes.txt”• Spara rösterna som JSON-format, enligt följande mall:

{"empire": 0,"rebels": 0

}• Vi kan omvandla ett lexikon i Python till JSON genom:• json_votes = json.dumps(votes)• Vi kan omvandla JSON till Python-datatyper genom:• Votes = json.loads(json_votes)

Page 36: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Flödesschema

Läs in textfilens innehåll

Tolka JSON-data till ett

lexikon

Skriv ut

imperiets

röster

Skriv ut Rebellers röster

Page 37: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Flödesschema

Läs in textfilens innehåll

Tolka JSON-data till ett

lexikon

Skriv ut imperiets

röster

Skriv ut Rebellers

röster

Skicka röster till template

Webbsida på internet

Page 38: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Nu bygger vi! ;)

Page 39: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

2. Fixa gästboken

Page 40: HT15, DA354A - Introduktion till Webbprogrammering - Bottle

Frågor?