| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | """ |
| 8 | A custom Matplotlib backend that overrides the show method to return image bytes. |
| 9 | """ |
| 10 | |
| 11 | import base64 |
| 12 | import io |
| 13 | import json as _json |
| 14 | |
| 15 | import matplotlib |
| 16 | from matplotlib.backend_bases import FigureManagerBase |
| 17 | |
| 18 | # Import necessary components from Matplotlib |
| 19 | from matplotlib.backends.backend_agg import FigureCanvasAgg |
| 20 | |
| 21 | |
| 22 | class CustomFigureCanvas(FigureCanvasAgg): |
| 23 | def show(self): |
| 24 | # Save the figure to a BytesIO object |
| 25 | buf = io.BytesIO() |
| 26 | self.print_png(buf) |
| 27 | image_bytes = buf.getvalue() |
| 28 | buf.close() |
| 29 | return image_bytes |
| 30 | |
| 31 | |
| 32 | class CustomFigureManager(FigureManagerBase): |
| 33 | def __init__(self, canvas, num): |
| 34 | super().__init__(canvas, num) |
| 35 | |
| 36 | |
| 37 | # Mimic module initialization that integrates with the Matplotlib backend system |
| 38 | def _create_figure_manager(num, *args, **kwargs): |
| 39 | """ |
| 40 | Create a custom figure manager instance. |
| 41 | """ |
| 42 | FigureClass = kwargs.pop("FigureClass", None) # noqa: N806 |
| 43 | if FigureClass is None: |
| 44 | from matplotlib.figure import Figure |
| 45 | |
| 46 | FigureClass = Figure # noqa: N806 |
| 47 | fig = FigureClass(*args, **kwargs) |
| 48 | canvas = CustomFigureCanvas(fig) |
| 49 | manager = CustomFigureManager(canvas, num) |
| 50 | return manager |
| 51 | |
| 52 | |
| 53 | def show(): |
| 54 | """ |
| 55 | Handle all figures and potentially return their images as bytes. |
| 56 | |
| 57 | This function iterates over all figures registered with the custom backend, |
| 58 | renders them as images in bytes format, and could return a list of bytes objects, |
| 59 | one for each figure, or handle them as needed. |
| 60 | """ |
| 61 | image_data = [] |
| 62 | for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers(): |
| 63 | # Get the figure from the manager |
| 64 | fig = manager.canvas.figure |
| 65 | buf = io.BytesIO() # Create a buffer for the figure |
| 66 | fig.savefig(buf, format="png") # Save the figure to the buffer in PNG format |
| 67 | buf.seek(0) # Go to the beginning of the buffer |
| 68 | image_bytes = buf.getvalue() # Retrieve bytes value |
| 69 | image_base64 = base64.b64encode(image_bytes).decode("utf-8") |
| 70 | image_data.append({"image_base64": image_base64}) |
| 71 | buf.close() |
| 72 | |
| 73 | req_con, resp_con = _open_connections() |
| 74 | |
| 75 | _json_dump = _json.dumps( |
| 76 | { |
| 77 | "type": "matplotlib", |
| 78 | "image_data": image_data, |
| 79 | } |
| 80 | ) |
| 81 | req_con.send_bytes(_json_dump.encode("utf-8")) |
| 82 | resp = _json.loads(resp_con.recv_bytes().decode("utf-8")) |
| 83 | print(resp) |
| 84 | |
| 85 | |
| 86 | FigureCanvas = CustomFigureCanvas |
| 87 | FigureManager = CustomFigureManager |