diff options
Diffstat (limited to 'website')
-rw-r--r-- | website/helpers.py | 28 | ||||
-rw-r--r-- | website/views.py | 16 |
2 files changed, 21 insertions, 23 deletions
diff --git a/website/helpers.py b/website/helpers.py index eac7720..016f2fd 100644 --- a/website/helpers.py +++ b/website/helpers.py @@ -5,7 +5,7 @@ import subprocess from soc.settings import PROJECT_DIR -def scilab_run(code, token): +def scilab_run(code, token, example_id): #Check for system commands system_commands = re.compile( 'unix\(.*\)|unix_g\(.*\)|unix_w\(.*\)|unix_x\(.*\)|unix_s\(.*\)' @@ -20,24 +20,14 @@ def scilab_run(code, token): #Finding the plot and appending xs2jpg function p = re.compile(r'.*plot.*\(.*,.*,*\).*\n') + + plot_path = '' if p.search(code): plot_exists = True code = code + '\n' - temp = code - code = '' - start, end, count = 0, 0, 0 current_time = time.time() - plot_path = [] - for (count, match) in enumerate(p.finditer(temp)): - print "count==================",count - plot_path.append( - PROJECT_DIR + '/static/tmp/{0}.jpg'.format(int(current_time) + count) - ) - end = match.end() - code += temp[start:end] - code += 'xs2jpg(gcf(), "{0}");\n'.format(plot_path[-1]) - start = end - code += temp[end:] + plot_path = PROJECT_DIR + '/static/tmp/{0}.jpg'.format(str(current_time)) + code += 'xs2jpg(gcf(), "{0}");\n'.format(plot_path) #Check whether to load scimax / maxima if 'syms' in code or 'Syms' in code: @@ -59,7 +49,6 @@ def scilab_run(code, token): #getting stuck in the prompt in case of error cmd = 'printf "lines(0)\nexec(\'{0}\',2);\nquit();"'.format(file_path) cmd += ' | /home/cheese/scilab-5.4.1/bin/scilab-adv-cli -nw' - print cmd output = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True @@ -67,10 +56,13 @@ def scilab_run(code, token): #os.remove(file_path) output = trim(output) - return output + data = { + 'output': output, + 'plot_path': plot_path.replace(PROJECT_DIR, '') + } + return data def trim(output): #for future use output = re.sub(r'\n \n \n', '\n', output) - #print repr(output) return output diff --git a/website/views.py b/website/views.py index aba54a2..785337f 100644 --- a/website/views.py +++ b/website/views.py @@ -66,19 +66,25 @@ def ajax_code(request): if request.method == "POST": example_id = request.POST['example_id'] example = TextbookCompanionExampleFiles.objects.using('scilab')\ - .get(id=example_id) + .get(example_id=example_id, filetype='S') example_path = '/var/www/scilab_in/uploads/' + example.filepath + f = open(example_path) code = f.readlines() f.close() - - print code return HttpResponse(code) def ajax_execute(request): if request.method == "POST": code = request.POST['code'] + example_id = request.POST.get('example_id', None) token = request.POST['csrfmiddlewaretoken'] - result = scilab_run(code, token) - return HttpResponse(result) + data = scilab_run(code, token, example_id) + return render(request, 'website/templates/ajax-execute.html', data) + + + + + + |