RemoteMojo / main.py
SoUmNerd's picture
Update main.py
7efb37a
raw
history blame
686 Bytes
from fastapi import FastAPI
import subprocess
import re
def find_imports(code):
pattern = r'Python\.import_module\("([^"]*)"\)'
matches = re.findall(pattern, code)
return matches
app = FastAPI()
@app.post("/code")
def run_mojo_code(code:str, filename:str) -> dict[str]:
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