jonathanjordan21 commited on
Commit
2c6fad5
·
verified ·
1 Parent(s): e2d6933

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -11
app.py CHANGED
@@ -60,19 +60,17 @@ async def run_code(code: Code):
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")
 
60
  # plt.savefig(file_path)
61
  # plt.close()
62
 
63
+ # Get all open figures
64
  figures = [plt.figure(i) for i in plt.get_fignums()]
 
65
 
66
+ # Save all figures in a single PDF using PdfPages
67
+ pdf_filename = "all_graphs.pdf"
68
+ with PdfPages(pdf_filename) as pdf:
69
+ for fig in figures:
70
+ pdf.savefig(fig) # Save each figure as a page in the PDF
71
+ plt.close(fig) # Close the figure to free memory
72
+
73
+ print(f"Saved all figures to {pdf_filename}")
 
74
 
75
 
76
  # return Response(content=img_buffer.getvalue(), media_type="image/png")