commit a0ecab2b0f81413f3ed25b00faa4238ae5e3489e Author: Simon Pirkelmann Date: Sun Nov 22 14:41:09 2020 +0100 examples for flask workshop diff --git a/00-hello-world.py b/00-hello-world.py new file mode 100644 index 0000000..4b059ec --- /dev/null +++ b/00-hello-world.py @@ -0,0 +1,12 @@ +from flask import Flask + +app = Flask(__name__) + + +@app.route('/') +def hello_world(): + return 'Hello World!' + + +if __name__ == '__main__': + app.run() diff --git a/01-hello-world-templates.py b/01-hello-world-templates.py new file mode 100644 index 0000000..01ec6c1 --- /dev/null +++ b/01-hello-world-templates.py @@ -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() \ No newline at end of file diff --git a/02-template-even.py b/02-template-even.py new file mode 100644 index 0000000..37acb7c --- /dev/null +++ b/02-template-even.py @@ -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() \ No newline at end of file diff --git a/03-template-primes.py b/03-template-primes.py new file mode 100644 index 0000000..83c2975 --- /dev/null +++ b/03-template-primes.py @@ -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() \ No newline at end of file diff --git a/04-template-inheritance.py b/04-template-inheritance.py new file mode 100644 index 0000000..ed748af --- /dev/null +++ b/04-template-inheritance.py @@ -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() \ No newline at end of file diff --git a/05-input-primes.py b/05-input-primes.py new file mode 100644 index 0000000..fabd2ff --- /dev/null +++ b/05-input-primes.py @@ -0,0 +1,12 @@ +from sympy import isprime +from flask import Flask, render_template +app = Flask(__name__) + +@app.route('/primes') +@app.route('/primes/') +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() \ No newline at end of file diff --git a/06-input-factorial.py b/06-input-factorial.py new file mode 100644 index 0000000..2ec5a94 --- /dev/null +++ b/06-input-factorial.py @@ -0,0 +1,12 @@ +from flask import Flask, render_template +from functools import reduce +from operator import mul +app = Flask(__name__) + +@app.route('/factorial/') +def index(number): + factorial = reduce(mul, range(1,number+1)) + return render_template('factorial.html', number=number, factorial=factorial) + +if __name__ == '__main__': + app.run() \ No newline at end of file diff --git a/07-input-url.py b/07-input-url.py new file mode 100644 index 0000000..88ab41e --- /dev/null +++ b/07-input-url.py @@ -0,0 +1,10 @@ +from flask import Flask, render_template +app = Flask(__name__) + +@app.route('/starter') +@app.route('/starter/') +def starter(choice=None): + return render_template('starter.html', choice=choice) + +if __name__ == '__main__': + app.run() \ No newline at end of file diff --git a/08-request-handling.py b/08-request-handling.py new file mode 100644 index 0000000..81c016f --- /dev/null +++ b/08-request-handling.py @@ -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() \ No newline at end of file diff --git a/09-sessions.py b/09-sessions.py new file mode 100644 index 0000000..36bc190 --- /dev/null +++ b/09-sessions.py @@ -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() \ No newline at end of file diff --git a/10-debugger.py b/10-debugger.py new file mode 100644 index 0000000..702d128 --- /dev/null +++ b/10-debugger.py @@ -0,0 +1,19 @@ +from flask import Flask, render_template +from flask_debugtoolbar import DebugToolbarExtension + +import random +app = Flask(__name__) +app.config['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) diff --git a/decorator-example.py b/decorator-example.py new file mode 100644 index 0000000..565c7fa --- /dev/null +++ b/decorator-example.py @@ -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)) \ No newline at end of file diff --git a/static/css/style.css b/static/css/style.css new file mode 100644 index 0000000..4a07967 --- /dev/null +++ b/static/css/style.css @@ -0,0 +1,3 @@ +body { + background-color: #f1f1f1; +} \ No newline at end of file diff --git a/templates/colorful-primes.html b/templates/colorful-primes.html new file mode 100644 index 0000000..a2f0759 --- /dev/null +++ b/templates/colorful-primes.html @@ -0,0 +1,14 @@ + + + {{ title }} + + + {% for number in numbers %} + {% if isprime(number) %} + {{ number }} + {% else %} + {{ number }} + {% endif %} + {% endfor %} + + \ No newline at end of file diff --git a/templates/cookies.html b/templates/cookies.html new file mode 100644 index 0000000..c391438 --- /dev/null +++ b/templates/cookies.html @@ -0,0 +1,13 @@ + + + {% if name is none %} +
+

Enter your name:

+

+

+
+ {% else %} +

Hello {{ name }}! I will remember you..

+ {% endif %} + + diff --git a/templates/evens.html b/templates/evens.html new file mode 100644 index 0000000..d5417f9 --- /dev/null +++ b/templates/evens.html @@ -0,0 +1,14 @@ + + + {{ title }} + + + {% for number in numbers %} + {% if number % 2 == 0 %} + {{ number }} + {% else %} + {{ number }} + {% endif %} + {% endfor %} + + \ No newline at end of file diff --git a/templates/factorial.html b/templates/factorial.html new file mode 100644 index 0000000..a24a8b7 --- /dev/null +++ b/templates/factorial.html @@ -0,0 +1,8 @@ + + + Factorial + + + The factorial of {{ number }} = {{ factorial }} + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..3802b89 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,8 @@ + + + {{ title }} + + +

Hello, {{ user.username }}!

+ + \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..f7c3878 --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,17 @@ + + + + {% block head %} + + {% block title %}{% endblock %} - My Webpage + {% endblock %} + + +
{% block content %}{% endblock %}
+ + + \ No newline at end of file diff --git a/templates/page1.html b/templates/page1.html new file mode 100644 index 0000000..37bd1d0 --- /dev/null +++ b/templates/page1.html @@ -0,0 +1,13 @@ +{% extends "layout.html" %} +{% block title %}Index{% endblock %} +{% block head %} + {{ super() }} + +{% endblock %} +{% block content %} +

Index

+

+ Welcome on my awesome homepage. +{% endblock %} \ No newline at end of file diff --git a/templates/request-handling.html b/templates/request-handling.html new file mode 100644 index 0000000..84aba27 --- /dev/null +++ b/templates/request-handling.html @@ -0,0 +1,12 @@ + + + Counter + + +

Counter = {{ counter }}

+ +
+ + +
+ \ No newline at end of file diff --git a/templates/starter.html b/templates/starter.html new file mode 100644 index 0000000..f2670c5 --- /dev/null +++ b/templates/starter.html @@ -0,0 +1,15 @@ + + + Choose your starter: + + + {% if choice == None %} + Choose your starter: +

Bulbasaur

+

Charmander

+

Squirtle

+ {% else %} + You chose {{ choice }}! + {% endif %} + + \ No newline at end of file