Skip to content

Commit caf48bc

Browse files
committed
Lint
1 parent 38143ac commit caf48bc

File tree

1 file changed

+54
-51
lines changed

1 file changed

+54
-51
lines changed

Backend/app.py

+54-51
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#importing dependencies - Flask for server and docker to communicate with docker API
1+
# importing dependencies - Flask for server and docker to communicate with docker API
22
from flask import Flask, jsonify, request
3-
from flask_cors import CORS,cross_origin
3+
from flask_cors import CORS, cross_origin
44
import docker
55
import os
66
import tarfile
@@ -9,66 +9,69 @@
99

1010
cors = CORS(app)
1111

12-
app.config['CORS_HEADERS'] = 'Content-Type'
12+
app.config["CORS_HEADERS"] = "Content-Type"
1313

14-
#Starting container
14+
# Starting container
1515
client = docker.from_env()
16-
container = client.containers.run('playground-small', tty=True,detach=True)
16+
container = client.containers.run("playground-small", tty=True, detach=True)
1717

18-
#Editing the file with code inside editor
19-
def edit_file(code, input=''):
20-
Fortran_file = open('./File.f90','w+')
21-
Fortran_file.write(code)
22-
program_input = open('./program_input.txt', 'w+')
23-
program_input.write(input)
18+
# Editing the file with code inside editor
19+
def edit_file(code, input=""):
20+
Fortran_file = open("./File.f90", "w+")
21+
Fortran_file.write(code)
22+
program_input = open("./program_input.txt", "w+")
23+
program_input.write(input)
2424

2525

26-
27-
#Copying file with fortran code to container
26+
# Copying file with fortran code to container
2827
def copy_to(src, dst, container):
29-
dst = dst
30-
container = container
28+
dst = dst
29+
container = container
3130

32-
os.chdir(os.path.dirname(src))
33-
srcname = os.path.basename(src)
34-
tar = tarfile.open(src + '.tar', mode='w')
35-
try:
36-
tar.add(srcname)
37-
finally:
38-
tar.close()
31+
os.chdir(os.path.dirname(src))
32+
srcname = os.path.basename(src)
33+
tar = tarfile.open(src + ".tar", mode="w")
34+
try:
35+
tar.add(srcname)
36+
finally:
37+
tar.close()
3938

40-
data = open(src + '.tar', 'rb').read()
41-
container.put_archive(os.path.dirname(dst), data)
39+
data = open(src + ".tar", "rb").read()
40+
container.put_archive(os.path.dirname(dst), data)
4241

4342

44-
#Executing code inside container and getting it's output
43+
# Executing code inside container and getting it's output
4544
def execute_code_in_container():
46-
copy_to('./File.f90', '/fortran/File.f90',container)
47-
copy_to('./program_input.txt', '/fortran/program_input.txt',container)
48-
executable = container.exec_run('gfortran File.f90 -o executed_file.o',demux=True)
49-
if(executable.exit_code==0):
50-
a = container.exec_run('sh -c "cat program_input.txt | ./executed_file.o"', demux=True)
51-
else:
52-
a = executable
53-
return a
54-
55-
56-
#API Endpoint to submit code
57-
@app.route('/run', methods=['POST','GET','OPTIONS'])
45+
copy_to("./File.f90", "/fortran/File.f90", container)
46+
copy_to("./program_input.txt", "/fortran/program_input.txt", container)
47+
executable = container.exec_run(
48+
"gfortran File.f90 -o executed_file.o", demux=True
49+
)
50+
if executable.exit_code == 0:
51+
a = container.exec_run(
52+
'sh -c "cat program_input.txt | ./executed_file.o"', demux=True
53+
)
54+
else:
55+
a = executable
56+
return a
57+
58+
59+
# API Endpoint to submit code
60+
@app.route("/run", methods=["POST", "GET", "OPTIONS"])
5861
@cross_origin()
5962
def run_code():
60-
data = request.get_json()
61-
edit_file(data["code"],data["programInput"])
62-
code_result = execute_code_in_container()
63-
if code_result.exit_code == 0:
64-
print(code_result.output[0].decode())
65-
output = jsonify({"executed" : code_result.output[0].decode()})
66-
else:
67-
print(code_result.output[1].decode())
68-
output = jsonify({"executed" : code_result.output[1].decode()})
69-
70-
return output, 202
71-
72-
if __name__ == '__main__':
73-
app.run(debug = True)
63+
data = request.get_json()
64+
edit_file(data["code"], data["programInput"])
65+
code_result = execute_code_in_container()
66+
if code_result.exit_code == 0:
67+
print(code_result.output[0].decode())
68+
output = jsonify({"executed": code_result.output[0].decode()})
69+
else:
70+
print(code_result.output[1].decode())
71+
output = jsonify({"executed": code_result.output[1].decode()})
72+
73+
return output, 202
74+
7475

76+
if __name__ == "__main__":
77+
app.run(debug=True)

0 commit comments

Comments
 (0)