Skip to content

Commit 76f7905

Browse files
DOC-6089 jupyterize improvements and lessons learned
1 parent 7208f55 commit 76f7905

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

build/jupyterize/SPECIFICATION.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,8 @@ Go has a unique requirement: notebooks MUST have a `func main() {}` wrapper for
12551255
- This ensures imports and `func main() {}` are in the same cell, matching gophernotes expectations
12561256
- The wrapper is injected as boilerplate, then removed from source, then re-injected
12571257
- This ensures the notebook has a clean wrapper without test framework code
1258+
- **Client connection configuration**: Boilerplate can include more than just wrappersit can include client connection setup (e.g., Redis client initialization with configuration options like `MaintNotificationsConfig`)
1259+
- This allows notebooks to have a working client connection without requiring users to add boilerplate code manually
12581260

12591261
**Pattern complexity comparison** (As Proposed):
12601262
- **C#**: 5 patterns (class/method wrappers + closing braces)
@@ -1363,6 +1365,53 @@ When adding a new language, ask:
13631365

13641366
If yes to any of these, use Strategy 2 (append to first cell). Otherwise, use Strategy 1 (separate cell).
13651367

1368+
### Boilerplate Content Patterns (What Goes in Boilerplate)
1369+
1370+
**Lesson Learned**: Boilerplate is not limited to language wrappers and imports. It can include any code that should appear in every notebook generated from that language, including client connection setup and configuration.
1371+
1372+
**Common boilerplate content**:
1373+
1. **Language wrappers** (required by kernel)
1374+
- C#: NuGet package directives (`#r "nuget: ..."`)
1375+
- Go: `func main() {}` wrapper (gophernotes requirement)
1376+
- Java: (typically empty; dependencies handled via `%maven` magic commands)
1377+
1378+
2. **Client connection setup** (optional but recommended)
1379+
- Redis client initialization with connection options
1380+
- Configuration flags (e.g., `MaintNotificationsConfig` for Go)
1381+
- Default connection parameters (host, port, password)
1382+
- This ensures notebooks have a working client without manual setup
1383+
1384+
3. **Import statements** (language-dependent)
1385+
- C#: NuGet directives (not traditional imports)
1386+
- Go: Typically in STEP blocks, not boilerplate (unless needed for wrapper)
1387+
- Java: Typically in STEP blocks, not boilerplate
1388+
1389+
**Example: Go with Client Connection**:
1390+
```json
1391+
{
1392+
"go": {
1393+
"boilerplate": [
1394+
"rdb := redis.NewClient(&redis.Options{",
1395+
"\tAddr: \"localhost:6379\",",
1396+
"\tPassword: \"\",",
1397+
"\tDB: 0,",
1398+
"\tMaintNotificationsConfig: &maintnotifications.Config{",
1399+
"\t\tMode: maintnotifications.ModeDisabled,",
1400+
"\t},",
1401+
"})",
1402+
"func main() {}"
1403+
]
1404+
}
1405+
}
1406+
```
1407+
1408+
**Design considerations**:
1409+
- **Boilerplate should be minimal**: Include only code that's needed in every notebook
1410+
- **Avoid language-specific details**: Don't include code that only applies to some examples
1411+
- **Configuration over code**: Use boilerplate for configuration (connection options) rather than business logic
1412+
- **Test with real examples**: Verify boilerplate works with actual example files before committing
1413+
- **Document assumptions**: If boilerplate assumes certain imports or packages, document this in comments or README
1414+
13661415

13671416
### Testing Checklist (Language-Specific)
13681417

build/jupyterize/jupyterize_config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@
4545
},
4646
"go": {
4747
"boilerplate": [
48+
"rdb := redis.NewClient(&redis.Options{",
49+
"\tAddr: \"localhost:6379\",",
50+
"\tPassword: \"\",",
51+
"\tDB: 0,",
52+
"\tMaintNotificationsConfig: &maintnotifications.Config{",
53+
"\t\tMode: maintnotifications.ModeDisabled,",
54+
"\t},",
55+
"})",
4856
"func main() {}"
4957
],
5058
"unwrap_patterns": [

build/jupyterize/test_jupyterize.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def test_python_no_boilerplate():
516516

517517

518518
def test_go_boilerplate_injection():
519-
"""Test Go boilerplate injection (func main() {} appended to first cell)."""
519+
"""Test Go boilerplate injection (client config and func main() {} appended to first cell)."""
520520
print("\nTesting Go boilerplate injection...")
521521

522522
# Create test file with Go code
@@ -556,12 +556,16 @@ def test_go_boilerplate_injection():
556556
assert nb['metadata']['kernelspec']['name'] == 'gophernotes', \
557557
f"Kernel should be gophernotes, got {nb['metadata']['kernelspec']['name']}"
558558

559-
# First cell should contain imports AND func main() {}
559+
# First cell should contain imports, client config, and func main() {}
560560
# (boilerplate is appended to first cell for Go, not separate)
561561
first_cell = nb['cells'][0]
562562
first_cell_source = ''.join(first_cell['source'])
563563
assert 'import (' in first_cell_source, \
564564
f"First cell should contain imports, got: {first_cell_source}"
565+
assert 'redis.NewClient' in first_cell_source, \
566+
f"First cell should contain redis.NewClient, got: {first_cell_source}"
567+
assert 'MaintNotificationsConfig' in first_cell_source, \
568+
f"First cell should contain MaintNotificationsConfig, got: {first_cell_source}"
565569
assert 'func main() {}' in first_cell_source, \
566570
f"First cell should contain 'func main() {{}}', got: {first_cell_source}"
567571

0 commit comments

Comments
 (0)