-
Notifications
You must be signed in to change notification settings - Fork 131
/
displaying_objects.md
86 lines (63 loc) · 2.44 KB
/
displaying_objects.md
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Richly display objects
marimo has built-in rich representations of many objects, including native
Python objects like lists and dicts as well as marimo objects like [UI
elements](/guides/interactivity.md) and libraries, including matplotlib,
seaborn, Plotly, altair pandas, and more. These rich representations are
displayed for the last expression of a cell, or when using
[`mo.output.append`](#marimo.output.append).
You can register rich displays with marimo for your own objects.
### Option 1: Implement a `_mime_` method
When displaying an object, marimo's media viewer checks for the presence of a
method called `_mime_`. This method should take no arguments and return
a tuple of two strings, the [mime type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) and data to be displayed.
**Examples.**
```{eval-rst}
.. marimo-embed::
:size: medium
:mode: edit
@app.cell(hide_code=True)
def __():
mo.md("**JSON**")
@app.cell
def __():
import json
class MyJSONObject(object):
def __init__(self, data: dict[str, object]) -> None:
self.data = data
def _mime_(self) -> tuple[str, str]:
return ("application/json", json.dumps(self.data))
MyJSONObject({"hello": "world"})
@app.cell(hide_code=True)
def __():
mo.md("**HTML**")
@app.cell
def __():
class Colorize(object):
def __init__(self, text: str) -> None:
self.text = text
def _mime_(self) -> tuple[str, str]:
return (
"text/html",
"<span style='color:red'>" + self.text + "</span>",
)
Colorize("Hello!")
@app.cell(hide_code=True)
def __():
mo.md("**Image**")
@app.cell
def __():
class Image(object):
def __init__(self, url: str) -> None:
self.url = url
def _mime_(self) -> tuple[str, str]:
return ("image/png", self.url)
Image("https://raw.githubusercontent.com/marimo-team/marimo/main/docs/_static/marimo-logotype-thick.svg")
```
### Option 2: Add a formatter to the marimo repo
When you don't have the ability to implement a `_mime_` method on the type
you want displayed, you can register custom formatters with marimo.
We welcome contributors to add formatters to the marimo codebase. [Look at our
codebase for
examples](https://github.com/marimo-team/marimo/tree/main/marimo/_output/formatters),
then open a pull request.
[AnyWidget](/api/inputs/anywidget.md)