Add export to latex button (i think)
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
"""
|
||||
Simple Flask API for PDF Export
|
||||
|
||||
This provides a web API endpoint for the gallery export functionality.
|
||||
"""
|
||||
|
||||
from flask import Flask, request, jsonify, send_file
|
||||
from pathlib import Path
|
||||
import json
|
||||
import tempfile
|
||||
import os
|
||||
from orchestration.pdf_export import PDFExporter, check_dependencies
|
||||
|
||||
app = Flask(__name__)
|
||||
exporter = PDFExporter()
|
||||
|
||||
|
||||
@app.route('/api/export-pdf', methods=['POST'])
|
||||
def export_pdf():
|
||||
"""Export selected plots to merged PDF"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
|
||||
if not data or 'plots' not in data:
|
||||
return jsonify({'error': 'No plots specified'}), 400
|
||||
|
||||
plot_urls = data['plots']
|
||||
layout = data.get('layout', {'rows': 2, 'cols': 2})
|
||||
|
||||
# Convert URLs to file paths
|
||||
plot_paths = []
|
||||
for url in plot_urls:
|
||||
# Extract path from file:// URL or relative path
|
||||
if url.startswith('file://'):
|
||||
path = url[7:] # Remove 'file://' prefix
|
||||
elif url.startswith('http'):
|
||||
return jsonify({'error': 'Remote URLs not supported'}), 400
|
||||
else:
|
||||
# Assume relative path from web root
|
||||
path = url
|
||||
|
||||
# Resolve to absolute path
|
||||
plot_path = Path(path)
|
||||
if not plot_path.exists():
|
||||
return jsonify({'error': f'Plot not found: {path}'}), 404
|
||||
|
||||
plot_paths.append(str(plot_path))
|
||||
|
||||
if not plot_paths:
|
||||
return jsonify({'error': 'No valid plots found'}), 400
|
||||
|
||||
# Create merged PDF
|
||||
pdf_bytes = exporter.merge_plots(plot_paths, layout)
|
||||
|
||||
# Create temporary file to serve
|
||||
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp_file:
|
||||
tmp_file.write(pdf_bytes)
|
||||
tmp_path = tmp_file.name
|
||||
|
||||
def cleanup_temp_file():
|
||||
"""Clean up temporary file after sending"""
|
||||
try:
|
||||
os.unlink(tmp_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
return send_file(
|
||||
tmp_path,
|
||||
as_attachment=True,
|
||||
download_name='merged_plots.pdf',
|
||||
mimetype='application/pdf'
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@app.route('/api/export-status', methods=['GET'])
|
||||
def export_status():
|
||||
"""Check export capabilities"""
|
||||
deps = check_dependencies()
|
||||
return jsonify({
|
||||
'available': any(deps.values()),
|
||||
'dependencies': deps,
|
||||
'methods': {
|
||||
'pypdf2': deps['pypdf2'],
|
||||
'pdfjam': deps['pdfjam']
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# For development only
|
||||
app.run(debug=True, port=5000)
|
||||
Reference in New Issue
Block a user