flpolprojects commited on
Commit
daa7bba
·
verified ·
1 Parent(s): 4051dff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -77
app.py CHANGED
@@ -52,7 +52,6 @@ def upload_db_to_hf():
52
  api.upload_file(
53
  path_or_fileobj=DATA_FILE,
54
  path_in_repo=DATA_FILE,
55
- repo_id=REPO_ID,
56
  repo_type="dataset",
57
  token=HF_TOKEN_WRITE,
58
  commit_message=f"Автоматическое резервное копирование базы данных {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
@@ -94,6 +93,7 @@ def catalog():
94
  <meta charset="UTF-8">
95
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
96
  <title>Каталог</title>
 
97
  <style>
98
  * {
99
  margin: 0;
@@ -245,6 +245,58 @@ def catalog():
245
  ::-webkit-scrollbar-thumb:hover {
246
  background: #555;
247
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  </style>
249
  </head>
250
  <body>
@@ -252,24 +304,64 @@ def catalog():
252
  <h1>Каталог товаров</h1>
253
  <div class="products-grid">
254
  {% for product in products %}
255
- <a href="{{ url_for('product_detail', index=loop.index0) }}" style="text-decoration: none; color: inherit;">
256
- <div class="product">
257
- {% if product.get('photos') and product['photos']|length > 0 %}
258
- <div class="product-image">
259
- <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/photos/{{ product['photos'][0] }}"
260
- alt="{{ product['name'] }}"
261
- loading="lazy">
262
- </div>
263
- {% endif %}
264
- <h2>{{ product['name'] }}</h2>
265
- <div class="product-price">{{ product['price'] }} ₽</div>
266
- <p class="product-description">{{ product['description'][:100] }}{% if product['description']|length > 100 %}...{% endif %}</p>
267
- <div class="product-button">Подробнее</div>
268
  </div>
269
- </a>
 
 
 
 
 
270
  {% endfor %}
271
  </div>
272
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
  </body>
274
  </html>
275
  '''
@@ -283,73 +375,41 @@ def product_detail(index):
283
  except IndexError:
284
  return "Продукт не найден", 404
285
  detail_html = '''
286
- <!DOCTYPE html>
287
- <html lang="ru">
288
- <head>
289
- <meta charset="UTF-8">
290
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
291
- <title>{{ product['name'] }}</title>
292
- <style>
293
- body {
294
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
295
- background-color: #f5f5f5;
296
- color: #333;
297
- padding: 20px;
298
- }
299
- .container {
300
- max-width: 800px;
301
- margin: 0 auto;
302
- background: #fff;
303
- padding: 20px;
304
- border-radius: 8px;
305
- box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
306
- }
307
- h1 {
308
- margin-bottom: 20px;
309
- }
310
- .photos {
311
- display: flex;
312
- flex-wrap: wrap;
313
- gap: 10px;
314
- margin-bottom: 20px;
315
- }
316
- .photos img {
317
- width: calc(50% - 10px);
318
- height: auto;
319
- border-radius: 5px;
320
- }
321
- @media (max-width: 600px) {
322
- .photos img {
323
- width: 100%;
324
- }
325
- }
326
- .back-button {
327
- display: inline-block;
328
- margin-top: 20px;
329
- padding: 10px 15px;
330
- background-color: #3498db;
331
- color: white;
332
- text-decoration: none;
333
- border-radius: 5px;
334
- }
335
- </style>
336
- </head>
337
- <body>
338
- <div class="container">
339
- <h1>{{ product['name'] }}</h1>
340
- <div class="photos">
341
  {% if product.get('photos') %}
342
  {% for photo in product['photos'] %}
343
- <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/photos/{{ photo }}" alt="{{ product['name'] }}">
344
  {% endfor %}
345
  {% endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
346
  </div>
347
- <p><strong>Цена:</strong> {{ product['price'] }} ₽</p>
348
- <p><strong>Описание:</strong> {{ product['description'] }}</p>
349
- <a href="{{ url_for('catalog') }}" class="back-button">Назад к каталогу</a>
 
 
 
 
 
350
  </div>
351
- </body>
352
- </html>
 
353
  '''
354
  return render_template_string(detail_html, product=product, repo_id=REPO_ID)
355
 
@@ -624,4 +684,4 @@ if __name__ == '__main__':
624
  except Exception as e:
625
  logging.error(f"Не удалось загрузить базу данных при запуске: {e}")
626
 
627
- app.run(debug=True, host='0.0.0.0', port=7860)
 
52
  api.upload_file(
53
  path_or_fileobj=DATA_FILE,
54
  path_in_repo=DATA_FILE,
 
55
  repo_type="dataset",
56
  token=HF_TOKEN_WRITE,
57
  commit_message=f"Автоматическое резервное копирование базы данных {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
 
93
  <meta charset="UTF-8">
94
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
95
  <title>Каталог</title>
96
+ <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
97
  <style>
98
  * {
99
  margin: 0;
 
245
  ::-webkit-scrollbar-thumb:hover {
246
  background: #555;
247
  }
248
+
249
+ /* Modal Styles */
250
+ .modal {
251
+ display: none; /* Hidden by default */
252
+ position: fixed; /* Stay in place */
253
+ z-index: 1; /* Sit on top */
254
+ left: 0;
255
+ top: 0;
256
+ width: 100%; /* Full width */
257
+ height: 100%; /* Full height */
258
+ overflow: auto; /* Enable scroll if needed */
259
+ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
260
+ }
261
+
262
+ .modal-content {
263
+ position: relative;
264
+ background-color: #fefefe;
265
+ margin: 10% auto; /* 15% from the top and centered */
266
+ padding: 20px;
267
+ border: 1px solid #888;
268
+ width: 80%;
269
+ max-width: 600px;
270
+ box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
271
+ animation-name: animatetop;
272
+ animation-duration: 0.4s
273
+ }
274
+
275
+ /* Add Animation */
276
+ @keyframes animatetop {
277
+ from {top: -300px; opacity: 0}
278
+ to {top: 10%; opacity: 1}
279
+ }
280
+
281
+ /* The Close Button */
282
+ .close {
283
+ color: #aaa;
284
+ float: right;
285
+ font-size: 28px;
286
+ font-weight: bold;
287
+ }
288
+
289
+ .close:hover,
290
+ .close:focus {
291
+ color: black;
292
+ text-decoration: none;
293
+ cursor: pointer;
294
+ }
295
+
296
+ /* Image Carousel Styles */
297
+ .carousel-inner > .carousel-item > img {
298
+ margin: 0 auto;
299
+ }
300
  </style>
301
  </head>
302
  <body>
 
304
  <h1>Каталог товаров</h1>
305
  <div class="products-grid">
306
  {% for product in products %}
307
+ <div class="product">
308
+ {% if product.get('photos') and product['photos']|length > 0 %}
309
+ <div class="product-image">
310
+ <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/photos/{{ product['photos'][0] }}"
311
+ alt="{{ product['name'] }}"
312
+ loading="lazy">
 
 
 
 
 
 
 
313
  </div>
314
+ {% endif %}
315
+ <h2>{{ product['name'] }}</h2>
316
+ <div class="product-price">{{ product['price'] }} ₽</div>
317
+ <p class="product-description">{{ product['description'][:100] }}{% if product['description']|length > 100 %}...{% endif %}</p>
318
+ <button class="product-button" onclick="openModal({{ loop.index0 }})">Подробнее</button>
319
+ </div>
320
  {% endfor %}
321
  </div>
322
  </div>
323
+
324
+ <!-- The Modal -->
325
+ <div id="productModal" class="modal">
326
+ <div class="modal-content">
327
+ <span class="close" onclick="closeModal()">&times;</span>
328
+ <div id="modalContent">
329
+ <!-- Product details will be loaded here -->
330
+ </div>
331
+ </div>
332
+ </div>
333
+
334
+ <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
335
+ <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.3/dist/umd/popper.min.js"></script>
336
+ <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
337
+ <script>
338
+ // Function to open the modal
339
+ function openModal(index) {
340
+ loadProductDetails(index);
341
+ document.getElementById('productModal').style.display = "block";
342
+ }
343
+
344
+ // Function to close the modal
345
+ function closeModal() {
346
+ document.getElementById('productModal').style.display = "none";
347
+ }
348
+
349
+ // Function to load product details into the modal
350
+ function loadProductDetails(index) {
351
+ fetch('/product/' + index)
352
+ .then(response => response.text())
353
+ .then(data => {
354
+ document.getElementById('modalContent').innerHTML = data;
355
+ });
356
+ }
357
+
358
+ // Close the modal if the user clicks outside of it
359
+ window.onclick = function(event) {
360
+ if (event.target == document.getElementById('productModal')) {
361
+ closeModal();
362
+ }
363
+ }
364
+ </script>
365
  </body>
366
  </html>
367
  '''
 
375
  except IndexError:
376
  return "Продукт не найден", 404
377
  detail_html = '''
378
+ <div class="container">
379
+ <h2>{{ product['name'] }}</h2>
380
+ <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
381
+ <ol class="carousel-indicators">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
382
  {% if product.get('photos') %}
383
  {% for photo in product['photos'] %}
384
+ <li data-target="#carouselExampleIndicators" data-slide-to="{{ loop.index0 }}" {% if loop.first %}class="active"{% endif %}></li>
385
  {% endfor %}
386
  {% endif %}
387
+ </ol>
388
+ <div class="carousel-inner">
389
+ {% if product.get('photos') %}
390
+ {% for photo in product['photos'] %}
391
+ <div class="carousel-item {% if loop.first %}active{% endif %}">
392
+ <img class="d-block w-100" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/photos/{{ photo }}" alt="{{ product['name'] }}">
393
+ </div>
394
+ {% endfor %}
395
+ {% else %}
396
+ <div class="carousel-item active">
397
+ <img class="d-block w-100" src="https://via.placeholder.com/300" alt="No Image">
398
+ </div>
399
+ {% endif %}
400
  </div>
401
+ <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
402
+ <span class="carousel-control-prev-icon" aria-hidden="true"></span>
403
+ <span class="sr-only">Previous</span>
404
+ </a>
405
+ <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
406
+ <span class="carousel-control-next-icon" aria-hidden="true"></span>
407
+ <span class="sr-only">Next</span>
408
+ </a>
409
  </div>
410
+ <p><strong>Цена:</strong> {{ product['price'] }} ₽</p>
411
+ <p><strong>Описание:</strong> {{ product['description'] }}</p>
412
+ </div>
413
  '''
414
  return render_template_string(detail_html, product=product, repo_id=REPO_ID)
415
 
 
684
  except Exception as e:
685
  logging.error(f"Не удалось загрузить базу данных при запуске: {e}")
686
 
687
+ app.run(debug=True, host='0.0.0.0', port=7860)