Skip to content

Commit

Permalink
Merge branch 'release/v1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joo-Ho Lee committed May 20, 2019
2 parents 626bc97 + 63539f4 commit 5aadc02
Show file tree
Hide file tree
Showing 337 changed files with 172 additions and 0 deletions.
98 changes: 98 additions & 0 deletions App.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import os

from flask import Flask, jsonify, request, abort, send_from_directory
from flask_restful import Api, Resource, reqparse

from wcg import WCG

app = Flask(__name__)
api = Api(app)

APP_ROOT = os.path.dirname(os.path.abspath(__file__))


# default access page
@app.route("/")
def main():
return abort(404)


# upload sected image and foward to processing page
@app.route("/upload", methods=["POST"])
def upload_image():
# create image directory if not found
target = os.path.join(APP_ROOT, 'images')
if not os.path.isdir(target):
os.mkdir(target)
target = os.path.join(target, 'upload/')
if not os.path.isdir(target):
os.mkdir(target)

# retrieve file from ..
upload = request.files.getlist("image")[0]
filename = upload.filename
print("File name: {filename}".format(filename=filename))
filename = str(filename).lower()
print("File name: {filename}".format(filename=filename))

# file support verification
extension = os.path.splitext(filename)[1]
if (extension == ".jpg") or (extension == ".jpeg") or (extension == ".png") or (extension == ".bmp"):
print("File accepted")
else:
return abort(400)

# save file
destination = "".join([target, filename])
print("File saved to : ", destination)
upload.save(destination)

# forward to processing page
return send_image(filename)


@app.route('/wordcloud', methods=['POST'])
def generate_wordcloud():
directory_path = os.path.join(APP_ROOT, 'images/')
directory_path = os.path.join(directory_path, 'upload/')

mask_upload = request.files.getlist("mask_image")[0]
mask_filename = str(mask_upload.filename).lower()
print("File name: {filename}".format(filename=mask_filename))

extension = os.path.splitext(mask_filename)[1]
if (extension == ".jpg") or (extension == ".jpeg") or (extension == ".png") or (extension == ".bmp"):
print("File accepted")
else:
return abort(400)

# Save file
mask_image_path = "".join([directory_path, mask_filename])
print(" path : {path}".format(path=mask_image_path))
mask_upload.save(mask_image_path)

title = request.form['title']
data = request.form['data']
font = request.form['font']

wc = WCG(title=title, data=data, font=font, mask_image_path=mask_image_path)
wordCloud_path = wc.generate()

print(wordCloud_path)
return send_wordcloud(wordCloud_path)


# retrieve file from 'static/images' directory
@app.route('/static/mask/<filename>')
def send_image(filename):
return send_from_directory("images/upload", filename)


@app.route('/static/wordcloud/<filename>')
def send_wordcloud(filename):
print(filename)
return send_from_directory("images/wordcloud", filename)


if __name__ == "__main__":
app.run(debug=True, host='', port=8000)
Loading

0 comments on commit 5aadc02

Please sign in to comment.