Update app.py
Browse files
app.py
CHANGED
|
@@ -1,117 +1,178 @@
|
|
| 1 |
-
from flask import Flask, request, redirect
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
print(f"Ошибка при создании файла {DB_FILE}: {e}")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
with open(DB_FILE, 'r', encoding='utf-8') as f:
|
| 21 |
-
products = json.load(f)
|
| 22 |
-
print(f"Данные успешно загружены из {DB_FILE}: {products}")
|
| 23 |
-
return products
|
| 24 |
-
except (json.JSONDecodeError, Exception) as e:
|
| 25 |
-
print(f"Ошибка при чтении файла {DB_FILE}: {e}")
|
| 26 |
-
return []
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
with open(DB_FILE, 'w', encoding='utf-8') as f:
|
| 31 |
-
json.dump(products, f, indent=4)
|
| 32 |
-
print(f"Данные успешно сохранены в {DB_FILE}: {products}")
|
| 33 |
-
except Exception as e:
|
| 34 |
-
print(f"Ошибка при записи в файл {DB_FILE}: {e}")
|
| 35 |
-
|
| 36 |
-
def html_wrapper(content):
|
| 37 |
-
return f'''
|
| 38 |
-
<!DOCTYPE html>
|
| 39 |
-
<html>
|
| 40 |
-
<head>
|
| 41 |
-
<title>Каталог</title>
|
| 42 |
-
<style>
|
| 43 |
-
body {{ font-family: Arial, sans-serif; margin: 20px; }}
|
| 44 |
-
.header {{ font-size: 24px; margin-bottom: 20px; }}
|
| 45 |
-
.products {{ display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; }}
|
| 46 |
-
.product {{ border: 1px solid #ddd; padding: 15px; border-radius: 5px; }}
|
| 47 |
-
form {{ max-width: 500px; margin: 20px auto; }}
|
| 48 |
-
input, textarea {{ width: 100%; margin: 5px 0; padding: 8px; }}
|
| 49 |
-
button {{ background: #4CAF50; color: white; padding: 10px; border: none; cursor: pointer; }}
|
| 50 |
-
nav {{ margin-bottom: 20px; }}
|
| 51 |
-
a {{ margin-right: 15px; text-decoration: none; color: #333; }}
|
| 52 |
-
</style>
|
| 53 |
-
</head>
|
| 54 |
-
<body>
|
| 55 |
-
<nav>
|
| 56 |
-
<a href="/">Каталог</a>
|
| 57 |
-
<a href="/admin">Админка</a>
|
| 58 |
-
</nav>
|
| 59 |
-
{content}
|
| 60 |
-
</body>
|
| 61 |
-
</html>
|
| 62 |
-
'''
|
| 63 |
-
|
| 64 |
-
@app.route('/')
|
| 65 |
def catalog():
|
| 66 |
-
products =
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
<
|
| 73 |
-
<
|
| 74 |
-
<
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
|
|
|
| 83 |
@app.route('/admin', methods=['GET', 'POST'])
|
| 84 |
def admin():
|
| 85 |
if request.method == 'POST':
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
<
|
| 105 |
-
<form method="POST">
|
| 106 |
-
<input type="text" name="name" placeholder="Название" required>
|
| 107 |
-
<textarea name="description" placeholder="Описание" required></textarea>
|
| 108 |
-
<input type="number" name="price" placeholder="Цена" required>
|
| 109 |
-
<button type="submit">Добавить товар</button>
|
| 110 |
-
</form>
|
| 111 |
-
''')
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template_string, request, redirect, url_for, jsonify
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
+
DATA_FILE = 'products.json'
|
| 7 |
|
| 8 |
+
# Загрузка данных из JSON-файла
|
| 9 |
+
def load_data():
|
| 10 |
+
if os.path.exists(DATA_FILE):
|
| 11 |
+
with open(DATA_FILE, 'r', encoding='utf-8') as file:
|
| 12 |
+
return json.load(file)
|
| 13 |
+
return []
|
|
|
|
| 14 |
|
| 15 |
+
# Сохранение данных в JSON-файл
|
| 16 |
+
def save_data(data):
|
| 17 |
+
with open(DATA_FILE, 'w', encoding='utf-8') as file:
|
| 18 |
+
json.dump(data, file, ensure_ascii=False, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Главная страница каталога
|
| 21 |
+
@app.route('/catalog')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def catalog():
|
| 23 |
+
products = load_data()
|
| 24 |
+
catalog_html = '''
|
| 25 |
+
<!DOCTYPE html>
|
| 26 |
+
<html lang="en">
|
| 27 |
+
<head>
|
| 28 |
+
<meta charset="UTF-8">
|
| 29 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 30 |
+
<title>Каталог</title>
|
| 31 |
+
<style>
|
| 32 |
+
body {
|
| 33 |
+
font-family: Arial, sans-serif;
|
| 34 |
+
margin: 20px;
|
| 35 |
+
background-color: #f9f9f9;
|
| 36 |
+
}
|
| 37 |
+
h1 {
|
| 38 |
+
color: #333;
|
| 39 |
+
}
|
| 40 |
+
.product {
|
| 41 |
+
background-color: #fff;
|
| 42 |
+
border: 1px solid #ddd;
|
| 43 |
+
padding: 15px;
|
| 44 |
+
margin-bottom: 10px;
|
| 45 |
+
border-radius: 5px;
|
| 46 |
+
}
|
| 47 |
+
.product h2 {
|
| 48 |
+
margin-top: 0;
|
| 49 |
+
color: #555;
|
| 50 |
+
}
|
| 51 |
+
.product p {
|
| 52 |
+
color: #777;
|
| 53 |
+
}
|
| 54 |
+
</style>
|
| 55 |
+
</head>
|
| 56 |
+
<body>
|
| 57 |
+
<h1>Каталог товаров</h1>
|
| 58 |
+
{% for product in products %}
|
| 59 |
+
<div class="product">
|
| 60 |
+
<h2>{{ product['name'] }}</h2>
|
| 61 |
+
<p><strong>Цена:</strong> {{ product['price'] }} руб.</p>
|
| 62 |
+
<p><strong>Описание:</strong> {{ product['description'] }}</p>
|
| 63 |
+
</div>
|
| 64 |
+
{% endfor %}
|
| 65 |
+
</body>
|
| 66 |
+
</html>
|
| 67 |
+
'''
|
| 68 |
+
return render_template_string(catalog_html, products=products)
|
| 69 |
|
| 70 |
+
# Админ-панель для добавления товаров
|
| 71 |
@app.route('/admin', methods=['GET', 'POST'])
|
| 72 |
def admin():
|
| 73 |
if request.method == 'POST':
|
| 74 |
+
name = request.form.get('name')
|
| 75 |
+
price = request.form.get('price')
|
| 76 |
+
description = request.form.get('description')
|
| 77 |
+
|
| 78 |
+
if name and price and description:
|
| 79 |
+
products = load_data()
|
| 80 |
+
products.append({
|
| 81 |
+
'name': name,
|
| 82 |
+
'price': price,
|
| 83 |
+
'description': description
|
| 84 |
+
})
|
| 85 |
+
save_data(products)
|
| 86 |
+
return redirect(url_for('admin'))
|
| 87 |
|
| 88 |
+
products = load_data()
|
| 89 |
+
admin_html = '''
|
| 90 |
+
<!DOCTYPE html>
|
| 91 |
+
<html lang="en">
|
| 92 |
+
<head>
|
| 93 |
+
<meta charset="UTF-8">
|
| 94 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 95 |
+
<title>Админ-панель</title>
|
| 96 |
+
<style>
|
| 97 |
+
body {
|
| 98 |
+
font-family: Arial, sans-serif;
|
| 99 |
+
margin: 20px;
|
| 100 |
+
background-color: #f9f9f9;
|
| 101 |
+
}
|
| 102 |
+
h1 {
|
| 103 |
+
color: #333;
|
| 104 |
+
}
|
| 105 |
+
form {
|
| 106 |
+
background-color: #fff;
|
| 107 |
+
padding: 20px;
|
| 108 |
+
border: 1px solid #ddd;
|
| 109 |
+
border-radius: 5px;
|
| 110 |
+
max-width: 400px;
|
| 111 |
}
|
| 112 |
+
label {
|
| 113 |
+
display: block;
|
| 114 |
+
margin-top: 10px;
|
| 115 |
+
color: #555;
|
| 116 |
+
}
|
| 117 |
+
input, textarea {
|
| 118 |
+
width: 100%;
|
| 119 |
+
padding: 8px;
|
| 120 |
+
margin-top: 5px;
|
| 121 |
+
border: 1px solid #ddd;
|
| 122 |
+
border-radius: 4px;
|
| 123 |
+
}
|
| 124 |
+
button {
|
| 125 |
+
margin-top: 15px;
|
| 126 |
+
padding: 10px 15px;
|
| 127 |
+
background-color: #28a745;
|
| 128 |
+
color: white;
|
| 129 |
+
border: none;
|
| 130 |
+
border-radius: 4px;
|
| 131 |
+
cursor: pointer;
|
| 132 |
+
}
|
| 133 |
+
button:hover {
|
| 134 |
+
background-color: #218838;
|
| 135 |
+
}
|
| 136 |
+
.product-list {
|
| 137 |
+
margin-top: 20px;
|
| 138 |
+
}
|
| 139 |
+
.product-item {
|
| 140 |
+
background-color: #fff;
|
| 141 |
+
border: 1px solid #ddd;
|
| 142 |
+
padding: 10px;
|
| 143 |
+
margin-bottom: 10px;
|
| 144 |
+
border-radius: 5px;
|
| 145 |
+
}
|
| 146 |
+
</style>
|
| 147 |
+
</head>
|
| 148 |
+
<body>
|
| 149 |
+
<h1>Добавление товара</h1>
|
| 150 |
+
<form method="POST">
|
| 151 |
+
<label for="name">Название товара:</label>
|
| 152 |
+
<input type="text" id="name" name="name" required>
|
| 153 |
|
| 154 |
+
<label for="price">Цена:</label>
|
| 155 |
+
<input type="number" id="price" name="price" step="0.01" required>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
<label for="description">Описание:</label>
|
| 158 |
+
<textarea id="description" name="description" rows="4" required></textarea>
|
| 159 |
+
|
| 160 |
+
<button type="submit">Добавить товар</button>
|
| 161 |
+
</form>
|
| 162 |
|
| 163 |
+
<div class="product-list">
|
| 164 |
+
<h2>Список товаров</h2>
|
| 165 |
+
{% for product in products %}
|
| 166 |
+
<div class="product-item">
|
| 167 |
+
<strong>{{ product['name'] }}</strong> - {{ product['price'] }} руб.
|
| 168 |
+
<p>{{ product['description'] }}</p>
|
| 169 |
+
</div>
|
| 170 |
+
{% endfor %}
|
| 171 |
+
</div>
|
| 172 |
+
</body>
|
| 173 |
+
</html>
|
| 174 |
+
'''
|
| 175 |
+
return render_template_string(admin_html, products=products)
|
| 176 |
+
|
| 177 |
+
if __name__ == '__main__':
|
| 178 |
+
app.run(host='0.0.0.0', port=7860)
|