Training participants need a way to copy Markdown cells from trainer notebooks during live sessions. Unlike code cells which have visible execution counts, Markdown cells lack visible identifiers, making them difficult to reference.
- %code (formerly %nb): Handles code cells (identified by execution_count)
- %md: Handles markdown cells with flexible syntax (by index or relative to code cells)
- %nb: Kept as alias for %code for backward compatibility
- No visible numbering: Markdown cells don't have execution counts
- Cell IDs not user-friendly: Internal IDs exist but aren't visible in the UI
- Mixed cell types: Notebooks interleave code and Markdown cells
- Cell type conversion: Content retrieved needs manual conversion from Code to Markdown
Syntax: %md 1 2 - Get all Markdown cells between code cells 1 and 2
Pros:
- Uses existing visible code cell numbers
- Intuitive for contiguous Markdown sections
Cons:
- Ambiguous for non-contiguous selections
- Doesn't work for Markdown at notebook start/end
- May return multiple cells when only one is needed
Syntax: %md after:3 or %md before:5 - Get Markdown cell(s) after/before code cell
Pros:
- Precise single-cell selection
- Clear intent
- Works at notebook boundaries
Cons:
- Requires multiple commands for multiple cells
- User needs to know exact positions
Syntax: %md "# Section Title" - Find Markdown cells containing pattern
Pros:
- Natural for users who can see content
- Works without knowing cell positions
- Can use distinctive headers/keywords
Cons:
- Pattern might not be unique
- Requires exact string matching or regex
Syntax: %%md 3 - Cell magic that fetches Markdown after code cell 3
Pros:
- Automatically converts cell type to Markdown
- No manual cleanup needed
- Clear one-to-one mapping
- Can extend to support ranges
Cons:
- Different from line magic pattern
- Requires cell magic implementation
Syntax: %md 3 or %md 3-5 - Use sequential index for all Markdown cells
Implementation:
- Build index of all Markdown cells in order
- Reference by position (1st, 2nd, 3rd Markdown cell)
- Optional:
%md --listto show available Markdown cells with previews
Pros:
- Simple, consistent interface
- Works like %nb for code cells
- No ambiguity
- Can show preview list
Cons:
- Users need to count Markdown cells
- Numbers change if trainer adds cells
The %md command now supports unified syntax for both index-based and code-relative selection:
%md m1 # Get 1st Markdown cell
%md m3-m5 # Get 3rd through 5th Markdown cells
%md m7- # Get 7th cell onwards
%md -m3 # Get cells 1 through 3
%md --list # List all Markdown cells with m-prefixed numbers%md -3 # All Markdown cells before code cell 3
%md 5- # All Markdown cells after code cell 5
%md 3-5 # All Markdown between code cells 3 and 5%md -1 m3 5- # Before code 1, markdown cell 3, after code 5- Unlike %nb, the %md and %mdat magics do NOT add a comment header
- Content is inserted directly without any prefix
- Users manually convert cell type (Ctrl+M, M in Jupyter)
- Future: Investigate IPython API for automatic cell type conversion
%md --list
# Output:
# 1: # Introduction
# This notebook covers...
# 2: ## Data Loading
# We'll use pandas to...
# 3: ### Important Notes
# Remember to check...- Clear messages for invalid ranges
- Warnings when no Markdown cells found
- Handle notebooks without code cells gracefully
Add hidden HTML comments as anchors in Markdown cells:
<!-- md-anchor: section1 -->
# IntroductionRejected: Requires modifying trainer notebooks
Propose Jupyter enhancement to show Markdown cell numbers in UI. Rejected: Outside our control, long-term solution
Return both code and Markdown versions, let user choose. Rejected: Clutters interface, still requires manual work
- Renamed
%nbto%codeas the canonical command for code cells - Kept
%nbas an alias for backward compatibility - Implemented unified
%mdcommand with both index and position-based syntax - Removed separate
%mdatcommand (functionality merged into%md) - Updated all documentation and help text
[Code 1] Import statements
[Markdown] # Data Analysis Workshop
[Markdown] ## Prerequisites
[Code 2] Load data
[Markdown] ### Understanding the dataset
[Code 3] Explore data
%code 1 # Get code cell 1 (preferred)
%nb 1 # Same as %code 1 (backward compatibility)
%md m1 # Get "# Data Analysis Workshop"
%md m2-m3 # Get "## Prerequisites" and "### Understanding the dataset"
%md 1- # Get markdown after code cell 1
%md -2 # Get markdown before code cell 2The unified %md command provides a powerful and flexible interface for selecting markdown cells. By supporting both index-based (m1, m2-m5) and code-relative (-1, 2-, 3-5) syntax in a single command, users have maximum flexibility without needing to remember multiple commands. The implementation handles edge cases like duplicate code cell numbers (using the last occurrence) and provides clear error messages for invalid selections.