broadfield-dev commited on
Commit
d5dc3fa
·
verified ·
1 Parent(s): 4b7ec42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -29
app.py CHANGED
@@ -2,26 +2,30 @@ 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
 
10
- # Ensure a temporary directory exists for saving files
11
- os.makedirs("temp", exist_ok=True)
 
 
 
 
 
 
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'])
27
 
@@ -45,30 +49,37 @@ def index():
45
  """
46
 
47
  # Save HTML to a temporary file
48
- html_path = "temp/output.html"
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("""
74
  <!DOCTYPE html>
@@ -79,15 +90,16 @@ def index():
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>
@@ -98,6 +110,9 @@ def index():
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">
@@ -106,7 +121,8 @@ def index():
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)))
 
2
  import markdown
3
  import imgkit
4
  import os
5
+ import traceback
6
  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
+ markdown_text = request.form.get("markdown_text", "") if request.method == "POST" else ""
 
 
 
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
 
 
49
  """
50
 
51
  # Save HTML to a temporary file
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
+ html_path,
65
+ as_attachment=True,
66
+ download_name="output.html",
67
+ mimetype="text/html"
68
+ )
69
+ else: # PNG
70
+ # Convert HTML to PNG using imgkit
71
+ png_path = os.path.join(TEMP_DIR, "output.png")
72
+ imgkit.from_string(full_html, png_path, options={"quiet": ""})
73
+ return send_file(
74
+ png_path,
75
+ as_attachment=True,
76
+ download_name="output.png",
77
+ mimetype="image/png"
78
+ )
79
+
80
+ except Exception as e:
81
+ error_message = f"Error processing request: {str(e)}"
82
+ print(f"Error: {traceback.format_exc()}")
83
 
84
  return render_template_string("""
85
  <!DOCTYPE html>
 
90
  body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
91
  textarea { width: 100%; height: 300px; margin-bottom: 10px; }
92
  select, button { padding: 10px; margin: 5px; }
93
+ .preview { border: 1px solid #ddd; padding: 15px; margin-top: 20px; }
94
  .download-btn { background-color: #4CAF50; color: white; border: none; cursor: pointer; }
95
  .download-btn:hover { background-color: #45a049; }
96
+ .error { color: red; margin-top: 10px; }
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><br>
103
  <label for="download_type">Output format:</label>
104
  <select name="download_type">
105
  <option value="png" {% if download_type == 'png' %}selected{% endif %}>PNG</option>
 
110
  <button type="submit" name="download" value="true" class="download-btn">Download {{ download_type.upper() }}</button>
111
  {% endif %}
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
  {% endif %}
122
  </body>
123
  </html>
124
+ """, preview_html=preview_html, download_available=download_available,
125
+ download_type=download_type, error_message=error_message, markdown_text=markdown_text)
126
 
127
  if __name__ == "__main__":
128
  app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))