Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template_string, request, redirect, url_for | |
| import os | |
| from werkzeug.utils import secure_filename | |
| app = Flask(__name__) | |
| app.config['UPLOAD_FOLDER'] = '.' # Используем текущую директорию | |
| os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True) | |
| stories = [] | |
| HTML = """ | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Мобильная Веб-страница Историй</title> | |
| <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> | |
| <style> | |
| body { | |
| font-family: 'Arial', sans-serif; | |
| background-color: #f8f9fa; | |
| margin: 0; | |
| padding: 0; | |
| display: flex; | |
| justify-content: center; | |
| align-items: center; | |
| min-height: 100vh; | |
| } | |
| .container { | |
| background-color: #fff; | |
| padding: 20px; | |
| border-radius: 8px; | |
| box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | |
| width: 90%; | |
| max-width: 600px; | |
| } | |
| .story-list { | |
| display: flex; | |
| overflow-x: auto; | |
| padding-bottom: 10px; | |
| margin-bottom: 20px; | |
| } | |
| .story-item { | |
| flex: 0 0 auto; | |
| width: 100px; | |
| height: 100px; | |
| margin-right: 10px; | |
| border-radius: 50%; | |
| overflow: hidden; | |
| border: 2px solid #007bff; | |
| } | |
| .story-item img { | |
| width: 100%; | |
| height: 100%; | |
| object-fit: cover; | |
| } | |
| #addStoryButton { | |
| margin-top: 10px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <h1>Истории</h1> | |
| <div class="story-list"> | |
| {% for story in stories %} | |
| <div class="story-item"> | |
| <img src="{{ story.image }}" alt="История"> | |
| </div> | |
| {% endfor %} | |
| </div> | |
| <form action="/add_story" method="post" enctype="multipart/form-data"> | |
| <div class="form-group"> | |
| <input type="file" class="form-control-file" id="photo" name="photo" accept="image/*"> | |
| </div> | |
| <button type="submit" class="btn btn-primary" id="addStoryButton">Добавить историю</button> | |
| </form> | |
| </div> | |
| <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.3/dist/umd/popper.min.js"></script> | |
| <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> | |
| </body> | |
| </html> | |
| """ | |
| def index(): | |
| return render_template_string(HTML, stories=stories) | |
| def add_story(): | |
| if 'photo' in request.files: | |
| photo = request.files['photo'] | |
| if photo.filename != '': | |
| filename = secure_filename(photo.filename) | |
| photo.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
| stories.insert(0, {'image': filename}) # Сохраняем только имя файла | |
| return redirect(url_for('index')) | |
| if __name__ == '__main__': | |
| app.run(debug=True,host="0.0.0.0" , port=7860) | |