SoUmNerd commited on
Commit
b887ba2
·
1 Parent(s): 97c616c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +15 -15
main.py CHANGED
@@ -1,25 +1,25 @@
1
- from flask import Flask, request, jsonify
 
 
2
  import subprocess
3
  from regex import find_imports
 
 
 
 
4
 
5
- app = Flask(__name__)
6
 
7
- @app.route("/")
8
- def index():
9
- return "Hello World!"
10
 
11
- @app.route("/code", methods=['POST'])
12
- def run_mojo_code():
13
- item = request.get_json()
14
  try:
15
- imports = find_imports(item['code'])
16
  for imported in imports:
17
  subprocess.call(["python3", "-m", "pip", "install", imported], shell=True)
18
- with open(item['filename'], "w") as f:
19
- f.write(item['code'])
20
 
21
- return jsonify({"sucess":True, "output": subprocess.check_output(["mojo", item['filename']]).decode("utf-8")}), 200
22
  except:
23
- return jsonify({"sucess":False}), 500
24
-
25
- app.run(port=7860, debug=True)
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+
4
  import subprocess
5
  from regex import find_imports
6
+ class Item(BaseModel):
7
+ code: str
8
+ filename: str = ''
9
+
10
 
 
11
 
12
+ app = FastAPI()
 
 
13
 
14
+ @app.post("/code")
15
+ def run_mojo_code(item:Item) -> dict:
 
16
  try:
17
+ imports = find_imports(item.code)
18
  for imported in imports:
19
  subprocess.call(["python3", "-m", "pip", "install", imported], shell=True)
20
+ with open(item.filename, "w") as f:
21
+ f.write(item.code)
22
 
23
+ return {"sucess":True, "output": subprocess.check_output(["mojo", item.filename]).decode("utf-8")}, 200
24
  except:
25
+ return {"sucess":False}, 500