rogerxavier
commited on
Commit
•
b628481
1
Parent(s):
582ad23
Update api.py
Browse files
api.py
CHANGED
@@ -8,6 +8,8 @@ import io
|
|
8 |
import os
|
9 |
from moviepyTest import test
|
10 |
from typing import *
|
|
|
|
|
11 |
|
12 |
app = FastAPI()
|
13 |
|
@@ -47,4 +49,13 @@ async def delete_files(directory: str):
|
|
47 |
os.remove(file_path)
|
48 |
return {"message": f"成功删除{directory}目录下的所有文件"}
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
|
|
8 |
import os
|
9 |
from moviepyTest import test
|
10 |
from typing import *
|
11 |
+
from fastapi.responses import PlainTextResponse #执行其他py的plaintext返回
|
12 |
+
import subprocess
|
13 |
|
14 |
app = FastAPI()
|
15 |
|
|
|
49 |
os.remove(file_path)
|
50 |
return {"message": f"成功删除{directory}目录下的所有文件"}
|
51 |
|
52 |
+
|
53 |
+
@app.get("/execute_py_file/{file_name}")
|
54 |
+
async def execute_py_file(file_name: str):
|
55 |
+
try:
|
56 |
+
result = subprocess.check_output(["python", f"{file_name}.py"]).decode("utf-8")
|
57 |
+
return PlainTextResponse(result)
|
58 |
+
except subprocess.CalledProcessError as e:
|
59 |
+
return PlainTextResponse(f"Error executing {file_name}.py: {e}")
|
60 |
+
|
61 |
|