Aleksmorshen commited on
Commit
8b919e0
·
verified ·
1 Parent(s): 077d27a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, redirect
2
+ import json
3
+ import os
4
+
5
+ app = Flask(__name__)
6
+ DB_FILE = 'data.json'
7
+
8
+ def load_products():
9
+ if not os.path.exists(DB_FILE):
10
+ with open(DB_FILE, 'w') as f:
11
+ json.dump([], f)
12
+ try:
13
+ with open(DB_FILE, 'r') as f:
14
+ return json.load(f)
15
+ except:
16
+ return []
17
+
18
+ def save_products(products):
19
+ with open(DB_FILE, 'w') as f:
20
+ json.dump(products, f)
21
+
22
+ def html_wrapper(content):
23
+ return f'''
24
+ <!DOCTYPE html>
25
+ <html>
26
+ <head>
27
+ <title>Каталог</title>
28
+ <style>
29
+ body {{ font-family: Arial, sans-serif; margin: 20px; }}
30
+ .header {{ font-size: 24px; margin-bottom: 20px; }}
31
+ .products {{ display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; }}
32
+ .product {{ border: 1px solid #ddd; padding: 15px; border-radius: 5px; }}
33
+ .product img {{ max-width: 200px; max-height: 200px; }}
34
+ form {{ max-width: 500px; margin: 20px auto; }}
35
+ input, textarea {{ width: 100%; margin: 5px 0; padding: 8px; }}
36
+ button {{ background: #4CAF50; color: white; padding: 10px; border: none; cursor: pointer; }}
37
+ nav {{ margin-bottom: 20px; }}
38
+ a {{ margin-right: 15px; text-decoration: none; color: #333; }}
39
+ </style>
40
+ </head>
41
+ <body>
42
+ <nav>
43
+ <a href="/">Каталог</a>
44
+ <a href="/admin">Админка</a>
45
+ </nav>
46
+ {content}
47
+ </body>
48
+ </html>
49
+ '''
50
+
51
+ @app.route('/')
52
+ def catalog():
53
+ products = load_products()
54
+ products_html = ''.join([
55
+ f'''<div class="product">
56
+ <h3>{p['name']}</h3>
57
+ <img src="{p['image']}">
58
+ <p>{p['description']}</p>
59
+ <p>Цена: {p['price']} руб.</p>
60
+ </div>'''
61
+ for p in products
62
+ ])
63
+ return html_wrapper(f'''
64
+ <div class="header">Каталог товаров</div>
65
+ <div class="products">{products_html}</div>
66
+ ''')
67
+
68
+ @app.route('/admin', methods=['GET', 'POST'])
69
+ def admin():
70
+ if request.method == 'POST':
71
+ products = load_products()
72
+ products.append({
73
+ 'name': request.form['name'],
74
+ 'description': request.form['description'],
75
+ 'price': request.form['price'],
76
+ 'image': request.form['image']
77
+ })
78
+ save_products(products)
79
+ return redirect('/admin')
80
+
81
+ return html_wrapper('''
82
+ <div class="header">Админ-панель</div>
83
+ <form method="POST">
84
+ <input type="text" name="name" placeholder="Название" required>
85
+ <textarea name="description" placeholder="Описание" required></textarea>
86
+ <input type="number" name="price" placeholder="Цена" required>
87
+ <input type="url" name="image" placeholder="URL изображения" required>
88
+ <button type="submit">Добавить товар</button>
89
+ </form>
90
+ ''')
91
+
92
+ if __name__ == '__main__':
93
+ app.run(host='0.0.0.0', port=7860)