Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python wrapper, API #672

Merged
merged 43 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6b6096c
feat(wrappers/python): draft python wrapper
SKalt Jul 2, 2024
1f83df3
feat(wrappers/python): complete integration test
SKalt Jul 5, 2024
0a26eab
Merge remote-tracking branch 'trunk/main' into python-bindings
SKalt Jul 16, 2024
c0b21f3
Merge remote-tracking branch 'trunk/main' into python-bindings
SKalt Aug 3, 2024
1551d80
feat(python): script building binary-only wheels
SKalt Aug 4, 2024
8478ccb
refactor(python): remove 'experimental' prefix from python package names
SKalt Aug 4, 2024
6185637
Merge branch 'main' into python-bindings
bglw Aug 18, 2024
ecdb22c
chore(python): WIP backport of changes from skalt/pagefind_python
SKalt Aug 20, 2024
6cdcdf2
build(wrappers/python): add python build scripts for GH Actions
SKalt Aug 24, 2024
dab6f27
feat(wrappers/python): add logging, timeout to process shutdown
SKalt Aug 24, 2024
e670454
ci(wrappers/python): make sure scripts are executable
SKalt Aug 24, 2024
3e1637f
chore(wrappers/python): update poetry.lock
SKalt Aug 25, 2024
54f70ad
fix(wrappers/python): trivial type error
SKalt Aug 25, 2024
2a5512a
ci(wrappers/python): correct path to non-github-specific script
SKalt Aug 25, 2024
ef41fa5
ci(wrappers/python): debug path to mypy on windows
SKalt Sep 25, 2024
112f4a8
ci(wrappers/python): avoid invoking external python packages on windows
SKalt Sep 25, 2024
8109fdd
docs(wrappers/python): document python API
SKalt Sep 25, 2024
6cdf2d9
ci(wrappers/python): explore powershell venv activation script
SKalt Sep 25, 2024
a6f5c57
ci(wrappers/python): fix windows venv activation
SKalt Sep 28, 2024
8f58c1d
ci(wrappers/python): prepare to publish
SKalt Sep 28, 2024
89c795c
docs(wrappers/python): add docstrings, check links
SKalt Sep 28, 2024
7f670f0
ci(wrappers/python): fix PYTHONPATH before windows integration tests
SKalt Sep 28, 2024
27032b2
ci(wrappers/python): more logging for python build scripts
SKalt Sep 28, 2024
c3aaaed
Merge branch 'main' into python-bindings
bglw Sep 29, 2024
c929fce
Decode the files returned from `get_files` in the Python API
bglw Sep 29, 2024
afd9074
Check `PAGEFIND_BINARY_PATH` in the Python API
bglw Sep 29, 2024
d66e887
Update toolproof
bglw Sep 29, 2024
0d495b3
Add first Python API Toolproof test
bglw Sep 29, 2024
72595ca
Remove `source` command
bglw Sep 29, 2024
8ad5f0e
Install Python earlier in CI
bglw Sep 29, 2024
cf1e868
Python API: Allow `write_files` to take an optional `output_path`
bglw Sep 29, 2024
39613f6
Add Python API toolproof test
bglw Sep 29, 2024
3d81b64
Fix Python lint
bglw Sep 29, 2024
6b4684a
Toolproof: "Python API > Build a blended index to memory via the api"
bglw Sep 29, 2024
2139861
Python formatting
bglw Sep 29, 2024
e6c6f5e
Python API: Fix `DeleteIndex` response
bglw Sep 29, 2024
982708c
Allow deleting a Pagefind index in a context, update docs to match
bglw Sep 29, 2024
a53f3c2
Python API: Improve error handling for messages that failed to parse
bglw Sep 29, 2024
035f7a7
Final tranche of Toolproof Python API tests to meet Node API parity
bglw Sep 29, 2024
51118da
Note down useful testing script for local Python API integration tests
bglw Sep 29, 2024
38b3461
Flesh out documentation around Python installs
bglw Oct 2, 2024
9bdbe2d
Finalize Python release scripts and workflow
bglw Sep 30, 2024
be66cf5
Python formatting
bglw Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 74 additions & 12 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:

env:
CARGO_TERM_COLOR: always
HUMANE_VERSION: "0.9.0"
WASM_PACK_VERSION: "v0.10.3"

jobs:
Expand Down Expand Up @@ -87,6 +86,77 @@ jobs:
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

publish-python-packages:
name: Publish python packages
runs-on: ubuntu-latest # ok since none of the scripts depend on version-specific features
defaults:
run:
shell: bash
needs: publish-github-release
permissions: # required for trusted publishing to pypi/test-pypi
id-token: write
steps:
- name: Clone
uses: actions/checkout@v4
- name: Download CLI binaries
uses: actions/download-artifact@v4
with:
pattern: release-*
merge-multiple: true
path: ./wrappers/python/vendor
- name: Set up python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Set up poetry
run: ./wrappers/python/scripts/ci/github/setup_poetry.sh

- name: cache venv
uses: actions/cache@v4
with:
path: wrappers/python/.venv
key: ${{ runner.os }}-poetry-3.12-${{ hashFiles('**/poetry.lock') }}

- name: Install dev dependencies
run: ./wrappers/python/scripts/ci/github/install_dev_dependencies.sh

- name: activate venv
run: ./wrappers/python/scripts/ci/github/activate_venv.sh

- name: debug python paths
run: ./wrappers/python/scripts/ci/github/debug_python_paths.sh

- name: package binaries
working-directory: ./wrappers/python
run: | # should take ~30s; writes wheels to wrappers/python/dist
export PAGEFIND_PYTHON_LOG_LEVEL=DEBUG
python -m scripts.build.all_binary_only_wheels \
--tag "${{ github.ref_name }}" \
--bin-dir ./vendor
- name: package python api
working-directory: ./wrappers/python
run: | # writes stdist + wheel to wrappers/python/dist
export PAGEFIND_PYTHON_LOG_LEVEL=DEBUG
python -m scripts.build.api_package --tag "${{ github.ref_name }}"
- name: Archive dist
uses: actions/upload-artifact@v4
with:
path: wrappers/python/dist
name: python-packages
if-no-files-found: error

- name: Publish to pypi
uses: pypa/gh-action-pypi-publish@release/v1
# Note: this action requires test-pypi / pypi trusted publishing to be
# configured in the target repository. For instructions, see
# - https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/configuring-openid-connect-in-pypi
# - https://docs.pypi.org/trusted-publishers/
with: # see https://github.com/pypa/gh-action-pypi-publish/tree/release/v1/?tab=readme-ov-file#customizing-target-package-dists-directory
packages-dir: wrappers/python/dist
# repository-url: https://test.pypi.org/legacy/ # Use to publish test packages
verbose: true # can be commented out once the action is working as expected

publish-npm-package:
name: Publish NPM packages
runs-on: ubuntu-20.04
Expand Down Expand Up @@ -321,35 +391,30 @@ jobs:
os: windows-latest
rust: stable
target: x86_64-pc-windows-msvc
humane_target: x86_64-pc-windows-msvc
cross: false
run_tests: true
- build: linux
os: ubuntu-latest
rust: stable
target: x86_64-unknown-linux-musl
humane_target: x86_64-unknown-linux-musl
cross: false
run_tests: true
- build: linux
os: ubuntu-latest
rust: stable
target: aarch64-unknown-linux-musl
humane_target: x86_64-unknown-linux-musl
cross: false
run_tests: false
- build: macos
os: macos-latest
rust: stable
target: x86_64-apple-darwin
humane_target: x86_64-apple-darwin
cross: false
run_tests: true
- build: macos-m1
os: macos-latest
rust: stable
target: aarch64-apple-darwin
humane_target: x86_64-apple-darwin
cross: false
run_tests: false
steps:
Expand Down Expand Up @@ -417,13 +482,10 @@ jobs:
with:
version: ${{env.WASM_PACK_VERSION}}

- name: Install humane
if: matrix.run_tests == true
uses: supplypike/setup-bin@v3
- name: Set up python 3.12
uses: actions/setup-python@v5
with:
uri: "https://github.com/CloudCannon/humane/releases/download/v${{env.HUMANE_VERSION}}/humane-v${{env.HUMANE_VERSION}}-${{matrix.humane_target}}.tar.gz"
name: "humane"
version: ${{env.HUMANE_VERSION}}
python-version: "3.12"

- name: Prepare Git
run: |
Expand Down
50 changes: 50 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ jobs:
default: true
components: rustfmt, clippy

- name: Set up python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.12"
# NOTE: ^this strategy leaves older python versions intentionally
# without test coverage to keep CI fast.

- name: Check versions
run: |
cargo --version
Expand Down Expand Up @@ -111,3 +118,46 @@ jobs:

- name: Test CLI
run: ./test_ci.sh "release"

- name: Set up poetry
run: ./wrappers/python/scripts/ci/github/setup_poetry.sh

- name: cache venv
uses: actions/cache@v4
with:
path: wrappers/python/.venv
key: ${{ runner.os }}-poetry-3.12-${{ hashFiles('**/poetry.lock') }}

- name: Install dev dependencies
run: ./wrappers/python/scripts/ci/github/install_dev_dependencies.sh

- name: activate venv
run: ./wrappers/python/scripts/ci/github/activate_venv.sh

- name: debug python paths
run: ./wrappers/python/scripts/ci/github/debug_python_paths.sh

- name: Lint python
# avoid duplicating linting work on different OSes
if: runner.os == 'Linux'
working-directory: ./wrappers/python
run: ./scripts/ci/python_lints.sh

- name: ensure cog up-to-date
# avoid duplicating linting work on different OSes
if: runner.os == 'Linux'
working-directory: ./wrappers/python
run: ./scripts/ci/cog/check.sh

- name: set PYTHONPATH
shell: bash
working-directory: ./wrappers/python
run: python ./scripts/ci/github/add_src_to_pythonpath.py
- name: add target/release to windows PATH
shell: bash
run: echo $PWD/target/release >> "$GITHUB_PATH"
- name: Test python API
timeout-minutes: 1
# ^ guard against the tests getting deadlock if the subprocess pipe
# gets clogged
run: ./wrappers/python/scripts/ci/github/integration_tests.sh
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ vendor

# Node
node_modules

# Python
__pycache__/
*.pyc
dist
*.whl
*.egg-info
*.log
4 changes: 4 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
external-sources=true
source-path=SCRIPTDIR
disable=SC2002
# SC2002: ignore "useless cat" warning: starting pipes with `cat` improves composability
9 changes: 7 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"rust-analyzer.showUnlinkedFileNotification": false
}
"rust-analyzer.showUnlinkedFileNotification": false,
"python.analysis.typeCheckingMode": "standard",
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
13 changes: 10 additions & 3 deletions docs/content/docs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,22 @@ Now build your site to an output directory — this guide assumes that you're ru

## Indexing your site

The easiest way to run pagefind is through npx. If you don't have Node and npm installed, or want to install Pagefind another way, see the [Installing Pagefind](/docs/installation/) guide.
The easiest way to run Pagefind is through one of the official wrapper packages. If you don't have Node or Python installed, or want to install Pagefind another way, see the [Installing Pagefind](/docs/installation/) guide.

Run the following command from your terminal, where `--site` points to the output directory of your static site generator. We'll also add `--serve` so that we can view our final site right away.
To use the Node wrapper, run the following command from your terminal, where `--site` points to the output directory of your static site generator. We'll also add `--serve` so that we can view our final site right away.

```bash
npx -y pagefind --site public --serve
```

You should see some output along the lines of:
Using the Python wrapper is similar, but requires an initial install:

```bash
python3 -m pip install 'pagefind[extended]'
python3 -m pagefind --site public --serve
```

Regardless of the command you choose, after Pagefind has downloaded you should see some output along the lines of:
```
Indexed 2496 pages
Indexed 22852 words
Expand Down
38 changes: 34 additions & 4 deletions docs/content/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,55 @@ nav_section: References
weight: 49
---

Pagefind is a static binary with no dynamic dependencies, so in most cases will be simple to install and run. Pagefind is currently supported on Windows, macOS, and x86-64 Linux distributions.
Pagefind is a static binary with no dynamic dependencies, so in most cases will be simple to install and run. Pagefind is currently supported on Windows, macOS, and Linux distributions.

## Running via npx

For users with a NodeJS toolchain already installed, Pagefind publishes a [wrapper package through npm](https://www.npmjs.com/package/pagefind):

```bash
npx pagefind --site "public"
```

Pagefind publishes a [wrapper package through npm](https://www.npmjs.com/package/pagefind), which is the easiest way to get started. This package will download the correct [binary of the latest release](https://github.com/CloudCannon/pagefind/releases) as an npm dependency for your platform and run it.
This package includes the correct [binary of the relevant release](https://github.com/CloudCannon/pagefind/releases) as a dependency for your platform.

Specific versions can be run by passing a version tag:

```bash
npx pagefind@latest --site "public"

npx pagefind@v0.2.0 --site "public"
npx pagefind@v1.1.1 --site "public"
```

Running Pagefind via npx will always download the `pagefind_extended` release, which includes specialized support for indexing Chinese and Japanese pages.

> Pagefind's npm package can also be imported and controlled from a script. See the [Node API documentation](/docs/node-api/) for details.

## Running via Python

For users with a Python toolchain already installed, Pagefind publishes a [wrapper package through pypi](https://pypi.org/project/pagefind/):

```bash
python3 -m pip install 'pagefind[extended]'
python3 -m pagefind --site "public"
```

This package includes the correct [binary of the relevant release](https://github.com/CloudCannon/pagefind/releases) as a dependency for your platform.

Specific versions can be installed by passing a version:

```bash
python3 -m pip install 'pagefind[extended]==1.1.1'
```

The above example shows installing the `pagefind_extended` release, which includes specialized support for indexing Chinese and Japanese pages.
To install the smaller standard release, run:

```bash
python3 -m pip install 'pagefind[bin]'
```

> Running Pagefind via npx will download the `pagefind_extended` release, which includes specialized support for indexing Chinese and Japanese pages.
> Pagefind's Python package can also be imported and controlled from a script. See the [Python API documentation](/docs/py-api/) for details.

## Downloading a precompiled binary

Expand Down
Loading
Loading