Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
from fastapi.responses import Response
|
3 |
from fastapi.responses import FileResponse
|
4 |
from pydantic import BaseModel
|
@@ -46,9 +46,16 @@ async def create_ticket(ticket: Ticket):
|
|
46 |
|
47 |
|
48 |
@app.post("/run_code")
|
49 |
-
async def run_code(code: Code):
|
50 |
# img_buffer = io.BytesIO()
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
# img_buffer.seek(0) # Reset buffer position
|
53 |
|
54 |
file_path = f"graphs_{code.project_id}_{code.chain_id}_{code.session_id}.pdf"
|
@@ -79,7 +86,11 @@ async def run_code(code: Code):
|
|
79 |
|
80 |
# return Response(content=img_buffer.getvalue(), media_type="image/png")
|
81 |
# return FileResponse(file_path, media_type="image/png")
|
82 |
-
return FileResponse(file_path, media_type="application/pdf", filename="graph.pdf")
|
|
|
|
|
|
|
|
|
83 |
|
84 |
|
85 |
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
from fastapi.responses import Response
|
3 |
from fastapi.responses import FileResponse
|
4 |
from pydantic import BaseModel
|
|
|
46 |
|
47 |
|
48 |
@app.post("/run_code")
|
49 |
+
async def run_code(code: Code, request: Request):
|
50 |
# img_buffer = io.BytesIO()
|
51 |
+
|
52 |
+
pattern = f"""```python([\s\S]*?)```"""
|
53 |
+
codes = re.findall(pattern, code.code)
|
54 |
+
|
55 |
+
if codes:
|
56 |
+
exec("\n".join(codes))
|
57 |
+
else:
|
58 |
+
exec(code.code)
|
59 |
# img_buffer.seek(0) # Reset buffer position
|
60 |
|
61 |
file_path = f"graphs_{code.project_id}_{code.chain_id}_{code.session_id}.pdf"
|
|
|
86 |
|
87 |
# return Response(content=img_buffer.getvalue(), media_type="image/png")
|
88 |
# return FileResponse(file_path, media_type="image/png")
|
89 |
+
# return FileResponse(file_path, media_type="application/pdf", filename="graph.pdf")
|
90 |
+
return {
|
91 |
+
"message": "Graph created succesfully!",
|
92 |
+
"url": request.url + f"/chart/{code.project_id}/{code.chain_id}/{code.session_id}"
|
93 |
+
}
|
94 |
|
95 |
|
96 |
|