Skip to content

Commit 1c1015e

Browse files
committed
Add open lesson Flask intro 23.05.2022
1 parent 527612e commit 1c1015e

9 files changed

Lines changed: 193 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Быстрая разработка JSON API приложений на Flask (день 2) от 20.08.2021](open-lessons/flask-jsonapi.20.08.2021/)
88
- [Декораторы в Python от 15.02.2022](open-lessons/decorators.15.02.2022/)
99
- [Функции-помощники: map, filter, reduce от 21.02.2022](open-lessons/helper-funcs.21.02.2022/)
10+
- [Знакомство с веб-разработкой на Flask от 23.05.2022](open-lessons/flask.23.05.2022/)
1011

1112

1213
### Курсы Python Basic:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from flask import Flask, request
2+
3+
from views.products import products_app
4+
5+
app = Flask(__name__)
6+
app.config.update(ENV="development")
7+
8+
app.register_blueprint(
9+
products_app,
10+
url_prefix="/products",
11+
)
12+
13+
14+
@app.route("/")
15+
def hello_world():
16+
# another_func()
17+
return "<p>Hello, World!!</p>"
18+
19+
20+
def another_func():
21+
print(request)
22+
print(request.path)
23+
24+
25+
@app.route("/hello/")
26+
def hello_view():
27+
# another_func()
28+
name = request.args.get("name", "World")
29+
name = name.strip()
30+
if not name:
31+
name = "World"
32+
return {"message": f"Hello {name}!"}
33+
34+
35+
@app.route("/items/<int:item_id>/")
36+
def get_item(item_id: int):
37+
return {"item_id": item_id}
38+
39+
40+
@app.route("/items/<string:item_id>/")
41+
def get_item_str(item_id: str):
42+
return {"item": {"id": item_id.lower()}}
43+
44+
45+
if __name__ == '__main__':
46+
app.run(debug=True)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
click==8.1.3
2+
Flask==2.1.2
3+
itsdangerous==2.1.2
4+
Jinja2==3.1.2
5+
MarkupSafe==2.1.1
6+
Werkzeug==2.1.2
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>
6+
{% block title %}
7+
Base Title
8+
{% endblock %}
9+
</title>
10+
</head>
11+
<body>
12+
{% block body %}
13+
Base Body
14+
{# <small>#}
15+
{# {% block small %}#}
16+
{# small text#}
17+
{# {% endblock %}#}
18+
{# </small>#}
19+
{% endblock %}
20+
</body>
21+
<footer>
22+
<small>
23+
OTUS &copy; 2022
24+
</small>
25+
</footer>
26+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% extends 'base.html' %}
2+
3+
{% block body %}
4+
<div>
5+
<form method="post">
6+
<label for="id-name">Product name:</label>
7+
<input type="text" id="id-name" name="product-name" required>
8+
9+
<button type="submit">Create</button>
10+
</form>
11+
</div>
12+
{% endblock %}
13+
14+
{% block title %}
15+
Add product
16+
{% endblock %}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends 'base.html' %}
2+
3+
{% block title %}
4+
Product #{{ product_id }}
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Product {{ product_name }}</h1>
9+
10+
<div>
11+
<a href="{{ url_for('products_app.list') }}">
12+
Back to products list
13+
</a>
14+
</div>
15+
{% endblock %}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{% extends 'base.html' %}
2+
3+
{% block title %}
4+
Products List
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Products list:</h1>
9+
<div>
10+
<ul>
11+
{% for product_id, product_name in products %}
12+
<li>
13+
<a href="{{ url_for('products_app.details', product_id=product_id) }}">
14+
{{ product_name }}
15+
</a>
16+
</li>
17+
{% endfor %}
18+
</ul>
19+
</div>
20+
<div>
21+
<a href="{{ url_for('products_app.add') }}">
22+
Create product</a>
23+
</div>
24+
{% endblock %}
25+
26+
27+
{#{% block small %}#}
28+
{#some products#}
29+
{#{% endblock %}#}

open-lessons/flask.23.05.2022/views/__init__.py

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from flask import (
2+
Blueprint,
3+
render_template,
4+
request,
5+
redirect,
6+
url_for,
7+
)
8+
from werkzeug.exceptions import BadRequest, NotFound
9+
10+
products_app = Blueprint("products_app", __name__)
11+
12+
PRODUCTS = {
13+
1: "Laptop",
14+
2: "Desktop",
15+
3: "Smartphone",
16+
}
17+
18+
19+
@products_app.route("/", endpoint="list")
20+
def products_list():
21+
return render_template(
22+
"products/list.html",
23+
products=list(PRODUCTS.items()),
24+
)
25+
26+
27+
@products_app.route("/<int:product_id>/", endpoint="details")
28+
def get_product(product_id: int):
29+
product_name = PRODUCTS.get(product_id)
30+
if product_name is None:
31+
raise NotFound(f"product #{product_id} doesn't exist!")
32+
return render_template(
33+
"products/details.html",
34+
product_id=product_id,
35+
product_name=product_name,
36+
)
37+
38+
39+
@products_app.route("/add/", methods=["GET", "POST"], endpoint="add")
40+
def add_product():
41+
if request.method == "GET":
42+
return render_template("products/add.html")
43+
44+
# if request.method == "POST":
45+
# pass
46+
47+
product_name = request.form.get("product-name")
48+
if not product_name:
49+
raise BadRequest("Field `product-name` required!")
50+
51+
product_id = len(PRODUCTS) + 1
52+
PRODUCTS[product_id] = product_name
53+
url = url_for("products_app.details", product_id=product_id)
54+
return redirect(url)

0 commit comments

Comments
 (0)