Spaces:
Sleeping
Sleeping
add `init.py` for initial draft structure
Browse files
app.py
CHANGED
|
@@ -1,254 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import marimo
|
| 2 |
|
| 3 |
-
__generated_with = "0.9.
|
| 4 |
app = marimo.App()
|
| 5 |
|
| 6 |
|
| 7 |
-
@app.cell
|
| 8 |
-
def __():
|
| 9 |
-
import marimo as mo
|
| 10 |
-
|
| 11 |
-
mo.md("# Welcome to marimo! ππ")
|
| 12 |
-
return (mo,)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
@app.cell
|
| 16 |
-
def __(mo):
|
| 17 |
-
slider = mo.ui.slider(1, 22)
|
| 18 |
-
return (slider,)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
@app.cell
|
| 22 |
-
def __(mo, slider):
|
| 23 |
-
mo.md(
|
| 24 |
-
f"""
|
| 25 |
-
marimo is a **reactive** Python notebook.
|
| 26 |
-
|
| 27 |
-
This means that unlike traditional notebooks, marimo notebooks **run
|
| 28 |
-
automatically** when you modify them or
|
| 29 |
-
interact with UI elements, like this slider: {slider}.
|
| 30 |
-
|
| 31 |
-
{"##" + "π" * slider.value}
|
| 32 |
-
"""
|
| 33 |
-
)
|
| 34 |
-
return
|
| 35 |
-
|
| 36 |
-
|
| 37 |
@app.cell(hide_code=True)
|
| 38 |
-
def __(
|
| 39 |
-
|
| 40 |
-
{
|
| 41 |
-
"Tip: disabling automatic execution": mo.md(
|
| 42 |
-
rf"""
|
| 43 |
-
marimo lets you disable automatic execution: just go into the
|
| 44 |
-
notebook settings and set
|
| 45 |
-
|
| 46 |
-
"Runtime > On Cell Change" to "lazy".
|
| 47 |
-
|
| 48 |
-
When the runtime is lazy, after running a cell, marimo marks its
|
| 49 |
-
descendants as stale instead of automatically running them. The
|
| 50 |
-
lazy runtime puts you in control over when cells are run, while
|
| 51 |
-
still giving guarantees about the notebook state.
|
| 52 |
-
"""
|
| 53 |
-
)
|
| 54 |
-
}
|
| 55 |
-
)
|
| 56 |
return
|
| 57 |
|
| 58 |
|
| 59 |
@app.cell(hide_code=True)
|
| 60 |
def __(mo):
|
| 61 |
mo.md(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
"""
|
| 63 |
-
|
| 64 |
-
by entering `marimo edit` at the command line.
|
| 65 |
-
"""
|
| 66 |
-
).callout()
|
| 67 |
return
|
| 68 |
|
| 69 |
|
| 70 |
@app.cell(hide_code=True)
|
| 71 |
def __(mo):
|
| 72 |
mo.md(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
"""
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
A marimo notebook is made up of small blocks of Python code called
|
| 77 |
-
cells.
|
| 78 |
-
|
| 79 |
-
marimo reads your cells and models the dependencies among them: whenever
|
| 80 |
-
a cell that defines a global variable is run, marimo
|
| 81 |
-
**automatically runs** all cells that reference that variable.
|
| 82 |
-
|
| 83 |
-
Reactivity keeps your program state and outputs in sync with your code,
|
| 84 |
-
making for a dynamic programming environment that prevents bugs before they
|
| 85 |
-
happen.
|
| 86 |
-
"""
|
| 87 |
-
)
|
| 88 |
return
|
| 89 |
|
| 90 |
|
| 91 |
@app.cell(hide_code=True)
|
| 92 |
-
def __(
|
| 93 |
-
|
|
|
|
| 94 |
mo.md(
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
**reacted** by running this cell automatically, because this cell
|
| 100 |
-
references the global variable `changed`.
|
| 101 |
|
| 102 |
-
Reactivity ensures that your notebook state is always
|
| 103 |
-
consistent, which is crucial for doing good science; it's also what
|
| 104 |
-
enables marimo notebooks to double as tools and apps.
|
| 105 |
"""
|
| 106 |
)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
)
|
|
|
|
| 114 |
)
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
@app.cell
|
| 119 |
-
def __():
|
| 120 |
-
changed = False
|
| 121 |
-
return (changed,)
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
@app.cell(hide_code=True)
|
| 125 |
-
def __(mo):
|
| 126 |
-
mo.accordion(
|
| 127 |
-
{
|
| 128 |
-
"Tip: execution order": (
|
| 129 |
-
"""
|
| 130 |
-
The order of cells on the page has no bearing on
|
| 131 |
-
the order in which cells are executed: marimo knows that a cell
|
| 132 |
-
reading a variable must run after the cell that defines it. This
|
| 133 |
-
frees you to organize your code in the way that makes the most
|
| 134 |
-
sense for you.
|
| 135 |
-
"""
|
| 136 |
-
)
|
| 137 |
-
}
|
| 138 |
-
)
|
| 139 |
-
return
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
@app.cell(hide_code=True)
|
| 143 |
-
def __(mo):
|
| 144 |
-
mo.md(
|
| 145 |
-
"""
|
| 146 |
-
**Global names must be unique.** To enable reactivity, marimo imposes a
|
| 147 |
-
constraint on how names appear in cells: no two cells may define the same
|
| 148 |
-
variable.
|
| 149 |
-
"""
|
| 150 |
-
)
|
| 151 |
-
return
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
@app.cell(hide_code=True)
|
| 155 |
-
def __(mo):
|
| 156 |
-
mo.accordion(
|
| 157 |
-
{
|
| 158 |
-
"Tip: encapsulation": (
|
| 159 |
-
"""
|
| 160 |
-
By encapsulating logic in functions, classes, or Python modules,
|
| 161 |
-
you can minimize the number of global variables in your notebook.
|
| 162 |
-
"""
|
| 163 |
-
)
|
| 164 |
-
}
|
| 165 |
-
)
|
| 166 |
-
return
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
@app.cell(hide_code=True)
|
| 170 |
-
def __(mo):
|
| 171 |
-
mo.accordion(
|
| 172 |
-
{
|
| 173 |
-
"Tip: private variables": (
|
| 174 |
-
"""
|
| 175 |
-
Variables prefixed with an underscore are "private" to a cell, so
|
| 176 |
-
they can be defined by multiple cells.
|
| 177 |
-
"""
|
| 178 |
-
)
|
| 179 |
-
}
|
| 180 |
-
)
|
| 181 |
-
return
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
@app.cell(hide_code=True)
|
| 185 |
-
def __(mo):
|
| 186 |
-
mo.md(
|
| 187 |
-
"""
|
| 188 |
-
## 2. UI elements
|
| 189 |
-
|
| 190 |
-
Cells can output interactive UI elements. Interacting with a UI
|
| 191 |
-
element **automatically triggers notebook execution**: when
|
| 192 |
-
you interact with a UI element, its value is sent back to Python, and
|
| 193 |
-
every cell that references that element is re-run.
|
| 194 |
-
|
| 195 |
-
marimo provides a library of UI elements to choose from under
|
| 196 |
-
`marimo.ui`.
|
| 197 |
-
"""
|
| 198 |
-
)
|
| 199 |
-
return
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
@app.cell
|
| 203 |
-
def __(mo):
|
| 204 |
-
mo.md("""**π Some UI elements.** Try interacting with the below elements.""")
|
| 205 |
-
return
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
@app.cell
|
| 209 |
-
def __(mo):
|
| 210 |
-
icon = mo.ui.dropdown(["π", "π", "β¨"], value="π")
|
| 211 |
-
return (icon,)
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
@app.cell
|
| 215 |
-
def __(icon, mo):
|
| 216 |
-
repetitions = mo.ui.slider(1, 16, label=f"number of {icon.value}: ")
|
| 217 |
-
return (repetitions,)
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
@app.cell
|
| 221 |
-
def __(icon, repetitions):
|
| 222 |
-
icon, repetitions
|
| 223 |
-
return
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
@app.cell
|
| 227 |
-
def __(icon, mo, repetitions):
|
| 228 |
-
mo.md("# " + icon.value * repetitions.value)
|
| 229 |
-
return
|
| 230 |
|
| 231 |
|
| 232 |
@app.cell(hide_code=True)
|
| 233 |
def __(mo):
|
| 234 |
mo.md(
|
| 235 |
-
"""
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
marimo cells parse Python (and only Python), and marimo notebooks are
|
| 239 |
-
stored as pure Python files β outputs are _not_ included. There's no
|
| 240 |
-
magical syntax.
|
| 241 |
|
| 242 |
-
|
|
|
|
|
|
|
| 243 |
|
| 244 |
-
|
| 245 |
-
- legible for both humans and machines
|
| 246 |
-
- formattable using your tool of choice,
|
| 247 |
-
- usable as Python scripts, with UI elements taking their default
|
| 248 |
-
values, and
|
| 249 |
-
- importable by other modules (more on that in the future).
|
| 250 |
"""
|
| 251 |
-
)
|
| 252 |
return
|
| 253 |
|
| 254 |
|
|
@@ -256,215 +113,208 @@ def __(mo):
|
|
| 256 |
def __(mo):
|
| 257 |
mo.md(
|
| 258 |
"""
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
marimo notebooks can double as apps. Click the app window icon in the
|
| 262 |
-
bottom-right to see this notebook in "app view."
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
notebooking, without ever making apps.
|
| 267 |
"""
|
| 268 |
)
|
| 269 |
return
|
| 270 |
|
| 271 |
|
| 272 |
@app.cell(hide_code=True)
|
| 273 |
-
def __(
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
```
|
| 291 |
-
marimo run notebook.py
|
| 292 |
-
```
|
| 293 |
-
|
| 294 |
-
to start a webserver that serves your notebook as an app in read-only mode,
|
| 295 |
-
with code cells hidden.
|
| 296 |
-
|
| 297 |
-
**Convert a Jupyter notebook.** Convert a Jupyter notebook to a marimo
|
| 298 |
-
notebook using `marimo convert`:
|
| 299 |
-
|
| 300 |
-
```
|
| 301 |
-
marimo convert your_notebook.ipynb > your_app.py
|
| 302 |
-
```
|
| 303 |
-
|
| 304 |
-
**Tutorials.** marimo comes packaged with tutorials:
|
| 305 |
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
- `sql`: how to use SQL
|
| 312 |
-
- `layout`: layout elements in marimo
|
| 313 |
-
- `fileformat`: how marimo's file format works
|
| 314 |
-
- `markdown-format`: for using `.md` files in marimo
|
| 315 |
-
- `for-jupyter-users`: if you are coming from Jupyter
|
| 316 |
|
| 317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
-
|
| 324 |
-
[our GitHub repo](https://www.github.com/marimo-team/marimo/tree/main/examples).
|
| 325 |
"""
|
| 326 |
-
)
|
| 327 |
-
return
|
| 328 |
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
"""
|
| 338 |
-
)
|
| 339 |
-
return
|
| 340 |
|
|
|
|
| 341 |
|
| 342 |
-
|
| 343 |
-
def __(mo, tips):
|
| 344 |
-
mo.accordion(tips)
|
| 345 |
-
return
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
@app.cell(hide_code=True)
|
| 349 |
-
def __(mo):
|
| 350 |
-
mo.md("""## Finally, a fun fact""")
|
| 351 |
-
return
|
| 352 |
|
| 353 |
|
| 354 |
@app.cell(hide_code=True)
|
| 355 |
-
def __(
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
)
|
| 364 |
-
return
|
| 365 |
|
| 366 |
|
| 367 |
@app.cell(hide_code=True)
|
| 368 |
def __():
|
| 369 |
-
|
| 370 |
-
"Saving": (
|
| 371 |
-
"""
|
| 372 |
-
**Saving**
|
| 373 |
-
|
| 374 |
-
- _Name_ your app using the box at the top of the screen, or
|
| 375 |
-
with `Ctrl/Cmd+s`. You can also create a named app at the
|
| 376 |
-
command line, e.g., `marimo edit app_name.py`.
|
| 377 |
-
|
| 378 |
-
- _Save_ by clicking the save icon on the bottom right, or by
|
| 379 |
-
inputting `Ctrl/Cmd+s`. By default marimo is configured
|
| 380 |
-
to autosave.
|
| 381 |
-
"""
|
| 382 |
-
),
|
| 383 |
-
"Running": (
|
| 384 |
-
"""
|
| 385 |
-
1. _Run a cell_ by clicking the play ( β· ) button on the top
|
| 386 |
-
right of a cell, or by inputting `Ctrl/Cmd+Enter`.
|
| 387 |
-
|
| 388 |
-
2. _Run a stale cell_ by clicking the yellow run button on the
|
| 389 |
-
right of the cell, or by inputting `Ctrl/Cmd+Enter`. A cell is
|
| 390 |
-
stale when its code has been modified but not run.
|
| 391 |
-
|
| 392 |
-
3. _Run all stale cells_ by clicking the play ( β· ) button on
|
| 393 |
-
the bottom right of the screen, or input `Ctrl/Cmd+Shift+r`.
|
| 394 |
-
"""
|
| 395 |
-
),
|
| 396 |
-
"Console Output": (
|
| 397 |
-
"""
|
| 398 |
-
Console output (e.g., `print()` statements) is shown below a
|
| 399 |
-
cell.
|
| 400 |
-
"""
|
| 401 |
-
),
|
| 402 |
-
"Creating, Moving, and Deleting Cells": (
|
| 403 |
-
"""
|
| 404 |
-
1. _Create_ a new cell above or below a given one by clicking
|
| 405 |
-
the plus button to the left of the cell, which appears on
|
| 406 |
-
mouse hover.
|
| 407 |
-
|
| 408 |
-
2. _Move_ a cell up or down by dragging on the handle to the
|
| 409 |
-
right of the cell, which appears on mouse hover.
|
| 410 |
-
|
| 411 |
-
3. _Delete_ a cell by clicking the trash bin icon. Bring it
|
| 412 |
-
back by clicking the undo button on the bottom right of the
|
| 413 |
-
screen, or with `Ctrl/Cmd+Shift+z`.
|
| 414 |
-
"""
|
| 415 |
-
),
|
| 416 |
-
"Disabling Automatic Execution": (
|
| 417 |
-
"""
|
| 418 |
-
Via the notebook settings (gear icon) or footer panel, you
|
| 419 |
-
can disable automatic execution. This is helpful when
|
| 420 |
-
working with expensive notebooks or notebooks that have
|
| 421 |
-
side-effects like database transactions.
|
| 422 |
-
"""
|
| 423 |
-
),
|
| 424 |
-
"Disabling Cells": (
|
| 425 |
-
"""
|
| 426 |
-
You can disable a cell via the cell context menu.
|
| 427 |
-
marimo will never run a disabled cell or any cells that depend on it.
|
| 428 |
-
This can help prevent accidental execution of expensive computations
|
| 429 |
-
when editing a notebook.
|
| 430 |
-
"""
|
| 431 |
-
),
|
| 432 |
-
"Code Folding": (
|
| 433 |
-
"""
|
| 434 |
-
You can collapse or fold the code in a cell by clicking the arrow
|
| 435 |
-
icons in the line number column to the left, or by using keyboard
|
| 436 |
-
shortcuts.
|
| 437 |
|
| 438 |
-
|
| 439 |
-
quickly fold or unfold all cells.
|
| 440 |
-
"""
|
| 441 |
-
),
|
| 442 |
-
"Code Formatting": (
|
| 443 |
-
"""
|
| 444 |
-
If you have [ruff](https://github.com/astral-sh/ruff) installed,
|
| 445 |
-
you can format a cell with the keyboard shortcut `Ctrl/Cmd+b`.
|
| 446 |
-
"""
|
| 447 |
-
),
|
| 448 |
-
"Command Palette": (
|
| 449 |
-
"""
|
| 450 |
-
Use `Ctrl/Cmd+k` to open the command palette.
|
| 451 |
-
"""
|
| 452 |
-
),
|
| 453 |
-
"Keyboard Shortcuts": (
|
| 454 |
-
"""
|
| 455 |
-
Open the notebook menu (top-right) or input `Ctrl/Cmd+Shift+h` to
|
| 456 |
-
view a list of all keyboard shortcuts.
|
| 457 |
-
"""
|
| 458 |
-
),
|
| 459 |
-
"Configuration": (
|
| 460 |
-
"""
|
| 461 |
-
Configure the editor by clicking the gears icon near the top-right
|
| 462 |
-
of the screen.
|
| 463 |
-
"""
|
| 464 |
-
),
|
| 465 |
-
}
|
| 466 |
-
return (tips,)
|
| 467 |
|
| 468 |
|
| 469 |
if __name__ == "__main__":
|
| 470 |
-
app.run()
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# dependencies = [
|
| 3 |
+
# "marimo",
|
| 4 |
+
# "anywidget==0.9.13",
|
| 5 |
+
# "traitlets==5.14.3",
|
| 6 |
+
# ]
|
| 7 |
+
# ///
|
| 8 |
+
"""
|
| 9 |
+
This is the starting point for your notebook.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
import marimo
|
| 13 |
|
| 14 |
+
__generated_with = "0.9.10"
|
| 15 |
app = marimo.App()
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@app.cell(hide_code=True)
|
| 19 |
+
def __(header_widget):
|
| 20 |
+
header_widget
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return
|
| 22 |
|
| 23 |
|
| 24 |
@app.cell(hide_code=True)
|
| 25 |
def __(mo):
|
| 26 |
mo.md(
|
| 27 |
+
r"""
|
| 28 |
+
First of all, create a header for your notebook.
|
| 29 |
+
|
| 30 |
+
We've designed a `HeaderWidget` for you to display important information.
|
| 31 |
+
|
| 32 |
+
To use `HeaderWidget`, you need to create an instance of it. You can pass a dictionary containing key-value pairs that represent the information you want to display in the header.
|
| 33 |
+
|
| 34 |
+
For example:
|
| 35 |
+
|
| 36 |
+
```python
|
| 37 |
+
header_widget = HeaderWidget(
|
| 38 |
+
result={
|
| 39 |
+
"Title": "Comprehensive E-Commerce Customer Behavior Analysis",
|
| 40 |
+
"Author": '<a href="https://github.com/Haleshot/marimo-tutorials">Dr. Jane Smith, PhD</a>',
|
| 41 |
+
"Version": "1.2.3",
|
| 42 |
+
"Description": "This advanced notebook presents a multi-faceted analysis of <b>customer behavior patterns</b> across various e-commerce platforms. The primary goal is to derive actionable insights that can significantly enhance customer engagement, optimize conversion rates, and ultimately drive business growth in the competitive e-commerce landscape.",
|
| 43 |
+
"Keywords": "E-Commerce Analytics, Customer Behavior Modeling, Predictive Analytics, Machine Learning, Natural Language Processing, Data Visualization, Time Series Analysis",
|
| 44 |
+
"Data Sources": "1. Customer transaction logs (5 years, 10M+ records)<br>2. Website clickstream data (real-time, 1B+ events)<br>3. CRM records (customer demographics, purchase history)<br>4. Social media interactions (Twitter, Facebook, Instagram)<br>5. Customer support tickets and chat logs<br>6. Product catalog and inventory data",
|
| 45 |
+
"Last Updated": "November 3, 2024",
|
| 46 |
+
}
|
| 47 |
+
)
|
| 48 |
+
```
|
| 49 |
"""
|
| 50 |
+
)
|
|
|
|
|
|
|
|
|
|
| 51 |
return
|
| 52 |
|
| 53 |
|
| 54 |
@app.cell(hide_code=True)
|
| 55 |
def __(mo):
|
| 56 |
mo.md(
|
| 57 |
+
r"""
|
| 58 |
+
**Some tips from our experience with marimo**
|
| 59 |
+
|
| 60 |
+
1. If a notebook contains any io operation, e.g., reading an external .csv file, you'd better use a `marimo.ui.form` for users to config the path of this .csv file.
|
| 61 |
+
2. You can create more ui components and appealing contents with pure html, [anywidget](https://github.com/manzt/anywidget) and more. But when you are doing this, remember to check its appearance under both light and dark themes, and different widths.
|
| 62 |
+
3. Albeit you can create local variables in a cell with a prefix "_", we recommend you do this as little as possible because the `Explore variables` panel will neglect these variables, making debug these variables hard.
|
| 63 |
+
4. If you want your notebook to run properly in our cloud, please check whether the dependencies are supported by wasm. Some popular libraries like `polars` and `openai`, for example, are not supported.
|
| 64 |
+
5. Attach as few assets as possible, we want to keep our repo lightweight.
|
| 65 |
+
6. Functional thinking are preferred in marimo since instances are immutable.
|
| 66 |
"""
|
| 67 |
+
).callout(kind="info")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
return
|
| 69 |
|
| 70 |
|
| 71 |
@app.cell(hide_code=True)
|
| 72 |
+
def __(mo):
|
| 73 |
+
# Custom Constants
|
| 74 |
+
custom_form = (
|
| 75 |
mo.md(
|
| 76 |
+
r"""
|
| 77 |
+
**Customize your constants here:**
|
| 78 |
|
| 79 |
+
{image}
|
|
|
|
|
|
|
| 80 |
|
|
|
|
|
|
|
|
|
|
| 81 |
"""
|
| 82 |
)
|
| 83 |
+
.batch(
|
| 84 |
+
image=mo.ui.text(
|
| 85 |
+
value="../assets/<>/<>",
|
| 86 |
+
label="Path of your data: ",
|
| 87 |
+
full_width=True,
|
| 88 |
+
), ## add more rows below
|
| 89 |
)
|
| 90 |
+
.form(bordered=True, label="Custom Constants")
|
| 91 |
)
|
| 92 |
+
custom_form
|
| 93 |
+
return (custom_form,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
@app.cell(hide_code=True)
|
| 97 |
def __(mo):
|
| 98 |
mo.md(
|
| 99 |
+
r"""
|
| 100 |
+
You can access the value of the form above with:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
```python
|
| 103 |
+
custom_form.value['image']
|
| 104 |
+
```
|
| 105 |
|
| 106 |
+
after the submission.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
"""
|
| 108 |
+
).callout(kind="info")
|
| 109 |
return
|
| 110 |
|
| 111 |
|
|
|
|
| 113 |
def __(mo):
|
| 114 |
mo.md(
|
| 115 |
"""
|
| 116 |
+
<h1 id="refs">References</h1>
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
- [Reference 1](https://example.com)
|
| 119 |
+
- [Reference 2](https://example.com)
|
|
|
|
| 120 |
"""
|
| 121 |
)
|
| 122 |
return
|
| 123 |
|
| 124 |
|
| 125 |
@app.cell(hide_code=True)
|
| 126 |
+
def __():
|
| 127 |
+
import anywidget
|
| 128 |
+
import traitlets
|
| 129 |
+
|
| 130 |
+
class HeaderWidget(anywidget.AnyWidget):
|
| 131 |
+
_esm = """
|
| 132 |
+
function escapeHTML(str) {
|
| 133 |
+
return str.replace(/[&<>'"]/g,
|
| 134 |
+
tag => ({
|
| 135 |
+
'&': '&',
|
| 136 |
+
'<': '<',
|
| 137 |
+
'>': '>',
|
| 138 |
+
"'": ''',
|
| 139 |
+
'"': '"'
|
| 140 |
+
}[tag] || tag)
|
| 141 |
+
);
|
| 142 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
function stripHTML(html) {
|
| 145 |
+
const tmp = document.createElement("DIV");
|
| 146 |
+
tmp.innerHTML = html;
|
| 147 |
+
return tmp.textContent || tmp.innerText || "";
|
| 148 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
function renderValue(value) {
|
| 151 |
+
if (typeof value !== 'string') {
|
| 152 |
+
return escapeHTML(String(value));
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
const isHTML = /<[a-z][\s\S]*>/i.test(value);
|
| 156 |
+
const strippedValue = isHTML ? stripHTML(value) : value;
|
| 157 |
+
|
| 158 |
+
if (strippedValue.length > 100) {
|
| 159 |
+
if (isHTML) {
|
| 160 |
+
return `
|
| 161 |
+
<div class="preview">${value.substring(0, 100)}...</div>
|
| 162 |
+
<div class="full-text" style="display: none;">${value}</div>
|
| 163 |
+
<button class="toggle-button">Show More</button>
|
| 164 |
+
`;
|
| 165 |
+
} else {
|
| 166 |
+
return `
|
| 167 |
+
<div class="preview">${escapeHTML(value.substring(0, 100))}...</div>
|
| 168 |
+
<div class="full-text" style="display: none;">${escapeHTML(value)}</div>
|
| 169 |
+
<button class="toggle-button">Show More</button>
|
| 170 |
+
`;
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
return isHTML ? value : escapeHTML(value);
|
| 175 |
+
}
|
| 176 |
|
| 177 |
+
function render({ model, el }) {
|
| 178 |
+
const result = model.get("result");
|
| 179 |
+
const container = document.createElement("div");
|
| 180 |
+
container.className = "header-container";
|
| 181 |
+
|
| 182 |
+
container.innerHTML = `
|
| 183 |
+
<img class="banner" src="https://raw.githubusercontent.com/Haleshot/marimo-tutorials/main/community-tutorials-banner.png" alt="Marimo Banner">
|
| 184 |
+
<div class="form-container">
|
| 185 |
+
${Object.entries(result).map(([key, value]) => `
|
| 186 |
+
<div class="form-row">
|
| 187 |
+
<label>${escapeHTML(key)}</label>
|
| 188 |
+
<div class="value-container">
|
| 189 |
+
${renderValue(value)}
|
| 190 |
+
</div>
|
| 191 |
+
</div>
|
| 192 |
+
`).join('')}
|
| 193 |
+
</div>
|
| 194 |
+
`;
|
| 195 |
+
|
| 196 |
+
el.appendChild(container);
|
| 197 |
+
|
| 198 |
+
container.querySelectorAll('.toggle-button').forEach(button => {
|
| 199 |
+
button.addEventListener('click', () => {
|
| 200 |
+
const row = button.closest('.form-row');
|
| 201 |
+
const preview = row.querySelector('.preview');
|
| 202 |
+
const fullText = row.querySelector('.full-text');
|
| 203 |
+
|
| 204 |
+
if (fullText.style.display === "none") {
|
| 205 |
+
fullText.style.display = "block";
|
| 206 |
+
preview.style.display = "none";
|
| 207 |
+
button.textContent = "Show Less";
|
| 208 |
+
} else {
|
| 209 |
+
fullText.style.display = "none";
|
| 210 |
+
preview.style.display = "block";
|
| 211 |
+
button.textContent = "Show More";
|
| 212 |
+
}
|
| 213 |
+
});
|
| 214 |
+
});
|
| 215 |
+
}
|
| 216 |
|
| 217 |
+
export default { render };
|
|
|
|
| 218 |
"""
|
|
|
|
|
|
|
| 219 |
|
| 220 |
+
_css = """
|
| 221 |
+
.header-container {
|
| 222 |
+
font-family: 'Helvetica Neue', Arial, sans-serif;
|
| 223 |
+
max-width: 100%;
|
| 224 |
+
margin: 0 auto;
|
| 225 |
+
overflow: hidden;
|
| 226 |
+
}
|
| 227 |
+
.banner {
|
| 228 |
+
width: 100%;
|
| 229 |
+
height: 200px;
|
| 230 |
+
object-fit: cover;
|
| 231 |
+
border-radius: 10px 10px 0 0;
|
| 232 |
+
display: block;
|
| 233 |
+
}
|
| 234 |
+
.form-container {
|
| 235 |
+
padding: 30px;
|
| 236 |
+
display: grid;
|
| 237 |
+
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| 238 |
+
gap: 20px;
|
| 239 |
+
font-weight: 300;
|
| 240 |
+
box-shadow: 0 -10px 20px rgba(0,0,0,0.1);
|
| 241 |
+
}
|
| 242 |
+
.form-row {
|
| 243 |
+
display: flex;
|
| 244 |
+
flex-direction: column;
|
| 245 |
+
}
|
| 246 |
+
label {
|
| 247 |
+
font-size: 0.8em;
|
| 248 |
+
text-transform: uppercase;
|
| 249 |
+
letter-spacing: 1px;
|
| 250 |
+
margin-bottom: 5px;
|
| 251 |
+
font-weight: 500;
|
| 252 |
+
}
|
| 253 |
+
.value-container {
|
| 254 |
+
font-size: 1em;
|
| 255 |
+
line-height: 1.5;
|
| 256 |
+
}
|
| 257 |
+
.value-container a {
|
| 258 |
+
color: #0066cc;
|
| 259 |
+
text-decoration: none;
|
| 260 |
+
transition: color 0.2s ease;
|
| 261 |
+
}
|
| 262 |
+
.value-container a:hover {
|
| 263 |
+
color: #003366;
|
| 264 |
+
}
|
| 265 |
+
.preview, .full-text {
|
| 266 |
+
margin-bottom: 10px;
|
| 267 |
+
}
|
| 268 |
+
.toggle-button {
|
| 269 |
+
border: none;
|
| 270 |
+
border-radius: 20px;
|
| 271 |
+
padding: 8px 16px;
|
| 272 |
+
font-size: 0.9em;
|
| 273 |
+
cursor: pointer;
|
| 274 |
+
transition: all 0.3s ease;
|
| 275 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 276 |
+
}
|
| 277 |
+
.toggle-button:hover {
|
| 278 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.15);
|
| 279 |
+
}
|
| 280 |
+
@media (max-width: 600px) {
|
| 281 |
+
.form-container {
|
| 282 |
+
grid-template-columns: 1fr;
|
| 283 |
+
}
|
| 284 |
+
}
|
| 285 |
"""
|
|
|
|
|
|
|
| 286 |
|
| 287 |
+
result = traitlets.Dict({}).tag(sync=True)
|
| 288 |
|
| 289 |
+
return HeaderWidget, anywidget, traitlets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
|
| 292 |
@app.cell(hide_code=True)
|
| 293 |
+
def __(HeaderWidget):
|
| 294 |
+
header_widget = HeaderWidget(
|
| 295 |
+
result={
|
| 296 |
+
"Title": "Comprehensive E-Commerce Customer Behavior Analysis",
|
| 297 |
+
"Author": '<a href="https://github.com/Haleshot/marimo-tutorials">Dr. Jane Smith, PhD</a>',
|
| 298 |
+
"Affiliation": '<a href="https://www.datascience.university.edu">University of Data Science</a>',
|
| 299 |
+
"Version": "1.2.3",
|
| 300 |
+
"Description": "This advanced notebook presents a multi-faceted analysis of <b>customer behavior patterns</b> across various e-commerce platforms. The primary goal is to derive actionable insights that can significantly enhance customer engagement, optimize conversion rates, and ultimately drive business growth in the competitive e-commerce landscape.",
|
| 301 |
+
"Keywords": "E-Commerce Analytics, Customer Behavior Modeling, Predictive Analytics, Machine Learning, Natural Language Processing, Data Visualization, Time Series Analysis",
|
| 302 |
+
"Data Sources": "1. Customer transaction logs (5 years, 10M+ records)<br>2. Website clickstream data (real-time, 1B+ events)<br>3. CRM records (customer demographics, purchase history)<br>4. Social media interactions (Twitter, Facebook, Instagram)<br>5. Customer support tickets and chat logs<br>6. Product catalog and inventory data",
|
| 303 |
+
"Prerequisites": "Intermediate to advanced knowledge in statistics, machine learning, and Python programming. Familiarity with e-commerce concepts and business metrics is beneficial.",
|
| 304 |
+
"Acknowledgements": "This work was supported by a grant from the National Science Foundation (NSF-1234567). Special thanks to the E-Commerce Research Consortium for providing anonymized datasets.",
|
| 305 |
+
"License": '<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>',
|
| 306 |
+
"Last Updated": "November 3, 2024",
|
| 307 |
+
}
|
| 308 |
)
|
| 309 |
+
return (header_widget,)
|
| 310 |
|
| 311 |
|
| 312 |
@app.cell(hide_code=True)
|
| 313 |
def __():
|
| 314 |
+
import marimo as mo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
+
return (mo,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
|
| 319 |
if __name__ == "__main__":
|
| 320 |
+
app.run()
|