Skip to content

Commit

Permalink
Allow deleting a Pagefind index in a context, update docs to match
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Sep 29, 2024
1 parent e6c6f5e commit 982708c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
25 changes: 18 additions & 7 deletions docs/content/docs/py-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,28 @@ for file in (await index.get_files()):

## index.write_files

Closing the `PagefindIndex`'s context automatically calls `index.write_files`.
Calling `index.write_files()` writes the index files to disk, as they would be written when running the standard Pagefind binary directly.

If you aren't using `PagefindIndex` as a context manager, calling `index.write_files()` writes the index files to disk, as they would be written when running the standard Pagefind binary directly.
Closing the `PagefindIndex`'s context automatically calls `index.write_files`, so calling this function is not necessary in normal operation.

Calling this function won't prevent files being written when the context closes, which may cause duplicate files to be written.
If calling this function manually, you probably want to also call `index.delete_index()`.

```py
await index = PagefindIndex(
IndexConfig(
output_path="./public/pagefind",
),
config = IndexConfig(
output_path="./public/pagefind",
)
await index.write_files()
async with PagefindIndex(config=config) as index:
# ... add content to index

# write files to the configured output path for the index:
await index.write_files()

# write files to a different output path:
await index.write_files(output_path="./custom/pagefind")

# prevent also writing files when closing the `PagefindIndex`:
await index.delete_index()
```

The `output_path` option should contain the path to the desired Pagefind bundle directory. If relative, is relative to the current working directory of your Python process.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Python API > Close the Pagefind backend
platforms:
- linux
- mac

steps:
- ref: ./background.toolproof.yml
- step: I have a "public/run.py" file with the content {python}
python: |2-
import sys
sys.path.append('%repo_wd%/wrappers/python/src')
import asyncio
import json
import logging
import os
from pagefind.index import PagefindIndex, IndexConfig
async def main():
async with PagefindIndex() as index:
files = await index.get_files()
for file in files:
print(file["path"])
await index.delete_index()
try:
files = await index.get_files()
except AssertionError:
print("errored getting files after close")
if __name__ == "__main__":
asyncio.run(main())
- step: I run "cd public && PAGEFIND_BINARY_PATH=%pagefind_exec_path% python3 run.py"
- step: stdout should contain "pagefind.js"
- step: stdout should contain "pagefind-ui.js"
- step: stdout should contain "errored getting files after close"
6 changes: 4 additions & 2 deletions wrappers/python/src/pagefind/index/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ async def __aexit__(
exc_value: Optional[Any],
traceback: Optional[Any],
) -> None:
assert self._service is not None
assert self._index_id is not None
if self._service is None:
return
if self._index_id is None:
return
if exc_type is None:
await self.write_files()
await self._service.close()

0 comments on commit 982708c

Please sign in to comment.