Skip to content

Commit a8f81ae

Browse files
chekosclaude
andauthored
feat(docs): Add markdown-exec for executable code blocks (#309)
* feat(docs): Add markdown-exec for executable code blocks Replace manually-written output blocks with code that runs at build time via markdown-exec, so docs always reflect the current API behavior. Converted pages: quickstart, acs-data, decennial-data, geography, margins-of-error, migration-flows, multi-year, spatial. Pages left unconverted due to pre-existing API bugs (get_pums 400s, load_variables wrong URL pattern, get_estimates 404s): variables, pums-microdata, population-estimates, survey-design. Also adds --extra spatial to .readthedocs.yaml so geometry=True blocks can execute during the docs build. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: Add .env and .claude/ to .gitignore Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(docs): Address PR review feedback - Add missing print() in migration-flows.md (no output was rendered) - Revert get_estimates/get_flows exec blocks in spatial.md to static output - Fix Washington state population value in spatial.md (7614893 -> 7705281) - Remove unused income API call in margins-of-error.md - Add cache_table=True to ZCTA query in geography.md Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 966ce50 commit a8f81ae

File tree

13 files changed

+205
-565
lines changed

13 files changed

+205
-565
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
venv/
22
.venv/
33
.vscode/
4-
.claude/launch.json
4+
.env
5+
.claude/
56

67
# Created by https://www.gitignore.io/api/osx,python,pycharm,windows,visualstudio,visualstudiocode
78
# Edit at https://www.gitignore.io/?templates=osx,python,pycharm,windows,visualstudio,visualstudiocode

.readthedocs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ build:
1515
create_environment:
1616
- uv venv "${READTHEDOCS_VIRTUALENV_PATH}"
1717
install:
18-
- UV_PROJECT_ENVIRONMENT="${READTHEDOCS_VIRTUALENV_PATH}" uv sync --frozen --extra docs
18+
- UV_PROJECT_ENVIRONMENT="${READTHEDOCS_VIRTUALENV_PATH}" uv sync --frozen --extra docs --extra spatial

docs/getting-started/quickstart.md

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ print(ca_income.head())
2929
4. **year** -- The data year. Defaults to `2023`.
3030
5. **survey** -- `"acs5"` (5-year, default) or `"acs1"` (1-year). See [Census 101](census-101.md) for guidance on which to choose.
3131

32-
**Expected output** (tidy format, one row per geography per variable):
33-
34-
```
35-
GEOID NAME variable estimate moe
36-
0 06001 Alameda County, ... B19013_001 113650.0 1282.0
37-
1 06003 Alpine County, ... B19013_001 72857.0 9631.0
38-
2 06005 Amador County, ... B19013_001 71346.0 4076.0
39-
3 06007 Butte County, ... B19013_001 56219.0 1775.0
40-
4 06009 Calaveras County, ... B19013_001 70587.0 5047.0
32+
```python exec="on" session="qs"
33+
# markdown-exec: hide
34+
import pypums
35+
ca_income = pypums.get_acs(
36+
geography="county",
37+
variables="B19013_001",
38+
state="CA",
39+
year=2023,
40+
survey="acs5",
41+
)
42+
print(ca_income.head())
4143
```
4244

4345
| Column | Description |
@@ -78,21 +80,28 @@ print(la_poverty.head())
7880
print(type(la_poverty)) # <class 'geopandas.geodataframe.GeoDataFrame'>
7981
```
8082

81-
```
82-
GEOID NAME variable estimate moe geometry
83-
0 06037101100 Census Tract 1011, Los ... B17001_002 352.0 189.0 POLYGON ((-118.26480 34.05315, -118.26193 34....
84-
1 06037101202 Census Tract 1012.02, ... B17001_002 812.0 277.0 POLYGON ((-118.25714 34.04614, -118.25510 34....
85-
2 06037101300 Census Tract 1013, Los ... B17001_002 1045.0 344.0 POLYGON ((-118.27310 34.04055, -118.26950 34....
86-
3 06037101400 Census Tract 1014, Los ... B17001_002 627.0 258.0 POLYGON ((-118.28016 34.04400, -118.27670 34....
87-
4 06037102100 Census Tract 1021, Los ... B17001_002 490.0 199.0 POLYGON ((-118.24380 34.06120, -118.24117 34....
88-
```
89-
9083
1. **geography** -- `"tract"` gives you Census tracts, small statistical areas with 1,200--8,000 people.
9184
2. **variables** -- `B17001_002` is the count of people whose income is below the poverty level (from table B17001).
9285
3. **state** -- Required for tract-level queries so the API knows which state to pull tracts from.
9386
4. **county** -- `"037"` is the FIPS code for Los Angeles County. Use `pypums.datasets.fips.lookup_fips(state="California", county="Los Angeles County")` to look up codes.
9487
5. **geometry** -- When `True`, PyPUMS downloads cartographic boundary shapefiles (via pygris, cached locally) and merges them with the data. The result is a `GeoDataFrame` with a `geometry` column.
9588

89+
```python exec="on" session="qs-spatial"
90+
# markdown-exec: hide
91+
import pypums
92+
la_poverty = pypums.get_acs(
93+
geography="tract",
94+
variables="B17001_002",
95+
state="CA",
96+
county="037",
97+
year=2023,
98+
survey="acs5",
99+
geometry=True,
100+
)
101+
print(la_poverty.head())
102+
print(type(la_poverty))
103+
```
104+
96105
Now plot it with [Altair](https://altair-viz.github.io/):
97106

98107
```python
@@ -247,15 +256,17 @@ print(ca_pums.head())
247256
4. **survey** -- `"acs1"` (1-year) or `"acs5"` (5-year, default).
248257
5. **recode** -- When `True`, PyPUMS adds `*_label` columns that translate numeric codes into human-readable values. For example, `SEX=1` gets `SEX_label="Male"`.
249258

250-
**Expected output:**
251-
252-
```
253-
SERIALNO SPORDER PWGTP ST PUMA AGEP SEX WAGP SEX_label
254-
0 2022... 1 72 06 03701 35 1 45000 Male
255-
1 2022... 2 55 06 03701 32 2 38000 Female
256-
2 2022... 1 88 06 03702 28 1 52000 Male
257-
3 2022... 1 63 06 03702 41 2 67000 Female
258-
4 2022... 2 45 06 03702 19 1 8500 Male
259+
```python exec="on" session="qs-pums"
260+
# markdown-exec: hide
261+
import pypums
262+
ca_pums = pypums.get_pums(
263+
variables=["AGEP", "SEX", "WAGP"],
264+
state="CA",
265+
year=2023,
266+
survey="acs5",
267+
recode=True,
268+
)
269+
print(ca_pums.head())
259270
```
260271

261272
| Column | Description |

0 commit comments

Comments
 (0)