Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ from pygments import highlight
|
|
10 |
from pygments.lexers import get_lexer_by_name
|
11 |
from pygments.formatters import HtmlFormatter
|
12 |
from pygments.styles import get_all_styles
|
|
|
13 |
|
14 |
app = Flask(__name__)
|
15 |
|
@@ -126,29 +127,43 @@ def build_full_html(markdown_text, styles, include_fontawesome):
|
|
126 |
@app.route('/convert', methods=['POST'])
|
127 |
def convert_endpoint():
|
128 |
data = request.json
|
|
|
129 |
try:
|
130 |
full_html = build_full_html(
|
131 |
markdown_text=data.get('markdown_text', ''),
|
132 |
styles=data.get('styles', {}),
|
133 |
include_fontawesome=data.get('include_fontawesome', False)
|
134 |
)
|
135 |
-
|
136 |
-
options
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
if data.get('download', False):
|
139 |
download_type = data.get('download_type', 'png')
|
140 |
if download_type == 'html':
|
141 |
return send_file(BytesIO(full_html.encode("utf-8")), as_attachment=True, download_name="output.html", mimetype="text/html")
|
142 |
else:
|
143 |
-
|
|
|
144 |
return send_file(BytesIO(png_bytes), as_attachment=True, download_name="output.png", mimetype="image/png")
|
145 |
else:
|
146 |
-
|
|
|
147 |
png_base64 = base64.b64encode(png_bytes).decode('utf-8')
|
148 |
return jsonify({'preview_html': full_html, 'preview_png_base64': png_base64})
|
|
|
149 |
except Exception as e:
|
150 |
traceback.print_exc()
|
151 |
return jsonify({'error': f'Failed to convert content: {str(e)}'}), 500
|
|
|
|
|
|
|
|
|
152 |
|
153 |
# --- MAIN PAGE RENDERER (with corrected CSS) ---
|
154 |
@app.route('/')
|
@@ -305,4 +320,4 @@ def index():
|
|
305 |
if __name__ == "__main__":
|
306 |
# Ensure you have installed the required libraries:
|
307 |
# pip install Flask markdown imgkit pygments
|
308 |
-
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
10 |
from pygments.lexers import get_lexer_by_name
|
11 |
from pygments.formatters import HtmlFormatter
|
12 |
from pygments.styles import get_all_styles
|
13 |
+
import uuid # <--- REQUIRED CHANGE 1: Import uuid
|
14 |
|
15 |
app = Flask(__name__)
|
16 |
|
|
|
127 |
@app.route('/convert', methods=['POST'])
|
128 |
def convert_endpoint():
|
129 |
data = request.json
|
130 |
+
temp_html_path = None # Initialize path
|
131 |
try:
|
132 |
full_html = build_full_html(
|
133 |
markdown_text=data.get('markdown_text', ''),
|
134 |
styles=data.get('styles', {}),
|
135 |
include_fontawesome=data.get('include_fontawesome', False)
|
136 |
)
|
137 |
+
|
138 |
+
# Define options for wkhtmltoimage, removing the unsupported --no-cache
|
139 |
+
options = {"quiet": "", 'encoding': "UTF-8"}
|
140 |
+
|
141 |
+
# --- REQUIRED CHANGE 2: Manually create and use a temporary file ---
|
142 |
+
temp_html_path = os.path.join(TEMP_DIR, f"{uuid.uuid4()}.html")
|
143 |
+
with open(temp_html_path, "w", encoding="utf-8") as f:
|
144 |
+
f.write(full_html)
|
145 |
|
146 |
if data.get('download', False):
|
147 |
download_type = data.get('download_type', 'png')
|
148 |
if download_type == 'html':
|
149 |
return send_file(BytesIO(full_html.encode("utf-8")), as_attachment=True, download_name="output.html", mimetype="text/html")
|
150 |
else:
|
151 |
+
# Use from_file instead of from_string
|
152 |
+
png_bytes = imgkit.from_file(temp_html_path, False, options=options)
|
153 |
return send_file(BytesIO(png_bytes), as_attachment=True, download_name="output.png", mimetype="image/png")
|
154 |
else:
|
155 |
+
# Use from_file instead of from_string
|
156 |
+
png_bytes = imgkit.from_file(temp_html_path, False, options=options)
|
157 |
png_base64 = base64.b64encode(png_bytes).decode('utf-8')
|
158 |
return jsonify({'preview_html': full_html, 'preview_png_base64': png_base64})
|
159 |
+
|
160 |
except Exception as e:
|
161 |
traceback.print_exc()
|
162 |
return jsonify({'error': f'Failed to convert content: {str(e)}'}), 500
|
163 |
+
finally:
|
164 |
+
# --- REQUIRED CHANGE 3: Clean up the temporary file ---
|
165 |
+
if temp_html_path and os.path.exists(temp_html_path):
|
166 |
+
os.remove(temp_html_path)
|
167 |
|
168 |
# --- MAIN PAGE RENDERER (with corrected CSS) ---
|
169 |
@app.route('/')
|
|
|
320 |
if __name__ == "__main__":
|
321 |
# Ensure you have installed the required libraries:
|
322 |
# pip install Flask markdown imgkit pygments
|
323 |
+
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|