summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorprashantsinalkar2019-09-24 16:42:21 +0530
committerprashantsinalkar2019-09-24 16:42:21 +0530
commit8abd252e13863759208b8e3e58ec94d352a80ead (patch)
tree43a62d7be400fd3bd785630a8f6bc0d9b8764f3d
parent0018047ee5cecd81f0aa0b59ee3a45eb54d59258 (diff)
downloadR_on_Cloud_Flask_Web_API-8abd252e13863759208b8e3e58ec94d352a80ead.tar.gz
R_on_Cloud_Flask_Web_API-8abd252e13863759208b8e3e58ec94d352a80ead.tar.bz2
R_on_Cloud_Flask_Web_API-8abd252e13863759208b8e3e58ec94d352a80ead.zip
updated flask code for r api
-rw-r--r--.gitignore2
-rw-r--r--default_config.py16
-rw-r--r--r_api.py56
-rw-r--r--requirements.txt2
4 files changed, 76 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..382ddaa
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+__pycache__/
+config.py
diff --git a/default_config.py b/default_config.py
new file mode 100644
index 0000000..292a161
--- /dev/null
+++ b/default_config.py
@@ -0,0 +1,16 @@
+# Default R application location
+R_DIR = '/usr/bin/R'
+
+# The location to keep the flask session data on server.
+FLASKSESSIONDIR = '/tmp/flask-sessiondir'
+
+# The location to keep the session data on server.
+SESSIONDIR = '/tmp/sessiondir'
+SESSIONTIMEOUT = 21600
+
+# Temporary directory
+TEMP_DIR = '/tmp/R/'
+# The location to keep the flask caching data on server.
+FLASKCACHINGDEFAULTTIMEOUT = 900
+FLASKCACHINGDIR = '/tmp/flask-caching-dir'
+
diff --git a/r_api.py b/r_api.py
new file mode 100644
index 0000000..7c6a43f
--- /dev/null
+++ b/r_api.py
@@ -0,0 +1,56 @@
+from config import *
+import subprocess
+import flask
+from flask import Flask, url_for, jsonify, request
+import json
+import os
+import re
+from os.path import abspath, dirname, exists, isfile, join, splitext
+import config
+
+if not os.path.exists(TEMP_DIR):
+ os.makedirs(TEMP_DIR)
+
+app = Flask(__name__)
+
+
+@app.route('/')
+def api_root():
+ return 'Welcome'
+
+
+@app.route('/rscript', methods=['GET', 'POST'])
+def get_data():
+ # Validate the request body contains JSON
+ if request.is_json:
+
+ # Parse the JSON into a Python dictionary
+
+ req_data = json.loads(request.get_json())
+ user_dir = TEMP_DIR + req_data["user_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'
+ f = open(file_path, "w")
+ f.write(code)
+ f.close()
+ processed_data = subprocess.Popen(['Rscript', file_path],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ ret_data, err = processed_data.communicate()
+ response_body = {
+ "data": ret_data.decode("utf-8"),
+ "error": err.decode("utf-8")
+ }
+ result = jsonify(response_body)
+ return result
+
+ else:
+
+ # The request body wasn't JSON so return a 400 HTTP status code
+ return "Request was not JSON", 400
+
+
+if __name__ == '__main__':
+ app.run(debug=True)
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..5c1bea9
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,2 @@
+flask
+flask-session