Skip to content

Commit aeedcd8

Browse files
authored
Publish Python package to PyPI (#43)
* Don't use symlinks for data in the Python package, to support Windows installs * Clean up installation instructions in README * Revert "Clean up installation instructions in README" This reverts commit e35d0a2. * Add workflow for building and pushing releases to PyPI * Temporarily enable builds of the `pypi-publish` workflow on PRs, for testing * Tweak conditional syntax in `pypi-publish.yaml` to try to resolve parsing error * Revert "Temporarily enable builds of the `pypi-publish` workflow on PRs, for testing" This reverts commit f72df21. * Factor out common kwargs for reading the vars dict in vars_funs.py * Always use absolute path when reading vars dict from CSV * Tweaks to README ahead of PyPI release
1 parent c74f627 commit aeedcd8

File tree

7 files changed

+100
-22
lines changed

7 files changed

+100
-22
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
on:
2+
workflow_dispatch:
3+
release:
4+
types:
5+
- released
6+
- prereleased
7+
8+
name: pypi-publish
9+
10+
env:
11+
PYTHONUNBUFFERED: "1"
12+
13+
jobs:
14+
pypi-publish:
15+
name: pypi-publish
16+
runs-on: ubuntu-latest
17+
environment:
18+
name: pypi
19+
permissions:
20+
id-token: write
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v5
24+
25+
- name: Install uv
26+
uses: astral-sh/setup-uv@v7
27+
28+
- name: Build Python dist
29+
working-directory: python
30+
run: |
31+
# Copy over any necessary data files
32+
mkdir -p ccao/data/
33+
cp ../data-raw/vars_dict.csv ccao/data/vars_dict.csv
34+
# Build the package
35+
uv build
36+
37+
- name: Publish package distribution to test PyPI
38+
if: ${{ github.event.action == 'prereleased' }}
39+
env:
40+
url: https://test.pypi.org/legacy/p/ccao
41+
uses: pypa/gh-action-pypi-publish@release/v1
42+
with:
43+
repository-url: https://test.pypi.org/legacy/
44+
45+
- name: Publish package distribution to PyPI
46+
if: ${{ github.event.action == 'released' }}
47+
env:
48+
url: https://pypi.org/p/ccao
49+
uses: pypa/gh-action-pypi-publish@release/v1

python/README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ Install the latest release of `ccao` from PyPI:
1515
pip install ccao
1616
```
1717

18-
You can also install the most recent code directly from GitHub:
18+
## Usage
1919

20-
```bash
21-
pip install "git+https://github.com/ccao-data/ccao.git#egg=ccao&subdirectory=python"
22-
```
20+
See [the documentation](https://ccao-data.github.io/ccao/python/)
21+
for detailed API reference.
2322

2423
## Development
2524

@@ -49,3 +48,19 @@ sphinx-autobuild docs/source _build/html
4948
```
5049

5150
Navigate to http://localhost:8000 to view the docs.
51+
52+
### Releasing new versions
53+
54+
We handle package releases on GitHub. The `pypi-publish` workflow watches
55+
for release events and pushes new releases to GitHub.
56+
57+
The workflow watches for two types of releases:
58+
59+
- **Pre-release**: If you mark a release as a pre-release in the GitHub UI,
60+
the workflow will push the package to [the PyPI test
61+
instance](https://test.pypi.org/).
62+
- **Release**: If you cut a normal release in the GitHub UI, the workflow will
63+
publish the package to the prod PyPI instance.
64+
65+
Make sure to put up a PR to bump the package version in `pyproject.toml` before
66+
you cut a release.

python/ccao/data/__init__.py

Whitespace-only changes.

python/ccao/data/vars_dict.csv

Lines changed: 0 additions & 1 deletion
This file was deleted.

python/ccao/vars_funs.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
# Functions for translating variables between different data sources
2-
import importlib.resources
2+
import os
33
import typing
44

55
import pandas as pd
66

7-
import ccao.data
8-
97
# Load the default variable dictionary
10-
_data_path = importlib.resources.files(ccao.data)
11-
vars_dict = pd.read_csv(
12-
str(_data_path / "vars_dict.csv"),
13-
dtype=str,
14-
keep_default_na=False,
15-
na_values=[""],
16-
)
8+
vars_dict_read_kwargs = {
9+
"dtype": "str",
10+
"keep_default_na": False,
11+
"na_values": [""],
12+
}
13+
try:
14+
# Try loading from the canonical location of the file. This file does
15+
# not exist in the source code, but we copy it over prior to building the
16+
# package for PyPI, so it should be available in PyPI installations
17+
vars_dict = pd.read_csv(
18+
os.path.join(
19+
os.path.dirname(os.path.abspath(__file__)), "data", "vars_dict.csv"
20+
),
21+
**vars_dict_read_kwargs,
22+
)
23+
except FileNotFoundError:
24+
# If the file does not exist at the canonical location, we are most likely
25+
# in a local installation of the package, so we can reference the data
26+
# file in the R project. If this also fails, we allow the error to
27+
# propagate
28+
vars_dict = pd.read_csv(
29+
os.path.join(
30+
os.path.dirname(os.path.abspath(__file__)),
31+
"..",
32+
"..",
33+
"data-raw",
34+
"vars_dict.csv",
35+
),
36+
**vars_dict_read_kwargs,
37+
)
1738

1839
# Prefix we use to identify variable name columns in the variable dictionary
1940
VAR_NAME_PREFIX = "var_name"

python/docs/source/index.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,3 @@ Install the latest release of ``ccao`` from PyPI:
3434
.. code-block:: python
3535
3636
pip install ccao
37-
38-
You can also install the most recent code directly from GitHub:
39-
40-
.. code-block:: python
41-
42-
pip install "git+https://github.com/ccao-data/ccao.git#egg=ccao&subdirectory=python"

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "ccao"
33
version = "1.3.0"
44
description = "Convenience Functions and Datasets for the Cook County Assessor's Office"
5-
readme = "README.md"
5+
readme = "docs/source/index.rst"
66
requires-python = ">=3.9"
77
authors = [
88
{name = "Jean Cochrane", email="[email protected]"},

0 commit comments

Comments
 (0)