File size: 666 Bytes
b887ba2
 
 
f7cb3f7
3014ad5
b887ba2
 
 
 
4ec23ab
 
b887ba2
4ec23ab
b887ba2
 
4ec23ab
b887ba2
4ec23ab
 
b887ba2
 
4ec23ab
b887ba2
4ec23ab
b887ba2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from fastapi import FastAPI
from pydantic import BaseModel

import subprocess
from regex import find_imports
class Item(BaseModel):
    code: str
    filename: str = ''



app = FastAPI()

@app.post("/code")
def run_mojo_code(item:Item) -> dict:
    try:
        imports = find_imports(item.code)
        for imported in imports:
            subprocess.call(["python3", "-m", "pip", "install", imported], shell=True)
        with open(item.filename, "w") as f:
            f.write(item.code) 
            
        return {"sucess":True, "output": subprocess.check_output(["mojo", item.filename]).decode("utf-8")}, 200
    except:
        return {"sucess":False}, 500