examples for flask workshop

master
Simon Pirkelmann 2020-11-22 14:41:09 +01:00
commit a0ecab2b0f
22 changed files with 289 additions and 0 deletions

12
00-hello-world.py Normal file
View File

@ -0,0 +1,12 @@
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()

View File

@ -0,0 +1,11 @@
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
@app.route('/index')
def index():
user = {'username': 'Simon'}
return render_template('index.html', title='Home', user=user)
if __name__ == '__main__':
app.run()

10
02-template-even.py Normal file
View File

@ -0,0 +1,10 @@
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
numbers = range(1,25)
return render_template('evens.html', title='Evens', numbers=numbers)
if __name__ == '__main__':
app.run()

12
03-template-primes.py Normal file
View File

@ -0,0 +1,12 @@
from sympy import isprime
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
@app.route('/index')
def index():
numbers = range(1,25)
return render_template('colorful-primes.html', title='Primes', numbers=numbers, isprime=isprime)
if __name__ == '__main__':
app.run()

View File

@ -0,0 +1,9 @@
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def page1():
return render_template('page1.html')
if __name__ == '__main__':
app.run()

12
05-input-primes.py Normal file
View File

@ -0,0 +1,12 @@
from sympy import isprime
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/primes')
@app.route('/primes/<max_number>')
def primes(max_number=10):
numbers = range(1,int(max_number)+1)
return render_template('colorful-primes.html', title='Primes', numbers=numbers, isprime=isprime)
if __name__ == '__main__':
app.run()

12
06-input-factorial.py Normal file
View File

@ -0,0 +1,12 @@
from flask import Flask, render_template
from functools import reduce
from operator import mul
app = Flask(__name__)
@app.route('/factorial/<int:number>')
def index(number):
factorial = reduce(mul, range(1,number+1))
return render_template('factorial.html', number=number, factorial=factorial)
if __name__ == '__main__':
app.run()

10
07-input-url.py Normal file
View File

@ -0,0 +1,10 @@
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/starter')
@app.route('/starter/<choice>')
def starter(choice=None):
return render_template('starter.html', choice=choice)
if __name__ == '__main__':
app.run()

19
08-request-handling.py Normal file
View File

@ -0,0 +1,19 @@
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
counter = 0
@app.route('/', methods=('GET', 'POST'))
def index():
global counter
if request.method == 'GET':
return render_template('request-handling.html', counter=counter)
else:
if request.form.get('btn') == 'down':
counter -= 1
elif request.form.get('btn') == 'up':
counter += 1
return redirect(url_for('index'))
if __name__ == '__main__':
app.run()

16
09-sessions.py Normal file
View File

@ -0,0 +1,16 @@
from flask import Flask, render_template, request, make_response
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'GET':
name = request.cookies.get('name')
return render_template('cookies.html', name=name)
else:
name = request.form['name']
resp = make_response(render_template('cookies.html', name=name))
resp.set_cookie('name', name)
return resp
if __name__ == '__main__':
app.run()

19
10-debugger.py Normal file
View File

@ -0,0 +1,19 @@
from flask import Flask, render_template
from flask_debugtoolbar import DebugToolbarExtension
import random
app = Flask(__name__)
app.config['SECRET_KEY'] = '<replace with a secret key>'
app.debug = True
toolbar = DebugToolbarExtension(app)
@app.route('/')
def index():
coin = random.random()
if coin >= 0.5:
return render_template('index.html')
else:
raise Exception("Bad luck")
if __name__ == '__main__':
app.run(debug=True)

30
decorator-example.py Normal file
View File

@ -0,0 +1,30 @@
from random import random as rnd
def add_lametta(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs).replace('+++', '+/+')
return wrapper
def add_bulbs(func):
def wrapper(*args, **kwargs):
return "".join([x if x in ' \n' or rnd() < 0.9 else 'o' for x in func(*args, **kwargs)])
return wrapper
def add_stars(func):
def wrapper(*args, **kwargs):
return "".join([x if x in ' \n' or rnd() < 0.9 else '*' for x in func(*args, **kwargs)])
return wrapper
@add_lametta
@add_bulbs
def generate_christmas_tree(height):
return "\n".join(["".join(['+' if abs(j - (height)/2) < i/2 else ' '
for j in range(height)]) for i in range(height)])
def generate_xmas(height):
return "\n".join([' ' * (height - i) + '+' * 2 * i + ' ' * (height - i) for i in range(height)])
print("christmas tree:")
print(generate_christmas_tree(14))
print(generate_xmas(10))

3
static/css/style.css Normal file
View File

@ -0,0 +1,3 @@
body {
background-color: #f1f1f1;
}

View File

@ -0,0 +1,14 @@
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
{% for number in numbers %}
{% if isprime(number) %}
<b style="color:blue">{{ number }}</b>
{% else %}
{{ number }}
{% endif %}
{% endfor %}
</body>
</html>

13
templates/cookies.html Normal file
View File

@ -0,0 +1,13 @@
<html>
<body>
{% if name is none %}
<form method = "POST">
<p><h3>Enter your name:</h3></p>
<p><input type = 'text' name = 'name'/></p>
<p><input type = 'submit' value = 'Send'/></p>
</form>
{% else %}
<p><h3>Hello {{ name }}! I will remember you..</h3></p>
{% endif %}
</body>
</html>

14
templates/evens.html Normal file
View File

@ -0,0 +1,14 @@
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
{% for number in numbers %}
{% if number % 2 == 0 %}
<b style="color:limegreen">{{ number }}</b>
{% else %}
{{ number }}
{% endif %}
{% endfor %}
</body>
</html>

8
templates/factorial.html Normal file
View File

@ -0,0 +1,8 @@
<html>
<head>
<title>Factorial</title>
</head>
<body>
The factorial of {{ number }} = {{ factorial }}
</body>
</html>

8
templates/index.html Normal file
View File

@ -0,0 +1,8 @@
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Hello, {{ user.username }}!</h1>
</body>
</html>

17
templates/layout.html Normal file
View File

@ -0,0 +1,17 @@
<!doctype html>
<html>
<head>
{% block head %}
<link rel="stylesheet" href="{{ url_for('static', filename='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 2010 by <a href="http://domain.invalid/">you</a>.
{% endblock %}
</div>
</body>
</html>

13
templates/page1.html Normal file
View File

@ -0,0 +1,13 @@
{% extends "layout.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 on my awesome homepage.
{% endblock %}

View File

@ -0,0 +1,12 @@
<html>
<head>
<title>Counter</title>
</head>
<body>
<h1>Counter = {{ counter }}</h1>
</body>
<form method="post">
<input type="submit" name="btn" value="up">
<input type="submit" name="btn" value="down">
</form>
</html>

15
templates/starter.html Normal file
View File

@ -0,0 +1,15 @@
<html>
<head>
<title>Choose your starter:</title>
</head>
<body>
{% if choice == None %}
Choose your starter:
<p><a href="{{ url_for('starter', choice='Bulbasaur') }}">Bulbasaur</a></p>
<p><a href="{{ url_for('starter', choice='Charmander') }}">Charmander</a></p>
<p><a href="{{ url_for('starter', choice='Squirtle') }}">Squirtle</a></p>
{% else %}
You chose {{ choice }}!
{% endif %}
</body>
</html>