Pagefind

1.5.0 · active · verified Sat Apr 11

Pagefind is a library for performant, low-bandwidth, fully static search. The `pagefind-bin` Python package provides a convenient wrapper to install and manage the Pagefind binary for use in Python projects, making its CLI tools accessible. The current version is 1.5.0, and it typically releases updates in sync with the upstream Pagefind binary releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install the Pagefind CLI binary using `pagefind-bin`, retrieve its path, and then execute a Pagefind command via `subprocess` to build a search index for a static site. Remember to replace 'my_static_site_output' with your actual static site's build directory.

import os
import subprocess
from pagefind_bin import PagefindBinary

# 1. Initialize and install the Pagefind binary
pagefind = PagefindBinary()
# It's recommended to pin the version for production to avoid unexpected updates:
# pagefind.install(version="1.x.x")
pagefind.install()

# 2. Get the path to the installed binary
pagefind_cli_path = pagefind.path()
print(f"Pagefind CLI installed at: {pagefind_cli_path}")

# 3. Use the Pagefind CLI to build an index (example)
# For this example to work, you'd need some static files in 'my_static_site_output'.
# Replace 'my_static_site_output' with your actual static site build directory.
source_directory = "my_static_site_output" # e.g., 'build', '_site', 'public'
output_directory = os.path.join(source_directory, "pagefind") # Default output dir for Pagefind

# Create a dummy directory and file for demonstration if it doesn't exist
if not os.path.exists(source_directory):
    os.makedirs(source_directory, exist_ok=True)
    with open(os.path.join(source_directory, "index.html"), "w") as f:
        f.write("<html><body><h1>Hello Pagefind!</h1></body></html>")
    print(f"Created dummy static site at {source_directory}")

try:
    print(f"Running Pagefind build on {source_directory}...")
    command = [pagefind_cli_path, "--source", source_directory, "--output", output_directory]
    result = subprocess.run(command, capture_output=True, text=True, check=True)
    print("Pagefind build successful.")
    print("STDOUT:", result.stdout)
    if result.stderr:
        print("STDERR:", result.stderr)
    print(f"Search index generated in {output_directory}")
except subprocess.CalledProcessError as e:
    print(f"Pagefind build failed: {e}")
    print("STDOUT:", e.stdout)
    print("STDERR:", e.stderr)
except FileNotFoundError:
    print(f"Error: Pagefind binary not found at {pagefind_cli_path}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →