from flask import Flask, render_template_string
import os
import re
import json
app = Flask(__name__)
HTML_TEMPLATE = '''
Live Linescan Images
{% for image_info in images %}{% endfor %}
'''
def extract_timestamp(filename):
"""Extract timestamp from filename like 'live-1763227690-...'"""
match = re.match(r'live-(\d+)-', filename)
if match:
return int(match.group(1))
return 0
def extract_length(filename):
"""Extract length from filename like 'live-timestamp-start-end-length.ext'"""
match = re.match(r'live-\d+-\d+-\d+-(\d+)\.', filename)
if match:
return int(match.group(1))
return None
@app.route('/')
def index():
output_dir = os.path.join(os.path.dirname(__file__), 'output')
# Load metadata from jsonl file
metadata_map = {}
metadata_path = os.path.join(output_dir, 'metadata.jsonl')
if os.path.exists(metadata_path):
with open(metadata_path, 'r') as f:
for line in f:
if line.strip():
data = json.loads(line)
metadata_map[data['filename']] = data
# Get all image files
image_files = []
if os.path.exists(output_dir):
image_files = [f for f in os.listdir(output_dir)
if f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp', '.gif','.avif'))]
# Sort by timestamp in filename, reverse for latest first
image_files.sort(key=extract_timestamp, reverse=True)
# Create list with name, width, and gradient info
images = []
for img in image_files:
metadata = metadata_map.get(img, {})
width = metadata.get('length') or extract_length(img) or 1
gradient = metadata.get('gradient', 'none')
images.append({
'name': img,
'width': width,
'height': 2456, # Fixed height for aspect ratio hint
'gradient': gradient
})
return render_template_string(HTML_TEMPLATE, images=images)
@app.route('/output/')
def serve_image(filename):
"""Serve images from the output folder"""
from flask import send_from_directory
output_dir = os.path.join(os.path.dirname(__file__), 'output')
return send_from_directory(output_dir, filename)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=10104)