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

Bagatur/api styling #25172

Merged
merged 74 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
79f85d2
wip
baskaryan Jul 31, 2024
5cc283d
wip
baskaryan Jul 31, 2024
477348f
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 6, 2024
ab05745
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 6, 2024
37d3525
merge
baskaryan Aug 6, 2024
19fb035
wip
baskaryan Aug 6, 2024
85e262a
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 7, 2024
e193e68
wip
baskaryan Aug 8, 2024
274f3a6
update reqs
baskaryan Aug 8, 2024
b54c132
myst-nb
baskaryan Aug 8, 2024
eecf95e
build
baskaryan Aug 8, 2024
5bef45b
fix
baskaryan Aug 8, 2024
0b87869
fix
baskaryan Aug 8, 2024
c981fa2
make
baskaryan Aug 8, 2024
7c98615
fmt
baskaryan Aug 8, 2024
2ea67c6
fmt
baskaryan Aug 8, 2024
d704b72
fmt
baskaryan Aug 8, 2024
3ec9144
fmt
baskaryan Aug 8, 2024
f009a2a
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 8, 2024
9b29b1f
fmt
baskaryan Aug 8, 2024
425936e
fmt
baskaryan Aug 8, 2024
bfddb7e
fmt
baskaryan Aug 8, 2024
a77370c
fmt
baskaryan Aug 8, 2024
6bcef7d
rm
baskaryan Aug 8, 2024
436c3a3
fmt
baskaryan Aug 8, 2024
72f93ad
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 8, 2024
3bb64b6
fmt
baskaryan Aug 8, 2024
fd453a3
fmt
baskaryan Aug 8, 2024
f60df4f
fmt
baskaryan Aug 9, 2024
88c0164
fmt
baskaryan Aug 9, 2024
6e1d295
fmt
baskaryan Aug 9, 2024
567c673
fmt
baskaryan Aug 9, 2024
ee3cad6
fmt
baskaryan Aug 9, 2024
3c82313
fmt
baskaryan Aug 9, 2024
a00eca4
fmt
baskaryan Aug 9, 2024
f7e8e1e
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 9, 2024
93fdf8b
fmt
baskaryan Aug 9, 2024
012bf23
fmt
baskaryan Aug 9, 2024
489a32b
sphinx
baskaryan Aug 9, 2024
6f3140c
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 11, 2024
0526a6c
fmt
baskaryan Aug 11, 2024
8cae941
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 11, 2024
0b1067a
fmt
baskaryan Aug 11, 2024
6a2b1a5
fmt
baskaryan Aug 12, 2024
77f325a
fmt
baskaryan Aug 12, 2024
a5b4618
fmt
baskaryan Aug 12, 2024
979baeb
fmt
baskaryan Aug 12, 2024
6d8d148
fmt
baskaryan Aug 12, 2024
2ce5a45
fmt
baskaryan Aug 12, 2024
2c91579
fmt
baskaryan Aug 12, 2024
3336759
fmt
baskaryan Aug 12, 2024
4e3f9ed
fmt
baskaryan Aug 12, 2024
8cc84e5
fmt
baskaryan Aug 12, 2024
1040893
fmt
baskaryan Aug 12, 2024
9c37d41
fmt
baskaryan Aug 12, 2024
a35ee89
fmt
baskaryan Aug 12, 2024
c634382
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 12, 2024
eb03dce
fmt
baskaryan Aug 12, 2024
9a05829
fmt
baskaryan Aug 13, 2024
f4002ff
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 13, 2024
c8e544d
fmt
baskaryan Aug 13, 2024
234e863
Merge branch 'bagatur/api_styling' of github.com:langchain-ai/langcha…
baskaryan Aug 13, 2024
b415f2f
fmt
baskaryan Aug 13, 2024
4b1299b
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 13, 2024
2396461
fmt
baskaryan Aug 13, 2024
aef238f
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 13, 2024
ee96576
fmt
baskaryan Aug 14, 2024
7c5d676
Merge branch 'bagatur/api_styling' of github.com:langchain-ai/langcha…
baskaryan Aug 14, 2024
6310383
fmt
baskaryan Aug 14, 2024
f0ed7a4
fmt
baskaryan Aug 14, 2024
f9234ad
fmt
baskaryan Aug 14, 2024
8087797
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 14, 2024
1c1545d
Merge branch 'master' into bagatur/api_styling
baskaryan Aug 14, 2024
ab6eca1
Merge branch 'bagatur/api_styling' of github.com:langchain-ai/langcha…
baskaryan Aug 14, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ docs/api_reference/*/
!docs/api_reference/_static/
!docs/api_reference/templates/
!docs/api_reference/themes/
!docs/api_reference/_extensions/
!docs/api_reference/scripts/
docs/docs/build
docs/docs/node_modules
docs/docs/yarn.lock
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ docs_linkcheck:
api_docs_build:
poetry run python docs/api_reference/create_api_rst.py
cd docs/api_reference && poetry run make html
poetry run python docs/api_reference/scripts/custom_formatter.py docs/api_reference/_build/html/

API_PKG ?= text-splitters

api_docs_quick_preview:
poetry run pip install "pydantic<2"
poetry run python docs/api_reference/create_api_rst.py $(API_PKG)
cd docs/api_reference && poetry run make html
open docs/api_reference/_build/html/$(shell echo $(API_PKG) | sed 's/-/_/g')_api_reference.html
poetry run python docs/api_reference/scripts/custom_formatter.py docs/api_reference/_build/html/
open docs/api_reference/_build/html/reference.html

## api_docs_clean: Clean the API Reference documentation build artifacts.
api_docs_clean:
find ./docs/api_reference -name '*_api_reference.rst' -delete
git clean -fdX ./docs/api_reference
rm docs/api_reference/index.md


## api_docs_linkcheck: Run linkchecker on the API Reference documentation.
Expand Down
4 changes: 4 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ vercel-build: install-vercel-deps build generate-references
rm -rf docs
mv $(OUTPUT_NEW_DOCS_DIR) docs
rm -rf build
mkdir static/api_reference
git clone --depth=1 https://github.com/baskaryan/langchain-api-docs-build.git
mv langchain-api-docs-build/api_reference_build/html/* static/api_reference/
rm -rf langchain-api-docs-build
NODE_OPTIONS="--max-old-space-size=5000" yarn run docusaurus build
mv build v0.2
mkdir build
Expand Down
144 changes: 144 additions & 0 deletions docs/api_reference/_extensions/gallery_directive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
"""A directive to generate a gallery of images from structured data.

Generating a gallery of images that are all the same size is a common
pattern in documentation, and this can be cumbersome if the gallery is
generated programmatically. This directive wraps this particular use-case
in a helper-directive to generate it with a single YAML configuration file.

It currently exists for maintainers of the pydata-sphinx-theme,
but might be abstracted into a standalone package if it proves useful.
"""

from pathlib import Path
from typing import Any, ClassVar, Dict, List

from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.application import Sphinx
from sphinx.util import logging
from sphinx.util.docutils import SphinxDirective
from yaml import safe_load

logger = logging.getLogger(__name__)


TEMPLATE_GRID = """
`````{{grid}} {columns}
{options}

{content}

`````
"""

GRID_CARD = """
````{{grid-item-card}} {title}
{options}

{content}
````
"""


class GalleryGridDirective(SphinxDirective):
"""A directive to show a gallery of images and links in a Bootstrap grid.

The grid can be generated from a YAML file that contains a list of items, or
from the content of the directive (also formatted in YAML). Use the parameter
"class-card" to add an additional CSS class to all cards. When specifying the grid
items, you can use all parameters from "grid-item-card" directive to customize
individual cards + ["image", "header", "content", "title"].

Danger:
This directive can only be used in the context of a Myst documentation page as
the templates use Markdown flavored formatting.
"""

name = "gallery-grid"
has_content = True
required_arguments = 0
optional_arguments = 1
final_argument_whitespace = True
option_spec: ClassVar[dict[str, Any]] = {
# A class to be added to the resulting container
"grid-columns": directives.unchanged,
"class-container": directives.unchanged,
"class-card": directives.unchanged,
}

def run(self) -> List[nodes.Node]:
"""Create the gallery grid."""
if self.arguments:
# If an argument is given, assume it's a path to a YAML file
# Parse it and load it into the directive content
path_data_rel = Path(self.arguments[0])
path_doc, _ = self.get_source_info()
path_doc = Path(path_doc).parent
path_data = (path_doc / path_data_rel).resolve()
if not path_data.exists():
logger.info(f"Could not find grid data at {path_data}.")
nodes.text("No grid data found at {path_data}.")
return
yaml_string = path_data.read_text()
else:
yaml_string = "\n".join(self.content)

# Use all the element with an img-bottom key as sites to show
# and generate a card item for each of them
grid_items = []
for item in safe_load(yaml_string):
# remove parameters that are not needed for the card options
title = item.pop("title", "")

# build the content of the card using some extra parameters
header = f"{item.pop('header')} \n^^^ \n" if "header" in item else ""
image = f"![image]({item.pop('image')}) \n" if "image" in item else ""
content = f"{item.pop('content')} \n" if "content" in item else ""

# optional parameter that influence all cards
if "class-card" in self.options:
item["class-card"] = self.options["class-card"]

loc_options_str = "\n".join(f":{k}: {v}" for k, v in item.items()) + " \n"

card = GRID_CARD.format(
options=loc_options_str, content=header + image + content, title=title
)
grid_items.append(card)

# Parse the template with Sphinx Design to create an output container
# Prep the options for the template grid
class_ = "gallery-directive" + f' {self.options.get("class-container", "")}'
options = {"gutter": 2, "class-container": class_}
options_str = "\n".join(f":{k}: {v}" for k, v in options.items())

# Create the directive string for the grid
grid_directive = TEMPLATE_GRID.format(
columns=self.options.get("grid-columns", "1 2 3 4"),
options=options_str,
content="\n".join(grid_items),
)

# Parse content as a directive so Sphinx Design processes it
container = nodes.container()
self.state.nested_parse([grid_directive], 0, container)

# Sphinx Design outputs a container too, so just use that
return [container.children[0]]


def setup(app: Sphinx) -> Dict[str, Any]:
"""Add custom configuration to sphinx app.

Args:
app: the Sphinx application

Returns:
the 2 parallel parameters set to ``True``.
"""
app.add_directive("gallery-grid", GalleryGridDirective)

return {
"parallel_read_safe": True,
"parallel_write_safe": True,
}
Loading
Loading