Repository navigation
Expand file tree
/
Copy pathgetting-started.py
More file actions
334 lines (245 loc) Β· 8.97 KB
/
Copy pathgetting-started.py
File metadata and controls
334 lines (245 loc) Β· 8.97 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
# π Getting Started with Plaque
Welcome to **Plaque** - the interactive Python notebook system that turns your regular Python files into beautiful, live-updating notebooks!
This getting-started guide will teach you everything you need to know to become productive with Plaque in just a few minutes.
## What Makes Plaque Special?
β
**Local-first** - Your files stay on your computer
β
**Real Python files** - No special format, just `.py` files
β
**Live updates** - See changes instantly as you edit
β
**Rich displays** - Beautiful HTML, plots, and visualizations
β
**Your favorite editor** - Use any text editor or IDE
Let's dive in!
"""
# %%
# This is a traditional cell marker - one of three ways to create cells in Plaque
print("Hello, Plaque! π")
"""
## π Three Ways to Create Cells
Plaque supports three different cell formats. Let's explore each one:
### 1. Traditional Markers (like above)
Use `# %%` to create code cells, just like in other notebook systems.
"""
# %%
# Another traditional cell
name = "Plaque User"
print(f"Welcome, {name}!")
"""
### 2. Multiline Comment Cells (Recommended)
This cell you're reading right now is a **multiline comment cell**. This is the recommended way to write markdown in Plaque because:
- More readable in your text editor
- Proper syntax highlighting
- Easy to write and edit
- Supports full Markdown and LaTeX: $E=mc^2$
Just use triple quotes with your markdown content!
"""
# Let's create some variables to work with
user_name = "Alice"
favorite_number = 42
hobbies = ["reading", "coding", "hiking"]
print(f"User: {user_name}")
print(f"Favorite number: {favorite_number}")
print(f"Hobbies: {', '.join(hobbies)}")
"""
### 3. Dynamic F-String Cells
The third type uses f-strings for dynamic content that updates based on your variables:
"""
f"""
### π User Profile
**Name:** {user_name}
**Lucky Number:** {favorite_number}
**Hobbies:** {len(hobbies)} activities
The user {user_name} has {len(hobbies)} hobbies and their favorite number is {favorite_number}.
"""
# Create some sample data to use in our examples
student_grades = {"Alice": 95, "Bob": 87, "Charlie": 92, "Diana": 98}
"""
## π¨ Rich Display Features
One of Plaque's superpowers is rich display support. Let's explore different types of output:
"""
# The last expression in a cell is automatically displayed
student_grades
"""
### π Simple Data Visualization
Stderr and stdout are captured and displayed in the notebook.
"""
def create_text_chart(data, title="Chart"):
"""Create a simple text-based bar chart"""
max_value = max(data.values())
print(f"\nπ {title}")
print("=" * 30)
for name, value in data.items():
bar_length = int((value / max_value) * 20)
bar = "β" * bar_length
print(f"{name:10} {bar} {value}")
print("=" * 30)
create_text_chart(student_grades, "Student Grades")
"""
### π HTML Rich Display
Plaque supports rich HTML output for beautiful presentations:
"""
from IPython.display import HTML
# Simple HTML example - Plaque displays rich HTML automatically
HTML(f"""
<div style="border: 2px solid #4CAF50; border-radius: 8px; padding: 15px;
background: #f0f8ff; font-family: Arial;">
<h3 style="color: #333; margin-top: 0;">π Student Grades</h3>
<p><strong>Average:</strong> {sum(student_grades.values()) / len(student_grades):.1f}</p>
<ul>
{"".join([f"<li><strong>{name}:</strong> {grade}</li>" for name, grade in student_grades.items()])}
</ul>
</div>
""")
"""
## π Live Updates & State Management
Plaque solves a key problem with traditional IPython notebooks: **state management**. In Jupyter notebooks, cells can be run out of order, creating confusing state where variables might not match what you see on screen.
Plaque takes a different approach:
- **Top-to-bottom execution**: Your notebook runs like a regular Python file, from top to bottom
- **Smart re-execution**: Only cells that changed (or depend on changed variables) are re-run
- **Predictable state**: What you see is always what you get - no hidden state surprises
Unlike systems like Marimo that have full dependency resolution, Plaque keeps it simple: it runs your code sequentially but only re-executes what's necessary. This gives you the benefits of live updates without the complexity of a full reactive system.
Change any value above and save to see instant, predictable updates!
"""
def fibonacci_sequence(n):
"""Generate the first n Fibonacci numbers"""
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i - 1] + fib[i - 2])
return fib
# Try changing this number and saving the file!
fib_count = 12
fibonacci_numbers = fibonacci_sequence(fib_count)
print(f"First {fib_count} Fibonacci numbers:")
print(fibonacci_numbers)
"""
### π² Random Experiments
Let's create something fun that generates different results each time:
"""
import random
def roll_dice(num_dice=2, num_sides=6):
"""Roll some dice and return the results"""
rolls = [random.randint(1, num_sides) for _ in range(num_dice)]
total = sum(rolls)
return {"rolls": rolls, "total": total, "average": total / num_dice}
# Roll some dice - try saving the file multiple times to see different results!
dice_result = roll_dice(3, 6)
print(f"π² Dice rolls: {dice_result['rolls']}")
print(f"Total: {dice_result['total']}")
print(f"Average: {dice_result['average']:.1f}")
"""
### π― Try This Yourself!
Change values above and save to see instant updates:
- Change `fib_count` to 15
- Try `roll_dice(5, 10)` for 5 ten-sided dice
- Add a new student to `student_grades`
- Modify the `user_name` variable
Each save triggers re-execution and live updates!
"""
"""
## π Rich Data Displays
Plaque automatically displays DataFrames, plots, and other rich objects:
"""
"""
### πΌ Pandas DataFrames
DataFrames are automatically displayed as HTML tables (requires: pip install pandas):
"""
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame(
{
"Student": list(student_grades.keys()),
"Grade": list(student_grades.values()),
"Letter": [
"A" if g >= 90 else "B" if g >= 80 else "C" for g in student_grades.values()
],
}
)
# DataFrames are automatically displayed as HTML tables
df
# You can also create more complex DataFrames with calculations
summary_df = pd.DataFrame(
{
"Metric": ["Total Students", "Average Grade", "Highest Grade", "Lowest Grade"],
"Value": [
len(student_grades),
f"{sum(student_grades.values()) / len(student_grades):.1f}",
max(student_grades.values()),
min(student_grades.values()),
],
}
)
summary_df
"""
### π¬ Matplotlib Plots
Plots are automatically captured and displayed (requires: pip install matplotlib):
"""
import matplotlib.pyplot as plt
# Create a simple plot
x = list(range(1, 11))
y = [i**2 for i in x]
plt.figure(figsize=(10, 6))
plt.plot(x, y, "bo-", linewidth=2, markersize=8)
plt.title("Square Numbers", fontsize=16, fontweight="bold")
plt.xlabel("Number", fontsize=12)
plt.ylabel("Square", fontsize=12)
plt.grid(True, alpha=0.3)
plt.show()
# Also create a bar chart of our student grades
plt.figure(figsize=(10, 6))
names = list(student_grades.keys())
grades = list(student_grades.values())
bars = plt.bar(names, grades, color=["#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4"])
plt.title("Student Grades", fontsize=16, fontweight="bold")
plt.ylabel("Grade", fontsize=12)
plt.ylim(0, 100)
# Add value labels on bars
for bar, grade in zip(bars, grades, strict=True):
plt.text(
bar.get_x() + bar.get_width() / 2,
bar.get_height() + 1,
str(grade),
ha="center",
va="bottom",
fontweight="bold",
)
plt.tight_layout()
plt.show()
"""
## π How to Use This Notebook
Now that you've seen what Plaque can do, here's how to use it:
### π» Development Mode (Recommended)
```bash
plaque serve getting-started.py --open
```
This starts a live server that:
- β
Opens your notebook in the browser
- β
Updates automatically when you save changes
- β
Shows errors clearly if something goes wrong
- β
Lets you experiment and iterate quickly
- β
Exposes a REST API for programmatic access (perfect for agentic coding with assistants like Claude Code)
### π Export Mode
```bash
plaque render getting-started.py output.html --open
```
This creates a static HTML file that you can:
- β
Share with others
- β
View without running Plaque
- β
Include in presentations or reports
### π Watch Mode
```bash
plaque watch getting-started.py output.html --open
```
This watches for file changes and automatically regenerates the HTML.
"""
"""
## πͺ What's Next?
β
**Try other examples**: `simple_interactive.py`, `data_exploration.py`
β
**Create your own**: Any `.py` file can become a Plaque notebook
β
**Remember**: Use your favorite editor and save often for live updates!
**Happy coding with Plaque! π**
"""