| name | learning-notebook_conversion |
| description | Convert between .ipynb and .py formats for course assignments. Use when (1) user asks to convert notebook to script or vice versa, (2) mentions "转换" or "convert", (3) needs to switch between interactive and script formats. |
Learning Notebook Conversion
Objectives
- Convert .ipynb to .py while preserving structure and documentation
- Convert .py to .ipynb with proper cell organization
- Maintain code quality and academic formatting during conversion
Instructions
1. Conversion Methods
Method A: VS Code Built-in (Recommended for single files)
- Open .ipynb file
- Press
Ctrl+Shift+P
- Select "Notebook: Export to Python Script"
- Choose save location
Method B: Jupyter nbconvert (Recommended for batch/automation)
jupyter nbconvert --to python notebook.ipynb
jupytext --to notebook script.py
Method C: Python script (For custom processing)
import nbformat
from nbconvert import PythonExporter
with open('notebook.ipynb', 'r', encoding='utf-8') as f:
nb = nbformat.read(f, as_version=4)
exporter = PythonExporter()
source, meta = exporter.from_notebook_node(nb)
with open('output.py', 'w', encoding='utf-8') as f:
f.write(source)
2. .ipynb → .py Conversion
What gets converted:
- Code cells → Python code
- Markdown cells → Multi-line comments (
"""...""")
- Cell separators →
# %% markers (VS Code interactive cells)
- Outputs → Removed (code only)
Post-conversion cleanup:
-
Add file-level docstring (if missing):
"""
Course Code Lab X: Title
Author: [Student Name]
Section: [Section Number]
Date: [Date]
"""
-
Review cell markers:
- Keep
# %% for logical sections
- Remove unnecessary markers
- Add descriptive comments after markers
-
Convert markdown to docstrings:
- Section headers → Comments
- Important notes → Inline comments
- Instructions → Function docstrings
-
Adjust outputs:
- Change
display() to print()
- Ensure
plt.show() for plots
- Remove IPython-specific magic commands
Example conversion:
import pandas as pd
df = pd.read_csv('data.csv')
print(df.head())
3. .py → .ipynb Conversion
Using jupytext:
pip install jupytext
jupytext --to notebook script.py
jupytext --to notebook --execute script.py
Cell organization rules:
# %% markers → New code cells
- Multi-line strings at top → Markdown cells
- Section comments → Markdown headers
- Code blocks → Code cells
Post-conversion tasks:
-
Run all cells to generate outputs
-
Add markdown cells for better documentation:
- Title and metadata at top
- Section headers before major steps
- Explanations for complex logic
-
Verify cell order:
- Logical flow maintained
- No circular dependencies
- Outputs display correctly
-
Format markdown cells:
## Step 1: Import Libraries
Import required packages for data analysis.
4. Batch Conversion
Convert multiple files:
jupyter nbconvert --to python *.ipynb
for file in *.py; do jupytext --to notebook "$file"; done
Python script for batch processing:
import os
from pathlib import Path
import nbformat
from nbconvert import PythonExporter
def convert_notebooks_to_py(directory):
"""Convert all .ipynb files in directory to .py"""
for ipynb_file in Path(directory).glob('*.ipynb'):
with open(ipynb_file, 'r', encoding='utf-8') as f:
nb = nbformat.read(f, as_version=4)
exporter = PythonExporter()
source, _ = exporter.from_notebook_node(nb)
py_file = ipynb_file.with_suffix('.py')
with open(py_file, 'w', encoding='utf-8') as f:
f.write(source)
print(f"Converted: {ipynb_file} → {py_file}")
convert_notebooks_to_py('labs/')
5. Conversion Best Practices
Before conversion:
After conversion:
Maintain consistency:
- Keep both formats if needed for different use cases
- Use version control to track changes
- Document which format is "source of truth"
- Sync changes between formats when updating
6. Format-Specific Considerations
When to use .ipynb:
- Interactive exploration and visualization
- Teaching and presentations
- Sharing with outputs visible
- Iterative development
When to use .py:
- Version control (cleaner diffs)
- Automated testing and CI/CD
- Production deployment
- Command-line execution
- Code review
Hybrid approach:
- Develop in .ipynb for interactivity
- Convert to .py for submission/version control
- Use jupytext to sync both formats automatically
Validation
After conversion, verify:
Code Execution
Structure Preservation
Academic Requirements
Common Issues
Issue: Cell markers (# %%) not recognized
- Solution: Install Python extension in VS Code
- Or: Use Jupyter extension for interactive cells
Issue: Markdown cells lost in conversion
- Solution: Use
jupytext instead of basic nbconvert
- Or: Manually convert important markdown to docstrings
Issue: Relative imports broken
- Solution: Check working directory and adjust paths
- Or: Use absolute imports with package structure
Issue: Magic commands cause errors in .py
- Solution: Remove or comment out IPython magic commands:
%matplotlib inline → Remove or comment
!pip install → Convert to regular shell command
%%time → Use time module instead
Issue: Outputs don't display in .py
- Solution: Replace
display() with print()
- Add
plt.show() for matplotlib plots
- Use explicit print statements for results
Installation Requirements
pip install jupyter nbconvert
pip install jupytext
pip install nbformat
Example Workflow
User request: "把这个 notebook 转成 .py 文件"
Your workflow:
- Identify the .ipynb file (from context or ask user)
- Choose conversion method based on needs:
- Single file → VS Code export or nbconvert
- Batch → Script or command-line loop
- Custom processing → Python script
- Execute conversion
- Perform post-conversion cleanup:
- Add/verify docstrings
- Clean up cell markers
- Test execution
- Validate against checklist
- Inform user of completion and any issues
User request: "生成这个 .py 的 notebook 版本"
Your workflow:
- Identify the .py file
- Use jupytext to convert
- Open in Jupyter/VS Code
- Run all cells to generate outputs
- Add markdown cells for documentation
- Verify cell organization
- Save and inform user