Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,36 @@
|
|
| 1 |
-
from flask import Flask, request, redirect
|
| 2 |
-
import json
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
app = Flask(
|
| 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 |
-
|
| 19 |
-
with open(DB_FILE, '
|
| 20 |
-
json.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
def
|
| 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 |
-
|
| 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('/
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 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)
|
|
|
|
| 1 |
+
from flask import Flask, request, redirect import json import os
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
app = Flask(name) DB_FILE = 'data.json'
|
|
|
|
| 4 |
|
| 5 |
+
def load_products(): # Создаём файл, если его нет if not os.path.exists(DB_FILE): with open(DB_FILE, 'w') as f: json.dump([], f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
try:
|
| 8 |
+
with open(DB_FILE, 'r') as f:
|
| 9 |
+
return json.load(f)
|
| 10 |
+
except json.JSONDecodeError:
|
| 11 |
+
return [] # Если файл пустой или повреждён, возвращаем пустой список
|
| 12 |
+
except Exception as e:
|
| 13 |
+
print(f"Ошибка при загрузке данных: {e}")
|
| 14 |
+
return []
|
| 15 |
|
| 16 |
+
def save_products(products): try: with open(DB_FILE, 'w') as f: json.dump(products, f, indent=4, ensure_ascii=False) except Exception as e: print(f"Ошибка при сохранении данных: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def html_wrapper(content): return f''' <!DOCTYPE html> <html> <head> <title>Каталог</title> <style> body {{ font-family: Arial, sans-serif; margin: 20px; }} .header {{ font-size: 24px; margin-bottom: 20px; }} .products {{ display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; }} .product {{ border: 1px solid #ddd; padding: 15px; border-radius: 5px; }} .product img {{ max-width: 200px; max-height: 200px; }} form {{ max-width: 500px; margin: 20px auto; }} input, textarea {{ width: 100%; margin: 5px 0; padding: 8px; }} button {{ background: #4CAF50; color: white; padding: 10px; border: none; cursor: pointer; }} nav {{ margin-bottom: 20px; }} a {{ margin-right: 15px; text-decoration: none; color: #333; }} </style> </head> <body> <nav> <a href="/">Каталог</a> <a href="/admin">Админка</a> </nav> {content} </body> </html> '''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
@app.route('/') def catalog(): products = load_products() products_html = ''.join([ f'''<div class="product"> <h3>{p['name']}</h3> <img src="{p['image']}" alt="{p['name']}"> <p>{p['description']}</p> <p>Цена: {p['price']} руб.</p> </div>''' for p in products ]) return html_wrapper(f''' <div class="header">Каталог товаров</div> <div class="products">{products_html}</div> ''')
|
| 21 |
+
|
| 22 |
+
@app.route('/admin', methods=['GET', 'POST']) def admin(): if request.method == 'POST': products = load_products() products.append({ 'name': request.form['name'], 'description': request.form['description'], 'price': request.form['price'], 'image': request.form['image'] }) save_products(products) return redirect('/admin')
|
| 23 |
+
|
| 24 |
+
return html_wrapper('''
|
| 25 |
+
<div class="header">Админ-панель</div>
|
| 26 |
+
<form method="POST">
|
| 27 |
+
<input type="text" name="name" placeholder="Название" required>
|
| 28 |
+
<textarea name="description" placeholder="Описание" required></textarea>
|
| 29 |
+
<input type="number" name="price" placeholder="Цена" required>
|
| 30 |
+
<input type="url" name="image" placeholder="URL изображения" required>
|
| 31 |
+
<button type="submit">Добавить товар</button>
|
| 32 |
+
</form>
|
| 33 |
+
''')
|
| 34 |
+
|
| 35 |
+
if name == 'main': app.run(host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
|
|