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

auto-generate toctree from flytesnacks index.md docs #4587

Merged
merged 5 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
59 changes: 59 additions & 0 deletions docs/_ext/import_projects.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import os
import re
import shutil
Expand All @@ -9,8 +10,11 @@
from typing import Optional, List, Union

from git import Repo
from docutils import nodes
from docutils.statemachine import StringList, string2lines
from sphinx.application import Sphinx
from sphinx.config import Config
from sphinx.util.docutils import SphinxDirective

__version__ = "0.0.0"

Expand All @@ -20,6 +24,7 @@ class ImportProjectsConfig:
clone_dir: str
flytekit_api_dir: str
source_regex_mapping: dict = field(default_factory=dict)
list_table_toc: List[str] = field(default_factory=list)


@dataclass
Expand All @@ -32,6 +37,46 @@ class Project:
refresh: bool = False


TOC_TEMPLATE = """
```{{toctree}}
:maxdepth: 1
:hidden:
{toc}
```
"""

TABLE_TEMPLATE = """
```{{list-table}}
{content}
```
"""


class ListTableToc(SphinxDirective):
"""Custom directive to convert list-table into both list-table and toctree."""

has_content = True

def run(self) -> list:
return [self.parse()]

def parse(self):
"""Parses the list-table and adds a toctree"""
toc = ""

# finds all doc references in the form <doc/path>
for doc in re.findall(r"<(.+)>", self.block_text):
toc += f"\n{doc}"

container = nodes.container("")
toc = inspect.cleandoc(TOC_TEMPLATE.format(toc=toc))
table = inspect.cleandoc(TABLE_TEMPLATE.format(content=self.block_text))
content = f"{toc}\n\n{table}"

self.state.nested_parse(StringList(string2lines(content)), 0, container)
return container


def update_sys_path_for_flytekit(import_project_config: ImportProjectsConfig):
# create flytekit/_version.py file manually
with open(f"{import_project_config.flytekit_api_dir}/flytekit/_version.py", "w") as f:
Expand Down Expand Up @@ -132,10 +177,24 @@ def replace_refs_in_docstrings(
lines[i] = text


def add_list_table_toc(app: Sphinx, docname: str, source: List[str]):
"""This replaces list-table directives in specific documents with list-table-toc.

This is important for automatically generating a toctree and list-table from
a list-table.
"""
if docname in app.config.import_projects_config["list_table_toc"]:
text = source[0]
text = re.sub(r"{list-table}", r"{list-table-toc}", text)
source[0] = text


def setup(app: Sphinx) -> dict:
app.add_config_value("import_projects_config", None, False)
app.add_config_value("import_projects", None, False)
app.add_directive("list-table-toc", ListTableToc)
app.connect("config-inited", import_projects, priority=0)
app.connect("source-read", add_list_table_toc, priority=0)

return {
"version": __version__,
Expand Down
12 changes: 9 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@
"flytesnacks/bioinformatics_examples.md",
"flytesnacks/feature_engineering.md",
"flytesnacks/flyte_lab.md",
"flytesnacks/integrations.md",
"flytesnacks/ml_training.md",
"flytesnacks/tutorials.md",
"flytesnacks/userguide.md",
"flytesnacks/README.md",
"flytekit/**/README.md",
"flytekit/_templates/**",
Expand Down Expand Up @@ -313,8 +310,10 @@
# These patterns are used to replace values in source files that are imported
# from other repos.
REPLACE_PATTERNS = {
r"<flyte:deployment/index>": r"</deployment/index>",
r"<flytectl:index>": r"</flytectl_overview>",
INTERSPHINX_REFS_PATTERN: INTERSPHINX_REFS_REPLACE,
r"<auto_examples": r"<flytesnacks/examples",
r"<protos/docs/core/core:taskmetadata>": r"<ref_flyteidl.core.TaskMetadata>",
r"<protos/docs/core/core:tasktemplate>": r"<ref_flyteidl.core.TaskTemplate>",
r"<flytesnacks/examples": r"</flytesnacks/examples",
Expand All @@ -325,12 +324,19 @@
r"{ref}`bioinformatics <bioinformatics>`": r"bioinformatics",
PROTO_REF_PATTERN: PROTO_REF_REPLACE,
r"/protos/docs/service/index": r"/protos/docs/service/service",
r"<weather_forecasting>": r"</flytesnacks/weather_forecasting>",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If flytesnacks adds another ref in docs/tutorials.md, we would still need to add a new entry in flyte. If this is true, can we document this somewhere?

An alternative is to parse tutorials.md and friends for anything that is <SINGLE_WORD> and replace it with <flytesnacks/SINGLE_WORD>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no we don't, the weather_forecasting page is really the only exception to this (tbh it should really be an example). In general all the docs should follow the <auto_examples/... pattern above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see weather_forecasting and environment_setup having this pattern. For my understanding, if one adds a <mlops_setup> in docs/tutorials.md do we need to add the following to REPLACE_PATTERNS?

r"<mlops_setup>": r"<flytesnacks/mlops_setup>"

Or we want this to be an anti-pattern and so this should not happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is an anti-pattern that we need to fix more systematically.

Eventually we can can specify the list-table-toc directive in these files and assume that all refs in these directives follow the pattern <flytesnacks/examples/...>. Any ref outside of list-table-toc won't be treated specially.

Basically any entries listed under list-table-toc in tutorials, userguide, and integrations ought to follow the example template described here: https://docs.flyte.org/projects/cookbook/en/latest/contribute.html

r"<environment_setup>": r"</flytesnacks/environment_setup>"
}

import_projects_config = {
"clone_dir": "_projects",
"flytekit_api_dir": "_src/flytekit/",
"source_regex_mapping": REPLACE_PATTERNS,
"list_table_toc": [
"flytesnacks/userguide",
"flytesnacks/tutorials",
"flytesnacks/integrations",
],
}

# Define these environment variables to use local copies of the projects. This
Expand Down
12 changes: 6 additions & 6 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ contribute its architecture and design. You can also access the
* - {doc}`🔤 Intro to Flyte <introduction>`
- Get your first workflow running, learn about the Flyte development lifecycle
and core use cases.
* - {doc}`📖 User Guide <userguide>`
* - {doc}`📖 User Guide <flytesnacks/userguide>`
- A comprehensive view of Flyte's functionality for data and ML practitioners.
* - {doc}`📚 Tutorials <tutorials>`
* - {doc}`📚 Tutorials <flytesnacks/tutorials>`
- End-to-end examples of Flyte for data/feature engineering, machine learning,
bioinformatics, and more.
* - {doc}`🔌 Integrations <integrations>`
* - {doc}`🔌 Integrations <flytesnacks/integrations>`
- Leverage a rich ecosystem of third-party tools and libraries to make your
Flyte workflows even more effective.
* - {ref}`🚀 Deployment Guide <deployment>`
Expand Down Expand Up @@ -145,9 +145,9 @@ Core Use Cases <flytesnacks/getting_started/core_use_cases>
:name: examples-guides
:hidden:

User Guide <userguide>
Tutorials <tutorials>
Integrations <integrations>
User Guide <flytesnacks/userguide>
Tutorials <flytesnacks/tutorials>
Integrations <flytesnacks/integrations>
```

```{toctree}
Expand Down
216 changes: 0 additions & 216 deletions docs/integrations.md

This file was deleted.

Loading
Loading