SoUmNerd commited on
Commit
3014ad5
1 Parent(s): d6b8aa2

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -10
main.py CHANGED
@@ -1,24 +1,25 @@
1
  from fastapi import FastAPI
 
 
2
  import subprocess
3
- import re
 
 
 
4
 
5
- def find_imports(code):
6
- pattern = r'Python\.import_module\("([^"]*)"\)'
7
- matches = re.findall(pattern, code)
8
- return matches
9
 
10
 
11
  app = FastAPI()
12
 
13
  @app.post("/code")
14
- def run_mojo_code(code:str, filename:str) -> dict:
15
  try:
16
- imports = find_imports(code)
17
  for imported in imports:
18
  subprocess.call(["python3", "-m", "pip", "install", imported], shell=True)
19
- with open(filename, "w") as f:
20
- f.write(code)
21
 
22
- return {"sucess":True, "output": subprocess.check_output(["mojo", filename]).decode("utf-8")}, 200
23
  except:
24
  return {"sucess":False}, 500
 
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):
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