Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,40 +7,115 @@ from io import BytesIO
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
-
# Use a directory within the app's working directory to avoid permission issues
|
|
|
11 |
TEMP_DIR = os.path.join(os.getcwd(), "temp")
|
12 |
|
13 |
-
# Create temporary directory if it doesn't exist
|
14 |
try:
|
15 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
16 |
except Exception as e:
|
|
|
17 |
print(f"Error creating temp directory: {e}")
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
@app.route("/", methods=["GET", "POST"])
|
20 |
def index():
|
|
|
|
|
|
|
21 |
preview_html = None
|
22 |
download_available = False
|
23 |
-
download_type = "png"
|
24 |
error_message = None
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
if request.method == "POST" and markdown_text:
|
28 |
try:
|
29 |
-
# Convert Markdown to HTML
|
|
|
30 |
html_content = markdown.markdown(markdown_text, extensions=['fenced_code', 'tables'])
|
31 |
|
32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
full_html = f"""
|
34 |
<!DOCTYPE html>
|
35 |
<html>
|
36 |
<head>
|
37 |
-
<
|
38 |
-
|
39 |
-
|
40 |
-
table {{ border-collapse: collapse; width: 100%; }}
|
41 |
-
th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
|
42 |
-
th {{ background-color: #f2f2f2; }}
|
43 |
-
</style>
|
44 |
</head>
|
45 |
<body>
|
46 |
{html_content}
|
@@ -48,28 +123,26 @@ def index():
|
|
48 |
</html>
|
49 |
"""
|
50 |
|
51 |
-
#
|
52 |
-
html_path = os.path.join(TEMP_DIR, "output.html")
|
53 |
-
with open(html_path, "w", encoding="utf-8") as f:
|
54 |
-
f.write(full_html)
|
55 |
-
|
56 |
-
# Generate preview HTML
|
57 |
preview_html = full_html
|
58 |
download_available = True
|
59 |
-
download_type = request.form.get("download_type", "png")
|
60 |
|
|
|
61 |
if "download" in request.form:
|
62 |
if download_type == "html":
|
|
|
63 |
return send_file(
|
64 |
-
|
65 |
as_attachment=True,
|
66 |
download_name="output.html",
|
67 |
mimetype="text/html"
|
68 |
)
|
69 |
-
else: # PNG
|
70 |
-
#
|
|
|
71 |
png_path = os.path.join(TEMP_DIR, "output.png")
|
72 |
-
imgkit.from_string(full_html, png_path, options={"quiet": "",'encoding': "UTF-8"})
|
|
|
73 |
return send_file(
|
74 |
png_path,
|
75 |
as_attachment=True,
|
@@ -78,41 +151,110 @@ def index():
|
|
78 |
)
|
79 |
|
80 |
except Exception as e:
|
81 |
-
|
|
|
82 |
print(f"Error: {traceback.format_exc()}")
|
83 |
|
|
|
84 |
return render_template_string("""
|
85 |
<!DOCTYPE html>
|
86 |
-
<html>
|
87 |
<head>
|
88 |
-
<
|
|
|
89 |
<style>
|
90 |
-
body { font-family: Arial, sans-serif; max-width:
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
.
|
95 |
-
.
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
</style>
|
98 |
</head>
|
99 |
<body>
|
100 |
-
<h1>Markdown to PNG/HTML Converter</h1>
|
101 |
<form method="post">
|
102 |
-
<textarea name="markdown_text" placeholder="Paste your Markdown here...">{{ markdown_text }}</textarea
|
103 |
-
<
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
<
|
109 |
-
|
110 |
-
<
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
</form>
|
|
|
113 |
{% if error_message %}
|
114 |
<p class="error">{{ error_message }}</p>
|
115 |
{% endif %}
|
|
|
116 |
{% if preview_html %}
|
117 |
<h2>Preview</h2>
|
118 |
<div class="preview">
|
@@ -121,8 +263,17 @@ def index():
|
|
121 |
{% endif %}
|
122 |
</body>
|
123 |
</html>
|
124 |
-
""",
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
if __name__ == "__main__":
|
|
|
|
|
128 |
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
+
# Use a directory within the app's working directory to avoid permission issues.
|
11 |
+
# This makes it more compatible with various deployment environments.
|
12 |
TEMP_DIR = os.path.join(os.getcwd(), "temp")
|
13 |
|
14 |
+
# Create the temporary directory if it doesn't exist.
|
15 |
try:
|
16 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
17 |
except Exception as e:
|
18 |
+
# Log an error if the directory cannot be created.
|
19 |
print(f"Error creating temp directory: {e}")
|
20 |
|
21 |
+
# Define default values for styling options for a cleaner initial state.
|
22 |
+
DEFAULT_STYLES = {
|
23 |
+
"font_family": "'Arial', sans-serif",
|
24 |
+
"font_size": "16",
|
25 |
+
"text_color": "#333333",
|
26 |
+
"background_color": "#ffffff",
|
27 |
+
"code_bg_color": "#f4f4f4",
|
28 |
+
"custom_css": ""
|
29 |
+
}
|
30 |
+
|
31 |
@app.route("/", methods=["GET", "POST"])
|
32 |
def index():
|
33 |
+
"""
|
34 |
+
Main route to handle form submission, Markdown processing, and rendering.
|
35 |
+
"""
|
36 |
preview_html = None
|
37 |
download_available = False
|
|
|
38 |
error_message = None
|
39 |
+
|
40 |
+
# On POST, process the form data. On GET, use defaults.
|
41 |
+
if request.method == "POST":
|
42 |
+
markdown_text = request.form.get("markdown_text", "")
|
43 |
+
download_type = request.form.get("download_type", "png")
|
44 |
+
|
45 |
+
# Get styling options from the form, falling back to defaults.
|
46 |
+
styles = {
|
47 |
+
"font_family": request.form.get("font_family", DEFAULT_STYLES["font_family"]),
|
48 |
+
"font_size": request.form.get("font_size", DEFAULT_STYLES["font_size"]),
|
49 |
+
"text_color": request.form.get("text_color", DEFAULT_STYLES["text_color"]),
|
50 |
+
"background_color": request.form.get("background_color", DEFAULT_STYLES["background_color"]),
|
51 |
+
"code_bg_color": request.form.get("code_bg_color", DEFAULT_STYLES["code_bg_color"]),
|
52 |
+
"custom_css": request.form.get("custom_css", DEFAULT_STYLES["custom_css"])
|
53 |
+
}
|
54 |
+
include_fontawesome = "include_fontawesome" in request.form
|
55 |
+
|
56 |
+
else: # GET request
|
57 |
+
markdown_text = ""
|
58 |
+
download_type = "png"
|
59 |
+
styles = DEFAULT_STYLES.copy()
|
60 |
+
include_fontawesome = False
|
61 |
+
|
62 |
|
63 |
if request.method == "POST" and markdown_text:
|
64 |
try:
|
65 |
+
# Convert Markdown to HTML using python-markdown library.
|
66 |
+
# Extensions for tables and fenced code blocks are enabled.
|
67 |
html_content = markdown.markdown(markdown_text, extensions=['fenced_code', 'tables'])
|
68 |
|
69 |
+
# Optional: Include Font Awesome CSS if the user checked the box.
|
70 |
+
fontawesome_link = ""
|
71 |
+
if include_fontawesome:
|
72 |
+
fontawesome_link = '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">'
|
73 |
+
|
74 |
+
# Dynamically generate the CSS style block from user options.
|
75 |
+
style_block = f"""
|
76 |
+
<style>
|
77 |
+
body {{
|
78 |
+
font-family: {styles['font_family']};
|
79 |
+
font-size: {styles['font_size']}px;
|
80 |
+
color: {styles['text_color']};
|
81 |
+
background-color: {styles['background_color']};
|
82 |
+
padding: 25px;
|
83 |
+
display: inline-block; /* Helps imgkit to crop correctly */
|
84 |
+
}}
|
85 |
+
pre, code {{
|
86 |
+
background: {styles['code_bg_color']};
|
87 |
+
padding: 10px;
|
88 |
+
border-radius: 5px;
|
89 |
+
}}
|
90 |
+
table {{
|
91 |
+
border-collapse: collapse;
|
92 |
+
width: 100%;
|
93 |
+
}}
|
94 |
+
th, td {{
|
95 |
+
border: 1px solid #ddd;
|
96 |
+
padding: 8px;
|
97 |
+
text-align: left;
|
98 |
+
}}
|
99 |
+
th {{
|
100 |
+
background-color: #f2f2f2;
|
101 |
+
}}
|
102 |
+
img {{
|
103 |
+
max-width: 100%;
|
104 |
+
height: auto;
|
105 |
+
}}
|
106 |
+
/* User-defined custom CSS */
|
107 |
+
{styles['custom_css']}
|
108 |
+
</style>
|
109 |
+
"""
|
110 |
+
|
111 |
+
# Combine everything into a full HTML document.
|
112 |
full_html = f"""
|
113 |
<!DOCTYPE html>
|
114 |
<html>
|
115 |
<head>
|
116 |
+
<meta charset="UTF-8">
|
117 |
+
{fontawesome_link}
|
118 |
+
{style_block}
|
|
|
|
|
|
|
|
|
119 |
</head>
|
120 |
<body>
|
121 |
{html_content}
|
|
|
123 |
</html>
|
124 |
"""
|
125 |
|
126 |
+
# Set flags and content for the frontend preview.
|
|
|
|
|
|
|
|
|
|
|
127 |
preview_html = full_html
|
128 |
download_available = True
|
|
|
129 |
|
130 |
+
# If the user clicked the "Download" button.
|
131 |
if "download" in request.form:
|
132 |
if download_type == "html":
|
133 |
+
# For HTML download, send the generated HTML directly.
|
134 |
return send_file(
|
135 |
+
BytesIO(full_html.encode("utf-8")),
|
136 |
as_attachment=True,
|
137 |
download_name="output.html",
|
138 |
mimetype="text/html"
|
139 |
)
|
140 |
+
else: # For PNG download
|
141 |
+
# Use imgkit to convert the HTML string to a PNG image.
|
142 |
+
# The 'quiet' option suppresses console output from wkhtmltoimage.
|
143 |
png_path = os.path.join(TEMP_DIR, "output.png")
|
144 |
+
imgkit.from_string(full_html, png_path, options={"quiet": "", 'encoding': "UTF-8"})
|
145 |
+
|
146 |
return send_file(
|
147 |
png_path,
|
148 |
as_attachment=True,
|
|
|
151 |
)
|
152 |
|
153 |
except Exception as e:
|
154 |
+
# In case of any error, display a detailed message to the user.
|
155 |
+
error_message = f"An error occurred: {str(e)}"
|
156 |
print(f"Error: {traceback.format_exc()}")
|
157 |
|
158 |
+
# Render the main page template with all the necessary variables.
|
159 |
return render_template_string("""
|
160 |
<!DOCTYPE html>
|
161 |
+
<html lang="en">
|
162 |
<head>
|
163 |
+
<meta charset="UTF-8">
|
164 |
+
<title>Advanced Markdown to PNG/HTML Converter</title>
|
165 |
<style>
|
166 |
+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; background-color: #f9f9f9; }
|
167 |
+
h1 { text-align: center; color: #333; }
|
168 |
+
form { background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
|
169 |
+
textarea { width: 100%; height: 300px; margin-bottom: 10px; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px; padding: 10px; }
|
170 |
+
.controls { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 20px; }
|
171 |
+
.main-actions, .style-options { display: flex; flex-wrap: wrap; gap: 15px; align-items: center; }
|
172 |
+
fieldset { border: 1px solid #ddd; padding: 15px; border-radius: 5px; margin-top: 20px; }
|
173 |
+
legend { font-weight: bold; color: #555; }
|
174 |
+
.style-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 15px; }
|
175 |
+
.style-grid > div { display: flex; flex-direction: column; }
|
176 |
+
label { margin-bottom: 5px; color: #666; }
|
177 |
+
select, input[type="number"], input[type="color"], input[type="text"] { padding: 8px; border: 1px solid #ccc; border-radius: 4px; }
|
178 |
+
button { padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }
|
179 |
+
.generate-btn { background-color: #007BFF; color: white; }
|
180 |
+
.generate-btn:hover { background-color: #0056b3; }
|
181 |
+
.download-btn { background-color: #28a745; color: white; }
|
182 |
+
.download-btn:hover { background-color: #218838; }
|
183 |
+
.preview { border: 1px solid #ddd; padding: 20px; margin-top: 20px; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.05); }
|
184 |
+
.error { color: #D8000C; background-color: #FFD2D2; padding: 10px; border-radius: 5px; margin-top: 15px; }
|
185 |
+
.info { color: #00529B; background-color: #BDE5F8; padding: 10px; border-radius: 5px; margin: 10px 0; }
|
186 |
</style>
|
187 |
</head>
|
188 |
<body>
|
189 |
+
<h1>Advanced Markdown to PNG/HTML Converter</h1>
|
190 |
<form method="post">
|
191 |
+
<textarea name="markdown_text" placeholder="Paste your Markdown here...">{{ styles.get('markdown_text', '') }}</textarea>
|
192 |
+
<div class="info">
|
193 |
+
<b>Tip:</b> To include images, use full public URLs (e.g., `https://.../image.png`).
|
194 |
+
To use icons, check "Include Font Awesome" below and use tags like `<i class="fa-solid fa-star"></i>`.
|
195 |
+
</div>
|
196 |
+
|
197 |
+
<fieldset>
|
198 |
+
<legend>Styling Options</legend>
|
199 |
+
<div class="style-grid">
|
200 |
+
<div>
|
201 |
+
<label for="font_family">Font Family:</label>
|
202 |
+
<select id="font_family" name="font_family">
|
203 |
+
<option value="'Arial', sans-serif" {% if styles.font_family == "'Arial', sans-serif" %}selected{% endif %}>Arial</option>
|
204 |
+
<option value="'Georgia', serif" {% if styles.font_family == "'Georgia', serif" %}selected{% endif %}>Georgia</option>
|
205 |
+
<option value="'Times New Roman', serif" {% if styles.font_family == "'Times New Roman', serif" %}selected{% endif %}>Times New Roman</option>
|
206 |
+
<option value="'Verdana', sans-serif" {% if styles.font_family == "'Verdana', sans-serif" %}selected{% endif %}>Verdana</option>
|
207 |
+
<option value="'Courier New', monospace" {% if styles.font_family == "'Courier New', monospace" %}selected{% endif %}>Courier New</option>
|
208 |
+
</select>
|
209 |
+
</div>
|
210 |
+
<div>
|
211 |
+
<label for="font_size">Font Size (px):</label>
|
212 |
+
<input type="number" id="font_size" name="font_size" value="{{ styles.font_size }}">
|
213 |
+
</div>
|
214 |
+
<div>
|
215 |
+
<label for="text_color">Text Color:</label>
|
216 |
+
<input type="color" id="text_color" name="text_color" value="{{ styles.text_color }}">
|
217 |
+
</div>
|
218 |
+
<div>
|
219 |
+
<label for="background_color">Background Color:</label>
|
220 |
+
<input type="color" id="background_color" name="background_color" value="{{ styles.background_color }}">
|
221 |
+
</div>
|
222 |
+
<div>
|
223 |
+
<label for="code_bg_color">Code BG Color:</label>
|
224 |
+
<input type="color" id="code_bg_color" name="code_bg_color" value="{{ styles.code_bg_color }}">
|
225 |
+
</div>
|
226 |
+
</div>
|
227 |
+
<div>
|
228 |
+
<input type="checkbox" id="include_fontawesome" name="include_fontawesome" {% if include_fontawesome %}checked{% endif %}>
|
229 |
+
<label for="include_fontawesome">Include Font Awesome (for icons)</label>
|
230 |
+
</div>
|
231 |
+
<div>
|
232 |
+
<label for="custom_css">Custom CSS:</label>
|
233 |
+
<textarea id="custom_css" name="custom_css" rows="4" placeholder="e.g., h1 { color: blue; }">{{ styles.custom_css }}</textarea>
|
234 |
+
</div>
|
235 |
+
</fieldset>
|
236 |
+
|
237 |
+
<div class="controls">
|
238 |
+
<div class="main-actions">
|
239 |
+
<button type="submit" class="generate-btn">Generate Preview</button>
|
240 |
+
<div>
|
241 |
+
<label for="download_type">Output format:</label>
|
242 |
+
<select id="download_type" name="download_type">
|
243 |
+
<option value="png" {% if download_type == 'png' %}selected{% endif %}>PNG</option>
|
244 |
+
<option value="html" {% if download_type == 'html' %}selected{% endif %}>HTML</option>
|
245 |
+
</select>
|
246 |
+
</div>
|
247 |
+
{% if download_available %}
|
248 |
+
<button type="submit" name="download" value="true" class="download-btn">Download {{ download_type.upper() }}</button>
|
249 |
+
{% endif %}
|
250 |
+
</div>
|
251 |
+
</div>
|
252 |
</form>
|
253 |
+
|
254 |
{% if error_message %}
|
255 |
<p class="error">{{ error_message }}</p>
|
256 |
{% endif %}
|
257 |
+
|
258 |
{% if preview_html %}
|
259 |
<h2>Preview</h2>
|
260 |
<div class="preview">
|
|
|
263 |
{% endif %}
|
264 |
</body>
|
265 |
</html>
|
266 |
+
""",
|
267 |
+
styles=styles,
|
268 |
+
markdown_text=markdown_text,
|
269 |
+
download_type=download_type,
|
270 |
+
include_fontawesome=include_fontawesome,
|
271 |
+
download_available=download_available,
|
272 |
+
preview_html=preview_html,
|
273 |
+
error_message=error_message
|
274 |
+
)
|
275 |
|
276 |
if __name__ == "__main__":
|
277 |
+
# It's recommended to run Flask applications using a production-ready WSGI server.
|
278 |
+
# The built-in development server is used here for convenience.
|
279 |
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|