Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,6 @@ 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 |
-
import uuid # <--- REQUIRED CHANGE 1: Import uuid
|
14 |
|
15 |
app = Flask(__name__)
|
16 |
|
@@ -127,43 +126,29 @@ def build_full_html(markdown_text, styles, include_fontawesome):
|
|
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 |
-
|
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 |
-
|
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 |
-
|
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('/')
|
|
|
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 |
@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 |
+
# Define options here to avoid repetition and add the required fix
|
136 |
+
options = {"quiet": "", 'encoding': "UTF-8", "--no-cache": ""}
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
png_bytes = imgkit.from_string(full_html, False, options=options)
|
|
|
144 |
return send_file(BytesIO(png_bytes), as_attachment=True, download_name="output.png", mimetype="image/png")
|
145 |
else:
|
146 |
+
png_bytes = imgkit.from_string(full_html, False, options=options)
|
|
|
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('/')
|