|
| 1 | +# pycancensus Documentation |
| 2 | + |
| 3 | +This directory contains the documentation system for pycancensus, built with Sphinx and featuring: |
| 4 | + |
| 5 | +- **Sphinx-Gallery**: Executable Python examples with beautiful thumbnails |
| 6 | +- **MyST-NB**: Markdown tutorials that execute as notebooks |
| 7 | +- **Autosummary**: Auto-generated API documentation |
| 8 | +- **Professional Theming**: Read the Docs theme with copy buttons |
| 9 | + |
| 10 | +## Building Documentation |
| 11 | + |
| 12 | +### Prerequisites |
| 13 | + |
| 14 | +Install documentation dependencies: |
| 15 | + |
| 16 | +```bash |
| 17 | +pip install -e .[docs] |
| 18 | +``` |
| 19 | + |
| 20 | +### Build Commands |
| 21 | + |
| 22 | +```bash |
| 23 | +# Build HTML documentation |
| 24 | +cd docs |
| 25 | +make html |
| 26 | + |
| 27 | +# Clean and rebuild |
| 28 | +make clean-all && make html |
| 29 | + |
| 30 | +# Live rebuilding during development |
| 31 | +make livehtml # Requires sphinx-autobuild |
| 32 | +``` |
| 33 | + |
| 34 | +The built documentation will be in `docs/_build/html/`. |
| 35 | + |
| 36 | +## Documentation Structure |
| 37 | + |
| 38 | +``` |
| 39 | +docs/ |
| 40 | +├── conf.py # Sphinx configuration |
| 41 | +├── index.rst # Main documentation page |
| 42 | +├── tutorials/ # MyST-NB tutorials |
| 43 | +│ ├── getting_started.md # Main tutorial |
| 44 | +│ ├── working_with_geometry.md |
| 45 | +│ └── caching_data.md |
| 46 | +├── examples/ # Sphinx-Gallery examples |
| 47 | +│ ├── plot_basic_census_data.py |
| 48 | +│ └── plot_geographic_analysis.py |
| 49 | +├── api/ # Auto-generated API docs |
| 50 | +│ └── index.rst |
| 51 | +└── _static/ # Static files (CSS, images) |
| 52 | +``` |
| 53 | + |
| 54 | +## Adding New Content |
| 55 | + |
| 56 | +### Adding a New Tutorial |
| 57 | + |
| 58 | +1. Create a new `.md` file in `docs/tutorials/` |
| 59 | +2. Use MyST-NB format with executable code cells: |
| 60 | + |
| 61 | +```markdown |
| 62 | +--- |
| 63 | +jupytext: |
| 64 | + text_representation: |
| 65 | + extension: .md |
| 66 | + format_name: myst |
| 67 | +kernelspec: |
| 68 | + display_name: Python 3 |
| 69 | + language: python |
| 70 | + name: python3 |
| 71 | +--- |
| 72 | + |
| 73 | +# Your Tutorial Title |
| 74 | + |
| 75 | +Tutorial introduction text. |
| 76 | + |
| 77 | +``{code-cell} python |
| 78 | +import pycancensus as pc |
| 79 | +print("Your code here") |
| 80 | +`` |
| 81 | +``` |
| 82 | + |
| 83 | +3. Add the tutorial to `docs/tutorials/index.rst` |
| 84 | + |
| 85 | +### Adding a New Example |
| 86 | + |
| 87 | +1. Create a new Python file in `docs/examples/` with filename starting with `plot_` |
| 88 | +2. Use Sphinx-Gallery format with docstring introduction: |
| 89 | + |
| 90 | +```python |
| 91 | +""" |
| 92 | +Example Title |
| 93 | +============= |
| 94 | +
|
| 95 | +Detailed description of what this example demonstrates. |
| 96 | +""" |
| 97 | + |
| 98 | +# %% |
| 99 | +# Section Header |
| 100 | +# -------------- |
| 101 | +# |
| 102 | +# Explanation of this section. |
| 103 | + |
| 104 | +import pycancensus as pc |
| 105 | +# Your code here |
| 106 | + |
| 107 | +# %% |
| 108 | +# Another Section |
| 109 | +# --------------- |
| 110 | +# |
| 111 | +# More explanation and code. |
| 112 | + |
| 113 | +print("Examples are automatically executed!") |
| 114 | +``` |
| 115 | + |
| 116 | +3. Examples are automatically included in the gallery |
| 117 | + |
| 118 | +### Adding API Documentation |
| 119 | + |
| 120 | +API documentation is automatically generated from docstrings. To add a new function: |
| 121 | + |
| 122 | +1. Ensure the function is exported in `pycancensus/__init__.py` |
| 123 | +2. Add it to the appropriate section in `docs/api/index.rst`: |
| 124 | + |
| 125 | +```rst |
| 126 | +New Section |
| 127 | +----------- |
| 128 | +
|
| 129 | +.. autosummary:: |
| 130 | + :toctree: generated/ |
| 131 | +
|
| 132 | + your_new_function |
| 133 | +``` |
| 134 | + |
| 135 | +## Documentation Standards |
| 136 | + |
| 137 | +### Code Examples |
| 138 | + |
| 139 | +- **Always include working examples** in docstrings and tutorials |
| 140 | +- **Handle API key requirements** gracefully (show setup but don't require real keys) |
| 141 | +- **Use realistic but sample data** when possible |
| 142 | +- **Add error handling** to show users what can go wrong |
| 143 | + |
| 144 | +### Writing Style |
| 145 | + |
| 146 | +- **Be concise but complete** - explain what users need to know |
| 147 | +- **Use active voice** and clear instructions |
| 148 | +- **Include "why" not just "how"** - explain the purpose |
| 149 | +- **Cross-reference** related functions and tutorials |
| 150 | + |
| 151 | +### Code Style in Examples |
| 152 | + |
| 153 | +- **Follow PEP 8** and project conventions |
| 154 | +- **Use meaningful variable names** |
| 155 | +- **Add comments** to explain complex steps |
| 156 | +- **Show intermediate results** to help debugging |
| 157 | + |
| 158 | +## Common Issues and Solutions |
| 159 | + |
| 160 | +### Build Errors |
| 161 | + |
| 162 | +**Problem**: `ModuleNotFoundError` during build |
| 163 | +**Solution**: Install all dependencies with `pip install -e .[docs,dev]` |
| 164 | + |
| 165 | +**Problem**: Examples fail to execute |
| 166 | +**Solution**: |
| 167 | +- Check that examples handle missing API keys gracefully |
| 168 | +- Ensure all required packages are imported |
| 169 | +- Test examples independently: `python docs/examples/plot_example.py` |
| 170 | + |
| 171 | +**Problem**: MyST-NB execution errors |
| 172 | +**Solution**: |
| 173 | +- Check code cell syntax (triple backticks with `{code-cell} python`) |
| 174 | +- Verify notebook metadata in frontmatter |
| 175 | +- Test with: `jupyter-book build docs/tutorials/` |
| 176 | + |
| 177 | +### Link Errors |
| 178 | + |
| 179 | +**Problem**: Broken cross-references |
| 180 | +**Solution**: Use proper Sphinx references: |
| 181 | +- Functions: `:func:`pycancensus.get_census`` |
| 182 | +- Tutorials: `:doc:`tutorials/getting_started`` |
| 183 | +- External: `External Link <https://example.com>`_ |
| 184 | + |
| 185 | +## Continuous Integration |
| 186 | + |
| 187 | +Documentation builds automatically on: |
| 188 | +- Every push to main/develop branches |
| 189 | +- Pull requests to main |
| 190 | +- Scheduled daily builds (to catch external dependency issues) |
| 191 | + |
| 192 | +The CI workflow: |
| 193 | +1. Installs dependencies |
| 194 | +2. Builds documentation with `make html` |
| 195 | +3. Checks for broken links |
| 196 | +4. Fails if any errors occur |
| 197 | + |
| 198 | +## Publishing |
| 199 | + |
| 200 | +Documentation is published automatically to Read the Docs: |
| 201 | +- **Development**: Built from `develop` branch |
| 202 | +- **Stable**: Built from tagged releases |
| 203 | +- **Latest**: Built from `main` branch |
| 204 | + |
| 205 | +### Read the Docs Configuration |
| 206 | + |
| 207 | +The `.readthedocs.yaml` file configures: |
| 208 | +- Python version and dependencies |
| 209 | +- Build environment |
| 210 | +- Documentation format |
| 211 | + |
| 212 | +## Performance Tips |
| 213 | + |
| 214 | +### Fast Development Builds |
| 215 | + |
| 216 | +```bash |
| 217 | +# Skip example execution during development |
| 218 | +export SPHINX_GALLERY_CONF_PLOT_GALLERY=False |
| 219 | +make html |
| 220 | + |
| 221 | +# Build only specific sections |
| 222 | +sphinx-build -b html -D exclude_patterns="auto_examples/*" . _build/html |
| 223 | +``` |
| 224 | + |
| 225 | +### Caching |
| 226 | + |
| 227 | +- **Jupyter Cache**: MyST-NB caches notebook execution results |
| 228 | +- **Sphinx Cache**: Reuses unchanged documentation |
| 229 | +- **Gallery Cache**: Sphinx-Gallery caches example outputs |
| 230 | + |
| 231 | +Clear caches if you encounter stale content: |
| 232 | +```bash |
| 233 | +make clean-all |
| 234 | +rm -rf docs/_build/.jupyter_cache |
| 235 | +``` |
| 236 | + |
| 237 | +## Contributing Documentation |
| 238 | + |
| 239 | +1. **Fork** the repository |
| 240 | +2. **Create** a feature branch for your documentation changes |
| 241 | +3. **Test** your changes locally with `make html` |
| 242 | +4. **Submit** a pull request with a clear description |
| 243 | + |
| 244 | +### Documentation Pull Request Checklist |
| 245 | + |
| 246 | +- [ ] Documentation builds without errors |
| 247 | +- [ ] Examples execute successfully |
| 248 | +- [ ] Links work correctly |
| 249 | +- [ ] Spelling and grammar checked |
| 250 | +- [ ] Screenshots updated if UI changed |
| 251 | +- [ ] Cross-references added where helpful |
| 252 | + |
| 253 | +## Resources |
| 254 | + |
| 255 | +- [Sphinx Documentation](https://www.sphinx-doc.org/) |
| 256 | +- [MyST-NB Guide](https://myst-nb.readthedocs.io/) |
| 257 | +- [Sphinx-Gallery Examples](https://sphinx-gallery.github.io/) |
| 258 | +- [Read the Docs Guide](https://docs.readthedocs.io/) |
| 259 | + |
| 260 | +For questions about documentation, open an issue or discussion on GitHub. |
0 commit comments