diff options
author | prashantsinalkar | 2019-09-25 17:34:59 +0530 |
---|---|---|
committer | prashantsinalkar | 2019-09-25 17:34:59 +0530 |
commit | e9e5db84f7fa92d305e8450e2e2876391961c042 (patch) | |
tree | 9f1022323526b090165302223aaa24839baaa579 | |
parent | 8abd252e13863759208b8e3e58ec94d352a80ead (diff) | |
download | R_on_Cloud_Flask_Web_API-e9e5db84f7fa92d305e8450e2e2876391961c042.tar.gz R_on_Cloud_Flask_Web_API-e9e5db84f7fa92d305e8450e2e2876391961c042.tar.bz2 R_on_Cloud_Flask_Web_API-e9e5db84f7fa92d305e8450e2e2876391961c042.zip |
added generate plot and new settings
-rw-r--r-- | default_config.py | 4 | ||||
-rw-r--r-- | r_api.py | 38 |
2 files changed, 38 insertions, 4 deletions
diff --git a/default_config.py b/default_config.py index 292a161..612fff6 100644 --- a/default_config.py +++ b/default_config.py @@ -13,4 +13,8 @@ TEMP_DIR = '/tmp/R/' # The location to keep the flask caching data on server. FLASKCACHINGDEFAULTTIMEOUT = 900 FLASKCACHINGDIR = '/tmp/flask-caching-dir' +API_URL_PLOT = "http://127.0.0.1:5000/plot" +HTTP_HOST = '127.0.0.1' +HTTP_PORT = '5000' +PRODUCTION = True @@ -1,9 +1,12 @@ from config import * import subprocess import flask +from flask import send_file from flask import Flask, url_for, jsonify, request import json import os +import os.path +import base64 import re from os.path import abspath, dirname, exists, isfile, join, splitext import config @@ -27,21 +30,33 @@ def get_data(): # Parse the JSON into a Python dictionary req_data = json.loads(request.get_json()) - user_dir = TEMP_DIR + req_data["user_id"] + user_id = req_data["user_id"] + user_dir = TEMP_DIR + user_id + R_file_id = req_data["R_file_id"] code = req_data["code"] if not os.path.exists(user_dir): os.makedirs(user_dir) - file_path = user_dir + '/' + req_data["R_file_id"] + '.R' + file_path = user_dir + '/' + R_file_id + '.R' + plot_path = user_dir + '/' + R_file_id + '.png' f = open(file_path, "w") + #add png file path + f.write('png("{0}");\n'.format(plot_path)) + f.write('\n') f.write(code) + f.write('\n') + f.write("while (!is.null(dev.list())) dev.off()") f.close() processed_data = subprocess.Popen(['Rscript', file_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) ret_data, err = processed_data.communicate() + is_plot = str(os.path.isfile(plot_path)) + plot_path_req = API_URL_PLOT + '?user_id=' + user_id + '&R_file_id=' + R_file_id response_body = { "data": ret_data.decode("utf-8"), - "error": err.decode("utf-8") + "error": err.decode("utf-8"), + "is_plot": is_plot, + "plot_path_req": plot_path_req } result = jsonify(response_body) return result @@ -51,6 +66,21 @@ def get_data(): # The request body wasn't JSON so return a 400 HTTP status code return "Request was not JSON", 400 +@app.route('/plot', methods=['GET', 'POST']) +def get_plot(): + user_id = request.args.get('user_id') + R_file_id = request.args.get('R_file_id') + user_dir = TEMP_DIR + user_id + plot_path = user_dir + '/' + R_file_id + '.png' + try: + return send_file(plot_path, mimetype='image/png', as_attachment=True) + except Exception as e: + print("Error generated in") if __name__ == '__main__': - app.run(debug=True) + if (PRODUCTION == True): + app.run(debug = False) + else: + app.run(debug = True) + + app.run(port=HTTP_PORT, host=HTTP_HOST)
\ No newline at end of file |