-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy patharrays_and_dicts.py
154 lines (116 loc) · 3.33 KB
/
arrays_and_dicts.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "marimo",
# ]
# ///
import marimo
__generated_with = "0.10.6"
app = marimo.App()
@app.cell
def _():
import marimo as mo
import random
return mo, random
@app.cell(hide_code=True)
def _(mo):
mo.md("""# Arrays and Dictionaries""")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
Use `mo.ui.array` and `mo.ui.dictionary` to create UI elements that wrap
other elements.
Because UI elements must be assigned to global variables,
these functions are required when the set of elements to create is not
known until runtime.
"""
)
return
@app.cell
def _(mo):
create = mo.ui.button(label="Create new collections")
return (create,)
@app.cell
def _(create):
create.center()
return
@app.cell
def _(mo):
mo.md("""UI Elements ...""")
return
@app.cell
def _(create, mo, random):
create
array = mo.ui.array(
[mo.ui.text()]
+ [mo.ui.slider(1, 10) for _ in range(0, random.randint(2, 5))],
)
dictionary = mo.ui.dictionary(
{str(i): mo.ui.slider(1, 10) for i in range(0, random.randint(2, 5))}
)
mo.hstack([array, dictionary], justify="space-around")
return array, dictionary
@app.cell
def _(mo):
mo.md("""... and their values""")
return
@app.cell
def _(array, dictionary, mo):
mo.hstack([array.value, dictionary.value], justify="space-around")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
Key difference between marimo dict and standard python dict:
The main reason to use `mo.ui.dictionary` is for reactive execution — when you interact with an element in a `mo.ui.dictionary`, all cells that reference the `mo.ui.dictionary` run automatically, just like all other ui elements. When you use a regular dictionary, you don't get this reactivity.
"""
)
return
@app.cell(hide_code=True)
def _(create, mo):
create
slider = mo.ui.slider(1, 10, show_value=True)
text = mo.ui.text()
date = mo.ui.date()
mo_d = mo.ui.dictionary(
{
"slider": slider,
"text": text,
"date": date,
}
)
py_d = {
"slider": slider,
"text": text,
"date": date,
}
mo.hstack(
[
mo.vstack(["marimo dict", mo_d]),
mo.vstack(["original elements", mo.vstack([slider, text, date])]),
mo.vstack(["python dict", py_d]),
],
justify="space-around",
)
return date, mo_d, py_d, slider, text
@app.cell(hide_code=True)
def _(mo, mo_d, py_d):
mo_d_ref = {k: mo_d[k].value for k in mo_d.value.keys()}
py_d_ref = {k: py_d[k].value for k in py_d.keys()}
mo.hstack(
[
mo.vstack(["reference of marimo dict", mo_d_ref]),
mo.vstack(["reference of python dict", py_d_ref]),
],
justify="space-around",
)
return mo_d_ref, py_d_ref
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""Notice that when you interact with the UI elements in the marimo dict, the reference of marimo dict updates automatically. However, when you interact with the elements in the python dict, you need to manually re-run the cell to see the updated values.""")
return
if __name__ == "__main__":
app.run()