Skip to content

Commit

Permalink
docs(algolia): add methods from backend pages to algolia index
Browse files Browse the repository at this point in the history
The backend-specific methods are added to the backend pages
programmatically using `quarto`.  That seems to lead to them not showing
up in our Algolia search, because they aren't being scraped.

It's a little gross, but I've added in a section to our api docs
rendering scripts to also dump the corresponding records for each method
into a backend-specific json file during `quarto` rendering.

Then those backend-specific records files are loaded and added to the
Algolia index in `upload-algolia-api.py`
  • Loading branch information
gforsyth committed Jul 16, 2024
1 parent 09cfd85 commit c840ca5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .github/workflows/upload-algolia-api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations # noqa: INP001

import glob
import json
import os
import re
from functools import partial
Expand Down Expand Up @@ -94,6 +96,19 @@ def main():
print(f"Uploading {len(records)} records to {index.name=}") # noqa:T201
index.save_objects(records)

# Methods documented on backend-specific docs pages aren't scraped by Quarto
# since we construct them programmatically.
# There is a hook in docs/backends/_templates/api.qmd that calls
# `dump_methods_to_json_for_algolia` that serializes all the backend methods
# to a backend-specific json file in docs/backends/
# (Not Pandas and Impala because those backend pages don't use the template)
#
# Here, we load those records and upload them to the Algolia index
for record_json in glob.glob("docs/backends/*.json"):
with open(record_json) as f:
records = json.load(f)
index.save_objects(records)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ objects.json
# generated notebooks and files
*.ipynb
*_files
backends/*_methods.json

# inventories
_inv
Expand Down
4 changes: 3 additions & 1 deletion docs/backends/_templates/api.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#| echo: false
#| output: asis
from _utils import get_backend, render_methods
from _utils import get_backend, render_methods, dump_methods_to_json_for_algolia
# defined in the backend qmd, e.g., ../bigquery.qmd
module = BACKEND.lower()
Expand All @@ -17,5 +17,7 @@ methods = sorted(
if value.name != "do_connect"
)
dump_methods_to_json_for_algolia(backend, methods)
render_methods(backend, *methods, level=3)
```
22 changes: 22 additions & 0 deletions docs/backends/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import json
from functools import cache, partial
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -78,3 +79,24 @@ def render_methods(obj, *methods: str, level: int) -> None:

def render_do_connect(backend, level: int = 4) -> None:
render_methods(get_backend(backend), "do_connect", level=level)


def dump_methods_to_json_for_algolia(backend, methods):
backend_algolia_methods = list()
backend_name = backend.canonical_path.split(".")[2]
base_url_template = "backends/{backend}#ibis.backends.{backend}.Backend.{method}"

for method in methods:
base_url = base_url_template.format(backend=backend_name, method=method)
record = {
"objectID": base_url,
"href": base_url,
"title": f"{backend_name}.Backend.{method}",
"text": getattr(backend.all_members[method].docstring, "value", ""),
"crumbs": ["Backend API", "API", f"{backend_name} methods"],
}

backend_algolia_methods.append(record)

with open(f"{backend_name}_methods.json", "w") as f:
json.dump(backend_algolia_methods, f)

0 comments on commit c840ca5

Please sign in to comment.