jonathanjordan21 commited on
Commit
e2d6933
·
verified ·
1 Parent(s): 9fe7bf2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -49,18 +49,32 @@ async def run_code(code: Code):
49
 
50
  file_path = "graph.pdf"
51
 
52
- if "plt.subplots(" in code.code:
53
- print("SUBPLOTS DETECTED")
54
- exec(code.code + "\nfig.savefig(file_path)\nplt.close()")
55
- else:
56
- print("NO SUBPLOTS")
57
- exec(code.code + "\nplt.savefig(file_path)\nplt.close()")
58
 
59
- plt.close()
60
  # plt.savefig(file_path)
61
  # plt.close()
62
 
63
- # Return image as response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  # return Response(content=img_buffer.getvalue(), media_type="image/png")
65
  # return FileResponse(file_path, media_type="image/png")
66
  return FileResponse(file_path, media_type="application/pdf", filename="graph.pdf")
 
49
 
50
  file_path = "graph.pdf"
51
 
52
+ # if "plt.subplots(" in code.code:
53
+ # print("SUBPLOTS DETECTED")
54
+ # exec(code.code + "\nfig.savefig(file_path)\nplt.close()")
55
+ # else:
56
+ # print("NO SUBPLOTS")
57
+ # exec(code.code + "\nplt.savefig(file_path)\nplt.close()")
58
 
59
+ # plt.close()
60
  # plt.savefig(file_path)
61
  # plt.close()
62
 
63
+ # Get all open figure numbers
64
+ figures = [plt.figure(i) for i in plt.get_fignums()]
65
+
66
+
67
+ # Save all figures in a single PDF
68
+ if figures:
69
+ figures[0].savefig("graph.pdf", format="pdf")
70
+ for fig in figures[1:]:
71
+ fig.savefig("graph.pdf", format="pdf", bbox_inches="tight", append=True)
72
+
73
+ plt.close("all") # Close all figures
74
+ else:
75
+ print("No open figures found.")
76
+
77
+
78
  # return Response(content=img_buffer.getvalue(), media_type="image/png")
79
  # return FileResponse(file_path, media_type="image/png")
80
  return FileResponse(file_path, media_type="application/pdf", filename="graph.pdf")