Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -1,33 +1,20 @@ | |
| 1 | 
            -
            import flask
         | 
| 2 | 
            -
            from flask import request, jsonify, send_from_directory, render_template_string
         | 
| 3 | 
             
            import os
         | 
|  | |
| 4 |  | 
| 5 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
| 6 |  | 
| 7 | 
             
            # Создание директории, если она не существует
         | 
| 8 | 
             
            if not os.path.exists(UPLOAD_FOLDER):
         | 
| 9 | 
             
                os.makedirs(UPLOAD_FOLDER)
         | 
| 10 |  | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
            app = flask.Flask(__name__, template_folder="./")
         | 
| 15 | 
            -
            app.config['DEBUG'] = True
         | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
             
            @app.route('/online', methods=['GET'])
         | 
| 20 | 
             
            def online():
         | 
| 21 | 
             
                return render_template('online.html')
         | 
| 22 |  | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
            UPLOAD_FOLDER = 'static'
         | 
| 27 | 
            -
            IMAGE_FILENAME = 'latest_image.jpg'
         | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
             
            @app.route('/upload', methods=['POST'])
         | 
| 32 | 
             
            def upload_file():
         | 
| 33 | 
             
                if 'photo' not in request.files:
         | 
| @@ -35,12 +22,10 @@ def upload_file(): | |
| 35 | 
             
                file = request.files['photo']
         | 
| 36 | 
             
                if file.filename == '':
         | 
| 37 | 
             
                    return "No selected file", 400
         | 
| 38 | 
            -
                save_path = os.path.join(UPLOAD_FOLDER,  | 
| 39 | 
             
                file.save(save_path)
         | 
| 40 | 
             
                return f"File uploaded successfully and saved to {save_path}", 200
         | 
| 41 |  | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
             
            @app.route('/image', methods=['GET'])
         | 
| 45 | 
             
            def get_image():
         | 
| 46 | 
             
                return send_from_directory(UPLOAD_FOLDER, IMAGE_FILENAME)
         | 
| @@ -56,9 +41,36 @@ def index(): | |
| 56 | 
             
                    <title>Camera Image</title>
         | 
| 57 | 
             
                </head>
         | 
| 58 | 
             
                <body>
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 59 | 
             
                    <h1>Latest Image</h1>
         | 
| 60 | 
             
                    <img id="cameraImage" src="/image" alt="Image" style="width:100%;">
         | 
| 61 | 
             
                    <script>
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 62 | 
             
                        setInterval(function(){
         | 
| 63 | 
             
                            var image = document.getElementById("cameraImage");
         | 
| 64 | 
             
                            image.src = "/image?" + new Date().getTime();
         | 
| @@ -69,10 +81,5 @@ def index(): | |
| 69 | 
             
                '''
         | 
| 70 | 
             
                return render_template_string(html)
         | 
| 71 |  | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
             
            if __name__ == '__main__':
         | 
| 78 | 
            -
                app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
         | 
|  | |
|  | |
|  | |
| 1 | 
             
            import os
         | 
| 2 | 
            +
            from flask import Flask, request, send_from_directory, render_template_string, render_template
         | 
| 3 |  | 
| 4 | 
            +
            app = Flask(__name__, template_folder="./")
         | 
| 5 | 
            +
            app.config['DEBUG'] = True
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            UPLOAD_FOLDER = 'static'
         | 
| 8 | 
            +
            IMAGE_FILENAME = 'latest_image.jpg'
         | 
| 9 |  | 
| 10 | 
             
            # Создание директории, если она не существует
         | 
| 11 | 
             
            if not os.path.exists(UPLOAD_FOLDER):
         | 
| 12 | 
             
                os.makedirs(UPLOAD_FOLDER)
         | 
| 13 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 14 | 
             
            @app.route('/online', methods=['GET'])
         | 
| 15 | 
             
            def online():
         | 
| 16 | 
             
                return render_template('online.html')
         | 
| 17 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 18 | 
             
            @app.route('/upload', methods=['POST'])
         | 
| 19 | 
             
            def upload_file():
         | 
| 20 | 
             
                if 'photo' not in request.files:
         | 
|  | |
| 22 | 
             
                file = request.files['photo']
         | 
| 23 | 
             
                if file.filename == '':
         | 
| 24 | 
             
                    return "No selected file", 400
         | 
| 25 | 
            +
                save_path = os.path.join(UPLOAD_FOLDER, IMAGE_FILENAME)
         | 
| 26 | 
             
                file.save(save_path)
         | 
| 27 | 
             
                return f"File uploaded successfully and saved to {save_path}", 200
         | 
| 28 |  | 
|  | |
|  | |
| 29 | 
             
            @app.route('/image', methods=['GET'])
         | 
| 30 | 
             
            def get_image():
         | 
| 31 | 
             
                return send_from_directory(UPLOAD_FOLDER, IMAGE_FILENAME)
         | 
|  | |
| 41 | 
             
                    <title>Camera Image</title>
         | 
| 42 | 
             
                </head>
         | 
| 43 | 
             
                <body>
         | 
| 44 | 
            +
                    <h1>Upload Image</h1>
         | 
| 45 | 
            +
                    <form id="uploadForm" enctype="multipart/form-data" method="post" action="/upload">
         | 
| 46 | 
            +
                        <input type="file" name="photo">
         | 
| 47 | 
            +
                        <button type="submit">Upload</button>
         | 
| 48 | 
            +
                    </form>
         | 
| 49 | 
            +
                    <div id="message"></div>
         | 
| 50 | 
             
                    <h1>Latest Image</h1>
         | 
| 51 | 
             
                    <img id="cameraImage" src="/image" alt="Image" style="width:100%;">
         | 
| 52 | 
             
                    <script>
         | 
| 53 | 
            +
                        document.getElementById('uploadForm').addEventListener('submit', function(event) {
         | 
| 54 | 
            +
                            event.preventDefault();
         | 
| 55 | 
            +
                            var formData = new FormData(this);
         | 
| 56 | 
            +
                            fetch('/upload', {
         | 
| 57 | 
            +
                                method: 'POST',
         | 
| 58 | 
            +
                                body: formData
         | 
| 59 | 
            +
                            })
         | 
| 60 | 
            +
                            .then(response => {
         | 
| 61 | 
            +
                                if (response.ok) {
         | 
| 62 | 
            +
                                    return response.text();
         | 
| 63 | 
            +
                                }
         | 
| 64 | 
            +
                                throw new Error('Network response was not ok.');
         | 
| 65 | 
            +
                            })
         | 
| 66 | 
            +
                            .then(data => {
         | 
| 67 | 
            +
                                document.getElementById('message').innerText = data;
         | 
| 68 | 
            +
                            })
         | 
| 69 | 
            +
                            .catch(error => {
         | 
| 70 | 
            +
                                document.getElementById('message').innerText = 'Error: ' + error.message;
         | 
| 71 | 
            +
                            });
         | 
| 72 | 
            +
                        });
         | 
| 73 | 
            +
             | 
| 74 | 
             
                        setInterval(function(){
         | 
| 75 | 
             
                            var image = document.getElementById("cameraImage");
         | 
| 76 | 
             
                            image.src = "/image?" + new Date().getTime();
         | 
|  | |
| 81 | 
             
                '''
         | 
| 82 | 
             
                return render_template_string(html)
         | 
| 83 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 84 | 
             
            if __name__ == '__main__':
         | 
| 85 | 
            +
                app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
         |