onscreen-translator/web_app.py

37 lines
1.0 KiB
Python

from flask import Flask, Response, render_template
import threading
import io
app = Flask(__name__)
latest_image = None
# Global variable to hold the current image
def curr_image():
return latest_image
@app.route('/')
def index():
return render_template('index.html')
@app.route('/image')
def stream_image():
if curr_image() is None:
return "No image generated yet.", 503
file_object = io.BytesIO()
curr_image().save(file_object, 'PNG')
file_object.seek(0)
response = Response(file_object.getvalue(), mimetype='image/png')
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' # HTTP 1.1
response.headers['Pragma'] = 'no-cache' # HTTP 1.0
response.headers['Expires'] = '0' # Proxies
return response
if __name__ == '__main__':
# Start the image updating thread
import main, asyncio
threading.Thread(target=asyncio.run, args=(main(),), daemon=True).start()
# Start the Flask web server
app.run(host='0.0.0.0', port=5000, debug=True)