+ All Categories
Home > Documents > Python for Practice - cuni.cz

Python for Practice - cuni.cz

Date post: 04-Jan-2022
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
31
1 Python for Practice Web applications
Transcript
Page 1: Python for Practice - cuni.cz

1

Python for Practice

Web applications

Page 2: Python for Practice - cuni.cz

2

Overview

WSGI

Web Server Gateway Interface

a standard interface between web servers and Python web application frameworks

WSGI makes it possible to write portable Python web code that can be deployed in any WSGI-compliant web server

Python web-frameworks

Djangoa “batteries included” web application framework

for complex, database-backed web applications quickly

Flaska “microframework” for Python

for building smaller applications, APIs, and web services

Page 3: Python for Practice - cuni.cz

3

Web applications (common structure)

URL Routing

incoming request to a particular piece of code

Request and Response Objects

Information from a user browser to object(s)

Object(s) to user browser

Template Engine

separating logic (code) from presentation

Web Server

Page 4: Python for Practice - cuni.cz

4

Template engines

Page 5: Python for Practice - cuni.cz

5

Overview

Separating logic from presentation“user interface” can be developed separately

the same logic for different views

no need for “escaping” in code

….

General overviewtemplate + data => final document

Template engines in PythonJinja2

Mako

Chameleon

Hello,

$name

name =

Petr

Hello,

Petr

Template engine

Page 6: Python for Practice - cuni.cz

6

Jinja2

https://palletsprojects.com/p/jinja/

General usage

from jinja2 import Template

template = Template('Hello {{ name }}!')

template.render(name='John Doe')

See

e01_jinja.py

Page 7: Python for Practice - cuni.cz

7

Jinja2

Environmentconfiguration for the engine

if not created manually, it creates automatically

methodsget_template(name)

list_templates()

env = Environment(

loader=PackageLoader('yourapplication',

'templates'),

autoescape=select_autoescape(['html',

'xml'])

)

Page 8: Python for Practice - cuni.cz

8

Jinja2

Loaders

FileSystemLoader(searchpath)

PackageLoader(package_name, package_path)

DictLoader(mapping)

ChoiceLoader(loaders)

BaseLoader

Page 9: Python for Practice - cuni.cz

9

Jinja2

Template

render(context)

generate(context)

render_async(context)

Page 10: Python for Practice - cuni.cz

10

Jinja2

Template language

<!DOCTYPE html>

<html lang="en">

<head>

<title>My Webpage</title>

</head>

<body>

<ul id="navigation">

{% for item in navigation %}

<li><a href="{{ item.href }}">

{{ item.caption }}</a></li>

{% endfor %}

</ul>

<h1>My Webpage</h1>

{{ a_variable }}

{# a comment #}

</body>

</html>

Page 11: Python for Practice - cuni.cz

11

Jinja2

Template language

{% ... %} statements

{{ ... }} expressions

{# ... #} comments

# ... line statements

Page 12: Python for Practice - cuni.cz

12

Jinja2

Template inheritance

<!DOCTYPE html>

<html lang="en">

<head>

{% block head %}

<link rel="stylesheet" href="style.css" />

<title>{% block title %}{% endblock %} –

My Webpage</title>

{% endblock %}

</head>

<body>

<div id="content">{% block content %}{% endblock %}</div>

<div id="footer">

{% block footer %}

&copy; Copyright 2008 by <a

href="http://domain.invalid/">you</a>.

{% endblock %}

</div>

</body>

</html>

Page 13: Python for Practice - cuni.cz

13

Jinja2

Template inheritance

{% extends "base.html" %}

{% block title %}Index{% endblock %}

{% block head %}

{{ super() }}

<style type="text/css">

.important { color: #336699; }

</style>

{% endblock %}

{% block content %}

<h1>Index</h1>

<p class="important">

Welcome to my awesome homepage.

</p>

{% endblock %}

Page 14: Python for Practice - cuni.cz

14

Jinja2

Template language statements{% for row in rows %}

<li class="{{ loop.cycle('odd', 'even') }}">

{{ row }}</li>

{% endfor %}

{% if kenny.sick %}

Kenny is sick.

{% elif kenny.dead %}

You killed Kenny! You bastard!!!

{% else %}

Kenny looks okay --- so far

{% endif %}

{% macro input(name, value='', type='text', size=20) -%}

<input type="{{ type }}" name="{{ name }}" value="{{

value|e }}" size="{{ size }}">

{%- endmacro %}

<p>{{ input('username') }}</p>

Page 15: Python for Practice - cuni.cz

15

Flask

Page 16: Python for Practice - cuni.cz

16

Overview

https://palletsprojects.com/p/flask/

from flask import Flask, escape, request

app = Flask(__name__)

@app.route('/')

def hello():

name = request.args.get("name", "World")

return f'Hello, {escape(name)}!'

$ env FLASK_APP=e02_hello.py flask run

See

e02_hello.py

Page 17: Python for Practice - cuni.cz

17

Deployment

CGI

FastCGI

mod_wsgi (Apache)

from wsgiref.handlers import CGIHandler

from yourapplication import app

CGIHandler().run(app)

#!/usr/bin/python

from flup.server.fcgi import WSGIServer

from yourapplication import app

if __name__ == '__main__':

WSGIServer(app).run()

Page 18: Python for Practice - cuni.cz

18

Routing

@app.route('/')

def index():

return 'Index Page'

@app.route('/hello')

def hello():

return 'Hello, World'

Page 19: Python for Practice - cuni.cz

19

Routing

string (default) accepts any text without a slash

int accepts positive integers

float accepts positive floating point values

path like string but also accepts slashes

uuid accepts UUID strings

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

def show_user_profile(username):

return 'User %s' % escape(username)

@app.route('/post/<int:post_id>')

def show_post(post_id):

return 'Post %d' % post_id

@app.route('/path/<path:subpath>')

def show_subpath(subpath):

return 'Subpath %s' % escape(subpath)

Page 20: Python for Practice - cuni.cz

20

Routing

Building [email protected]('/')

def index():

return 'index'

@app.route('/login')

def login():

return 'login'

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

def profile(username):

return escape(username)

with app.test_request_context():

print(url_for('index'))

print(url_for('login'))

print(url_for('login', next='/'))

print(url_for('profile', username='John Doe'))

Page 21: Python for Practice - cuni.cz

21

HTTP method

@app.route('/login', methods=['GET', 'POST'])

def login():

if request.method == 'POST':

return do_the_login()

else:

return show_the_login_form()

Page 22: Python for Practice - cuni.cz

22

Static files and templates

Static files

have to be stored in static/ subdirectory

Templates

Jinja2 preconfigured

directory templates/

url_for('static', filename='style.css')

@app.route('/hello/')

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

def hello(name=None):

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

See

e03_templete.py

Page 23: Python for Practice - cuni.cz

23

Important objects

requestrepresents request for a browser

fieldspath

full_path

script_root

url

base_url

url_root

host

args

form

method

scheme

cookies

@app.route('/login')

def login():

u = request.form['username'],

p = request.form['password']):

...

See

e04_request.py

Page 24: Python for Practice - cuni.cz

24

File uploads

HTML form must have the

enctype="multipart/form-data" attribute

@app.route('/upload', methods=['GET', 'POST'])

def upload_file():

if request.method == 'POST':

f = request.files['the_file']

f.save('/var/www/uploads/uploaded_file.txt')

Page 25: Python for Practice - cuni.cz

25

Errors and redirects

@app.route('/')

def index():

return redirect(url_for('login'))

@app.route('/login')

def login():

abort(401)

this_is_never_executed()

@app.errorhandler(404)

def page_not_found(error):

return render_template(

'page_not_found.html'), 404

HTTP error code(default 200)

See

e05_redirect.py

Page 26: Python for Practice - cuni.cz

26

Response

returning

string -> default response

dict -> json

@app.route('/')

def index():

resp = make_response(render_template(...))

return resp

See

e06_response.py

Page 27: Python for Practice - cuni.cz

27

Assignment

Create a weather service

methods for obtaining

a list of cities

weather for a city

methods return json

Page 28: Python for Practice - cuni.cz

28

Sessions

session object

to store information specific to a user from one

request to the next

implemented via cookies

See

e07_session.py

Page 29: Python for Practice - cuni.cz

29

Extensions

Huge amount of extensions

search on PyPI

Page 30: Python for Practice - cuni.cz

30

Assignment

Create a web app for managing users on a

unix system (i.e., an app that manipulates

/etc/passwd)

create own copy of the passwd file

operations for listing, adding and removing users

Update the app to require a login

session needed

Page 31: Python for Practice - cuni.cz

31The slides are licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.


Recommended