RemoteMojo / main.py
SoUmNerd's picture
Update main.py
fbacf15
raw
history blame
758 Bytes
from fastapi import FastAPI, Request
import subprocess, json
from regex import find_imports
app = FastAPI()
@app.post("/code")
async def run_mojo_code(request:Request) -> dict:
data_as_str = await request.json()
print(data_as_str)
print(type(data_as_str))
data = data_as_str
code = data["code"]
filename = data["filename"]
try:
imports = find_imports(code)
for imported in imports:
subprocess.call(["python3", "-m", "pip", "install", imported], shell=True)
with open(filename, "w") as f:
f.write(code)
return {"sucess":True, "output": subprocess.check_output(["mojo", filename]).decode("utf-8")}, 200
except:
return {"sucess":False}, 500