Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"permissions": {
"allow": [
"Bash(npm run dev:*)",
"Bash(curl:*)",
"Bash(taskkill:*)",
"Bash(rm:*)",
"Bash(true)",
"Bash(npm run build:*)",
"Bash(python test_grid_exporter.py)"
],
"deny": []
}
}
1 change: 1 addition & 0 deletions comfy-easy-grids
Submodule comfy-easy-grids added at 376a8a
96 changes: 96 additions & 0 deletions dream_layer_backend/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tempfile
import shutil
from dream_layer import get_directories
from grid_exporter import LabeledGridExporter

# Create Flask app
app = Flask(__name__)
Expand Down Expand Up @@ -311,6 +312,101 @@ def upscale_image():
"message": str(e)
}), 500

@app.route('/api/extras/grid-export', methods=['POST'])
def create_grid():
"""Handle grid export request"""
try:
# Get parameters from request
data = request.get_json()
if not data:
return jsonify({
"status": "error",
"message": "No parameters provided"
}), 400

# Extract parameters
count = data.get('count', 4)
grid_size_str = data.get('grid_size', 'auto')
filename = data.get('filename', f'labeled_grid_{int(time.time())}.png')
show_labels = data.get('show_labels', True)
show_filenames = data.get('show_filenames', False)

# Parse grid size
if grid_size_str == 'auto':
grid_size = None
else:
try:
cols, rows = grid_size_str.split('x')
grid_size = (int(cols), int(rows))
except:
grid_size = None

Comment on lines +338 to +343
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Replace bare except with a specific exception

Catching all exceptions swallows programming errors and makes debugging harder. Catch ValueError (and optionally TypeError) that int() / split() may raise instead.

-    try:
-        cols, rows = grid_size_str.split('x')
-        grid_size = (int(cols), int(rows))
-    except:
-        grid_size = None
+    try:
+        cols, rows = grid_size_str.split('x')
+        grid_size = (int(cols), int(rows))
+    except (ValueError, TypeError):
+        grid_size = None
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try:
cols, rows = grid_size_str.split('x')
grid_size = (int(cols), int(rows))
except:
grid_size = None
try:
cols, rows = grid_size_str.split('x')
grid_size = (int(cols), int(rows))
except (ValueError, TypeError):
grid_size = None
🧰 Tools
🪛 Ruff (0.12.2)

341-341: Do not use bare except

(E722)

🤖 Prompt for AI Agents
In dream_layer_backend/extras.py around lines 338 to 343, replace the bare
except clause with a specific exception handler. Catch ValueError and optionally
TypeError exceptions that can be raised by the split or int conversion
operations. This will prevent swallowing unrelated exceptions and improve
debugging by only handling expected conversion errors.

print(f"[API] Grid export request:")
print(f" Count: {count}")
print(f" Grid size: {grid_size}")
print(f" Filename: {filename}")
print(f" Show labels: {show_labels}")
print(f" Show filenames: {show_filenames}")

# Create grid exporter
exporter = LabeledGridExporter()

# Create the grid
output_path = exporter.create_grid_from_recent(
count=count,
grid_size=grid_size,
filename=filename
)

# Copy to served images directory for frontend access
served_filename = f"grid_{int(time.time())}.png"
served_path = os.path.join(SERVED_IMAGES_DIR, served_filename)
shutil.copy2(output_path, served_path)

# Get file info
file_size = os.path.getsize(output_path)

# Get grid info
from PIL import Image
with Image.open(output_path) as img:
width, height = img.size

print(f"[API] Grid created successfully:")
print(f" Output path: {output_path}")
print(f" Served path: {served_path}")
print(f" Size: {file_size:,} bytes")
print(f" Dimensions: {width}x{height}")

return jsonify({
"status": "success",
"data": {
"grid_url": f"{SERVER_URL}/images/{served_filename}",
"filename": filename,
"file_size": file_size,
"dimensions": {
"width": width,
"height": height
},
"images_processed": count
}
})

except Exception as e:
print(f"[API] Error creating grid: {str(e)}")
return jsonify({
"status": "error",
"message": str(e)
}), 500

@app.route('/images/<filename>')
def serve_image(filename):
"""Serve images from the served_images directory"""
try:
return send_from_directory(SERVED_IMAGES_DIR, filename)
except Exception as e:
print(f"[API] Error serving image {filename}: {str(e)}")
return jsonify({"error": "Image not found"}), 404

# This endpoint is now handled by dream_layer.py

def start_extras_server():
Expand Down
Loading