-
Notifications
You must be signed in to change notification settings - Fork 136
/
matplotlib_formatters.py
57 lines (44 loc) · 2 KB
/
matplotlib_formatters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
from marimo._messaging.mimetypes import KnownMimeType
from marimo._output.formatters.formatter_factory import FormatterFactory
class MatplotlibFormatter(FormatterFactory):
@staticmethod
def package_name() -> str:
return "matplotlib"
def register(self) -> None:
import matplotlib # type: ignore
from marimo._runtime.context import (
get_global_context,
runtime_context_installed,
)
get_global_context().set_mpl_installed(True)
from marimo._output import mpl # noqa: F401
if runtime_context_installed():
matplotlib.use("module://marimo._output.mpl")
import base64
import io
from matplotlib.artist import Artist # type: ignore
from matplotlib.container import BarContainer # type: ignore
from marimo._output import formatting
from marimo._output.utils import build_data_url
def mime_data_artist(artist: Artist) -> tuple[KnownMimeType, str]:
buf = io.BytesIO()
artist.figure.savefig(buf, format="png") # type: ignore
mimetype: KnownMimeType = "image/png"
plot_bytes = base64.b64encode(buf.getvalue())
return (
mimetype,
build_data_url(mimetype=mimetype, data=plot_bytes),
)
# monkey-patch a _mime_ method, instead of using a formatter, because
# we want all subclasses of Artist to inherit this renderer.
Artist._mime_ = mime_data_artist # type: ignore[attr-defined]
# use an explicit formatter, no need to try to format subclasses of
# BarContainer
@formatting.formatter(BarContainer)
def _show_bar_container(bc: BarContainer) -> tuple[KnownMimeType, str]:
if len(bc.patches) > 0:
return mime_data_artist(bc.patches[0].figure) # type: ignore
else:
return ("text/plain", str(bc))