-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
32 lines (24 loc) · 958 Bytes
/
flask_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from flask import Flask, request, render_template, send_file, Response
import os
from wordcloud_generator import generate_word_cloud
from io import BytesIO
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
text_data = request.form.get('text_data')
if not text_data:
return "Please enter some text data."
wordcloud = generate_word_cloud(text_data)
# Create a downloadable response
img_bytes_io = BytesIO()
wordcloud.to_image().save(img_bytes_io, 'PNG')
img_bytes = img_bytes_io.getvalue()
img_bytes_io.close()
return Response(img_bytes, mimetype='image/png', headers={
'Content-Disposition': 'attachment; filename=wordcloud.png'
})
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)