-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.py
320 lines (272 loc) · 11.6 KB
/
init.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# /// script
# dependencies = [
# "marimo",
# "anywidget==0.9.13",
# "traitlets==5.14.3",
# ]
# ///
"""
This is the starting point for your notebook.
"""
import marimo
__generated_with = "0.9.10"
app = marimo.App()
@app.cell(hide_code=True)
def __(header_widget):
header_widget
return
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
First of all, create a header for your notebook.
We've designed a `HeaderWidget` for you to display important information.
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.
For example:
```python
header_widget = HeaderWidget(
result={
"Title": "Comprehensive E-Commerce Customer Behavior Analysis",
"Author": '<a href="https://github.com/Haleshot/marimo-tutorials">Dr. Jane Smith, PhD</a>',
"Version": "1.2.3",
"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.",
"Keywords": "E-Commerce Analytics, Customer Behavior Modeling, Predictive Analytics, Machine Learning, Natural Language Processing, Data Visualization, Time Series Analysis",
"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",
"Last Updated": "November 3, 2024",
}
)
```
"""
)
return
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
**Some tips from our experience with marimo**
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.
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.
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.
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.
5. Attach as few assets as possible, we want to keep our repo lightweight.
6. Functional thinking are preferred in marimo since instances are immutable.
"""
).callout(kind="info")
return
@app.cell(hide_code=True)
def __(mo):
# Custom Constants
custom_form = (
mo.md(
r"""
**Customize your constants here:**
{image}
"""
)
.batch(
image=mo.ui.text(
value="../assets/<>/<>",
label="Path of your data: ",
full_width=True,
), ## add more rows below
)
.form(bordered=True, label="Custom Constants")
)
custom_form
return (custom_form,)
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
You can access the value of the form above with:
```python
custom_form.value['image']
```
after the submission.
"""
).callout(kind="info")
return
@app.cell(hide_code=True)
def __(mo):
mo.md(
"""
<h1 id="refs">References</h1>
- [Reference 1](https://example.com)
- [Reference 2](https://example.com)
"""
)
return
@app.cell(hide_code=True)
def __():
import anywidget
import traitlets
class HeaderWidget(anywidget.AnyWidget):
_esm = """
function escapeHTML(str) {
return str.replace(/[&<>'"]/g,
tag => ({
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"'
}[tag] || tag)
);
}
function stripHTML(html) {
const tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
function renderValue(value) {
if (typeof value !== 'string') {
return escapeHTML(String(value));
}
const isHTML = /<[a-z][\s\S]*>/i.test(value);
const strippedValue = isHTML ? stripHTML(value) : value;
if (strippedValue.length > 100) {
if (isHTML) {
return `
<div class="preview">${value.substring(0, 100)}...</div>
<div class="full-text" style="display: none;">${value}</div>
<button class="toggle-button">Show More</button>
`;
} else {
return `
<div class="preview">${escapeHTML(value.substring(0, 100))}...</div>
<div class="full-text" style="display: none;">${escapeHTML(value)}</div>
<button class="toggle-button">Show More</button>
`;
}
}
return isHTML ? value : escapeHTML(value);
}
function render({ model, el }) {
const result = model.get("result");
const container = document.createElement("div");
container.className = "header-container";
container.innerHTML = `
<img class="banner" src="https://raw.githubusercontent.com/Haleshot/marimo-tutorials/main/community-tutorials-banner.png" alt="Marimo Banner">
<div class="form-container">
${Object.entries(result).map(([key, value]) => `
<div class="form-row">
<label>${escapeHTML(key)}</label>
<div class="value-container">
${renderValue(value)}
</div>
</div>
`).join('')}
</div>
`;
el.appendChild(container);
container.querySelectorAll('.toggle-button').forEach(button => {
button.addEventListener('click', () => {
const row = button.closest('.form-row');
const preview = row.querySelector('.preview');
const fullText = row.querySelector('.full-text');
if (fullText.style.display === "none") {
fullText.style.display = "block";
preview.style.display = "none";
button.textContent = "Show Less";
} else {
fullText.style.display = "none";
preview.style.display = "block";
button.textContent = "Show More";
}
});
});
}
export default { render };
"""
_css = """
.header-container {
font-family: 'Helvetica Neue', Arial, sans-serif;
max-width: 100%;
margin: 0 auto;
overflow: hidden;
}
.banner {
width: 100%;
height: 200px;
object-fit: cover;
border-radius: 10px 10px 0 0;
display: block;
}
.form-container {
padding: 30px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
font-weight: 300;
box-shadow: 0 -10px 20px rgba(0,0,0,0.1);
}
.form-row {
display: flex;
flex-direction: column;
}
label {
font-size: 0.8em;
text-transform: uppercase;
letter-spacing: 1px;
margin-bottom: 5px;
font-weight: 500;
}
.value-container {
font-size: 1em;
line-height: 1.5;
}
.value-container a {
color: #0066cc;
text-decoration: none;
transition: color 0.2s ease;
}
.value-container a:hover {
color: #003366;
}
.preview, .full-text {
margin-bottom: 10px;
}
.toggle-button {
border: none;
border-radius: 20px;
padding: 8px 16px;
font-size: 0.9em;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.toggle-button:hover {
box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}
@media (max-width: 600px) {
.form-container {
grid-template-columns: 1fr;
}
}
"""
result = traitlets.Dict({}).tag(sync=True)
return HeaderWidget, anywidget, traitlets
@app.cell(hide_code=True)
def __(HeaderWidget):
header_widget = HeaderWidget(
result={
"Title": "Comprehensive E-Commerce Customer Behavior Analysis",
"Author": '<a href="https://github.com/Haleshot/marimo-tutorials">Dr. Jane Smith, PhD</a>',
"Affiliation": '<a href="https://www.datascience.university.edu">University of Data Science</a>',
"Version": "1.2.3",
"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.",
"Keywords": "E-Commerce Analytics, Customer Behavior Modeling, Predictive Analytics, Machine Learning, Natural Language Processing, Data Visualization, Time Series Analysis",
"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",
"Prerequisites": "Intermediate to advanced knowledge in statistics, machine learning, and Python programming. Familiarity with e-commerce concepts and business metrics is beneficial.",
"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.",
"License": '<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>',
"Last Updated": "November 3, 2024",
}
)
return (header_widget,)
@app.cell(hide_code=True)
def __():
import marimo as mo
return (mo,)
if __name__ == "__main__":
app.run()