from flask import Flask, request, jsonify, send_file, render_template_string from flask_cors import CORS import os import subprocess import tempfile import shutil from pathlib import Path app = Flask(__name__) CORS(app) def read_file(filepath): """Read index.html content""" try: with open(filepath, 'r', encoding='utf-8') as f: return f.read() except FileNotFoundError: return "
Please ensure index.html is in the same directory as app.py
" @app.route('/') def index(): """Serve the frontend HTML""" html_content = read_file('index.html') return render_template_string(html_content) @app.route('/docs') def docs(): """API documentation""" docs_html = '''Compile LaTeX code to PDF
Content-Type: application/pdf PDF file binary data
{
"error": "Compilation failed",
"log": "LaTeX error log details..."
}
Serve the web frontend interface
Display this API documentation
curl -X POST http://localhost:7860/compile \\
-H "Content-Type: application/json" \\
-d '{"code": "\\\\documentclass{article}\\\\begin{document}Hello\\\\end{document}"}' \\
--output output.pdf
curl -X POST http://localhost:7860/compile \\ -F "file=@document.tex" \\ --output output.pdf
import requests
latex_code = r"""
\\documentclass{article}
\\begin{document}
Hello from Python!
\\end{document}
"""
response = requests.post(
'http://localhost:7860/compile',
json={'code': latex_code}
)
if response.status_code == 200:
with open('output.pdf', 'wb') as f:
f.write(response.content)
else:
print(response.json())