Skip to content

Commit 787a599

Browse files
Adding a prompts section to the docs (#5431)
This PR adds a section to the docs: generate with AI > Prompts for the marimo editor. This includes custom rules that you can use to more effectively write anywidgets. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d5142c1 commit 787a599

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

docs/guides/generate_with_ai/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
| Guide | Description |
44
|-------|-------------|
55
| [Zero-shot entire notebooks](text_to_notebook.md) | Generate entire notebooks with AI |
6-
| [AI-assisted coding](../editor_features/ai_completion.md) | Refactor code and generate cells with AI|
6+
| [AI-assisted coding](../editor_features/ai_completion.md) | Refactor code and generate cells with AI |
7+
| [Prompts for the marimo editor](prompts.md) | Collection of prompts to speed up workflows in marimo |
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
marimo offers many tools for [AI assisted coding](/guides/editor_features/ai_completion/). However, sometimes you may want to do something more custom and could benefit from a prompt template to get started from. The goal of this page is to share prompts that have proven to be useful and will help you get started on specific tasks.
2+
3+
You can add these prompts to your [custom rules](/guides/editor_features/ai_completion/?h=#custom-rules) but be mindful that doing so will make every call to the LLM more expensive because you're feeding it more tokens. That's why we generally recommend to start your conversations with these snippets if you don't plan on using them very often.
4+
5+
## Anywidget
6+
7+
If you want to generate [custom UIs or widgets for marimo](/api/inputs/anywidget/?h=anywidget#building-custom-ui-elements) then anywidget is the way to go. Because anywidget is a fairly revent project, LLMs have been known to hallucinate when you try to generate custom widgets from scratch. The following prompt contains an example that helps prevent this behaviour and it also points out common failure scenarios that the LLM should avoid.
8+
9+
```
10+
When writing an anywidget use vanilla javascript in `_esm` and do not forget about `_css`. The css should look bespoke in light mode and dark mode. Keep the css small unless explicitly asked to go the extra mile. When you display the widget it must be wrapped via `widget = mo.ui.anywidget(OriginalAnywidget())`.
11+
12+
<example title="Example anywidget implementation">
13+
import anywidget
14+
import traitlets
15+
16+
17+
class CounterWidget(anywidget.AnyWidget):
18+
_esm = """
19+
// Define the main render function
20+
function render({ model, el }) {
21+
let count = () => model.get("number");
22+
let btn = document.createElement("button");
23+
btn.innerHTML = `count is ${count()}`;
24+
btn.addEventListener("click", () => {
25+
model.set("number", count() + 1);
26+
model.save_changes();
27+
});
28+
model.on("change:number", () => {
29+
btn.innerHTML = `count is ${count()}`;
30+
});
31+
el.appendChild(btn);
32+
}
33+
// Important! We must export at the bottom here!
34+
export default { render };
35+
"""
36+
_css = """button{
37+
font-size: 14px;
38+
}"""
39+
number = traitlets.Int(0).tag(sync=True)
40+
41+
widget = mo.ui.anywidget(CounterWidget())
42+
widget
43+
44+
# Grabbing the widget from another cell, `.value` is a dictionary.
45+
print(widget.value["number"])
46+
</example>
47+
48+
When sharing the anywidget, keep the example minimal. No need to combine it with marimo ui elements unless explicitly stated to do so.
49+
```
50+

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ nav:
108108
- Generate with AI: guides/generate_with_ai/index.md
109109
- Generate entire notebooks: guides/generate_with_ai/text_to_notebook.md
110110
- AI-assisted coding: guides/editor_features/ai_completion.md
111+
- Prompts for the marimo editor: guides/generate_with_ai/prompts.md
111112
- Editor features:
112113
- Editor features: guides/editor_features/index.md
113114
- Editor overview: guides/editor_features/overview.md

0 commit comments

Comments
 (0)