broadfield-dev commited on
Commit
a9d4293
·
verified ·
1 Parent(s): 399aaf7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -30
app.py CHANGED
@@ -1,8 +1,9 @@
1
- from flask import Flask, request, render_template, render_template_string, send_file
2
  import markdown
3
- from weasyprint import HTML
4
  import os
5
  from io import BytesIO
 
6
 
7
  app = Flask(__name__)
8
 
@@ -11,8 +12,15 @@ os.makedirs("temp", exist_ok=True)
11
 
12
  @app.route("/", methods=["GET", "POST"])
13
  def index():
 
 
 
 
 
14
  if request.method == "POST":
15
  markdown_text = request.form.get("markdown_text")
 
 
16
  if markdown_text:
17
  # Convert Markdown to HTML
18
  html_content = markdown.markdown(markdown_text, extensions=['fenced_code', 'tables'])
@@ -41,31 +49,25 @@ def index():
41
  with open(html_path, "w") as f:
42
  f.write(full_html)
43
 
44
- # Convert HTML to PNG using WeasyPrint
45
- png_io = BytesIO()
46
- HTML(string=full_html).write_png(png_io)
47
- png_io.seek(0)
48
 
49
- # Save PNG to a temporary file
50
- png_path = "temp/output.png"
51
- with open(png_path, "wb") as f:
52
- f.write(png_io.getvalue())
53
-
54
- # Determine which file to download based on user selection
55
- download_type = request.form.get("download_type")
56
  if download_type == "html":
 
 
 
 
 
 
 
 
 
57
  return send_file(
58
- html_path,
59
- as_attachment=True,
60
- download_name="output.html",
61
- mimetype="text/html"
62
- )
63
- else: # Default to PNG
64
- return send_file(
65
- png_path,
66
  as_attachment=True,
67
- download_name="output.png",
68
- mimetype="image/png"
69
  )
70
 
71
  return render_template_string("""
@@ -77,22 +79,34 @@ def index():
77
  body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
78
  textarea { width: 100%; height: 300px; margin-bottom: 10px; }
79
  select, button { padding: 10px; margin: 5px; }
 
 
 
80
  </style>
81
  </head>
82
  <body>
83
  <h1>Markdown to PNG/HTML Converter</h1>
84
  <form method="post">
85
- <textarea name="markdown_text" placeholder="Paste your Markdown here..."></textarea><br>
86
- <label for="download_type">Download as:</label>
87
  <select name="download_type">
88
- <option value="png">PNG</option>
89
- <option value="html">HTML</option>
90
  </select><br>
91
- <button type="submit">Convert and Download</button>
 
 
 
92
  </form>
 
 
 
 
 
 
93
  </body>
94
  </html>
95
- """)
96
 
97
  if __name__ == "__main__":
98
- app.run(debug=True, host="0.0.0.0", port=7860)
 
1
+ from flask import Flask, request, render_template_string, send_file
2
  import markdown
3
+ import imgkit
4
  import os
5
  from io import BytesIO
6
+ import base64
7
 
8
  app = Flask(__name__)
9
 
 
12
 
13
  @app.route("/", methods=["GET", "POST"])
14
  def index():
15
+ preview_html = None
16
+ download_available = False
17
+ download_type = "png"
18
+ file_path = None
19
+
20
  if request.method == "POST":
21
  markdown_text = request.form.get("markdown_text")
22
+ download_type = request.form.get("download_type", "png")
23
+
24
  if markdown_text:
25
  # Convert Markdown to HTML
26
  html_content = markdown.markdown(markdown_text, extensions=['fenced_code', 'tables'])
 
49
  with open(html_path, "w") as f:
50
  f.write(full_html)
51
 
52
+ # Generate preview HTML
53
+ preview_html = full_html
54
+ download_available = True
 
55
 
 
 
 
 
 
 
 
56
  if download_type == "html":
57
+ file_path = html_path
58
+ else: # PNG
59
+ # Convert HTML to PNG using imgkit
60
+ png_path = "temp/output.png"
61
+ imgkit.from_string(full_html, png_path, options={"quiet": ""})
62
+ file_path = png_path
63
+
64
+ # Handle download if requested
65
+ if "download" in request.form:
66
  return send_file(
67
+ file_path,
 
 
 
 
 
 
 
68
  as_attachment=True,
69
+ download_name=f"output.{download_type}",
70
+ mimetype="text/html" if download_type == "html" else "image/png"
71
  )
72
 
73
  return render_template_string("""
 
79
  body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
80
  textarea { width: 100%; height: 300px; margin-bottom: 10px; }
81
  select, button { padding: 10px; margin: 5px; }
82
+ .preview { border: 1px solid #ddd; padding: 12px; margin-top: 20px; }
83
+ .download-btn { background-color: #4CAF50; color: white; border: none; cursor: pointer; }
84
+ .download-btn:hover { background-color: #45a049; }
85
  </style>
86
  </head>
87
  <body>
88
  <h1>Markdown to PNG/HTML Converter</h1>
89
  <form method="post">
90
+ <textarea name="markdown_text" placeholder="Paste your Markdown here...">{{ request.form.get('markdown_text', '') }}</textarea><br>
91
+ <label for="download_type">Output format:</label>
92
  <select name="download_type">
93
+ <option value="png" {% if download_type == 'png' %}selected{% endif %}>PNG</option>
94
+ <option value="html" {% if download_type == 'html' %}selected{% endif %}>HTML</option>
95
  </select><br>
96
+ <button type="submit">Generate Preview</button>
97
+ {% if download_available %}
98
+ <button type="submit" name="download" value="true" class="download-btn">Download {{ download_type.upper() }}</button>
99
+ {% endif %}
100
  </form>
101
+ {% if preview_html %}
102
+ <h2>Preview</h2>
103
+ <div class="preview">
104
+ {{ preview_html | safe }}
105
+ </div>
106
+ {% endif %}
107
  </body>
108
  </html>
109
+ """, preview_html=preview_html, download_available=download_available, download_type=download_type)
110
 
111
  if __name__ == "__main__":
112
+ app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))