Spaces:
Sleeping
Sleeping
from flask import Flask, request, render_template_string, send_file | |
import markdown | |
import imgkit | |
import os | |
import traceback | |
from io import BytesIO | |
app = Flask(__name__) | |
# Use a directory within the app's working directory to avoid permission issues | |
TEMP_DIR = os.path.join(os.getcwd(), "temp") | |
# Create temporary directory if it doesn't exist | |
try: | |
os.makedirs(TEMP_DIR, exist_ok=True) | |
except Exception as e: | |
print(f"Error creating temp directory: {e}") | |
def index(): | |
preview_html = None | |
download_available = False | |
download_type = "png" | |
error_message = None | |
markdown_text = request.form.get("markdown_text", "") if request.method == "POST" else "" | |
if request.method == "POST" and markdown_text: | |
try: | |
# Convert Markdown to HTML | |
html_content = markdown.markdown(markdown_text, extensions=['fenced_code', 'tables']) | |
# Prepare HTML with basic styling | |
full_html = f""" | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
body {{ font-family: Arial, sans-serif; padding: 20px; }} | |
pre, code {{ background: #f4f4f4; padding: 10px; border-radius: 5px; }} | |
table {{ border-collapse: collapse; width: 100%; }} | |
th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }} | |
th {{ background-color: #f2f2f2; }} | |
</style> | |
</head> | |
<body> | |
{html_content} | |
</body> | |
</html> | |
""" | |
# Save HTML to a temporary file | |
html_path = os.path.join(TEMP_DIR, "output.html") | |
with open(html_path, "w", encoding="utf-8") as f: | |
f.write(full_html) | |
# Generate preview HTML | |
preview_html = full_html | |
download_available = True | |
download_type = request.form.get("download_type", "png") | |
if "download" in request.form: | |
if download_type == "html": | |
return send_file( | |
html_path, | |
as_attachment=True, | |
download_name="output.html", | |
mimetype="text/html" | |
) | |
else: # PNG | |
# Convert HTML to PNG using imgkit | |
png_path = os.path.join(TEMP_DIR, "output.png") | |
imgkit.from_string(full_html, png_path, options={"quiet": ""}) | |
return send_file( | |
png_path, | |
as_attachment=True, | |
download_name="output.png", | |
mimetype="image/png" | |
) | |
except Exception as e: | |
error_message = f"Error processing request: {str(e)}" | |
print(f"Error: {traceback.format_exc()}") | |
return render_template_string(""" | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Markdown to PNG/HTML Converter</title> | |
<style> | |
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; } | |
textarea { width: 100%; height: 300px; margin-bottom: 10px; } | |
select, button { padding: 10px; margin: 5px; } | |
.preview { border: 1px solid #ddd; padding: 15px; margin-top: 20px; } | |
.download-btn { background-color: #4CAF50; color: white; border: none; cursor: pointer; } | |
.download-btn:hover { background-color: #45a049; } | |
.error { color: red; margin-top: 10px; } | |
</style> | |
</head> | |
<body> | |
<h1>Markdown to PNG/HTML Converter</h1> | |
<form method="post"> | |
<textarea name="markdown_text" placeholder="Paste your Markdown here...">{{ markdown_text }}</textarea><br> | |
<label for="download_type">Output format:</label> | |
<select name="download_type"> | |
<option value="png" {% if download_type == 'png' %}selected{% endif %}>PNG</option> | |
<option value="html" {% if download_type == 'html' %}selected{% endif %}>HTML</option> | |
</select><br> | |
<button type="submit">Generate Preview</button> | |
{% if download_available %} | |
<button type="submit" name="download" value="true" class="download-btn">Download {{ download_type.upper() }}</button> | |
{% endif %} | |
</form> | |
{% if error_message %} | |
<p class="error">{{ error_message }}</p> | |
{% endif %} | |
{% if preview_html %} | |
<h2>Preview</h2> | |
<div class="preview"> | |
{{ preview_html | safe }} | |
</div> | |
{% endif %} | |
</body> | |
</html> | |
""", preview_html=preview_html, download_available=download_available, | |
download_type=download_type, error_message=error_message, markdown_text=markdown_text) | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860))) |