Skip to content

Commit

Permalink
Determine documentation group automatically (#7062)
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Feb 5, 2024
1 parent 2e6672c commit 0fddd0f
Show file tree
Hide file tree
Showing 60 changed files with 107 additions and 197 deletions.
6 changes: 6 additions & 0 deletions .changeset/easy-nights-fly.md
@@ -0,0 +1,6 @@
---
"gradio": minor
"gradio_client": minor
---

feat:Determine documentation group automatically
5 changes: 1 addition & 4 deletions client/python/gradio_client/client.py
Expand Up @@ -31,7 +31,7 @@
from packaging import version

from gradio_client import serializing, utils
from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document
from gradio_client.exceptions import SerializationSetupError
from gradio_client.utils import (
Communicator,
Expand All @@ -43,9 +43,6 @@
StatusUpdate,
)

set_documentation_group("py-client")


DEFAULT_TEMP_DIR = os.environ.get("GRADIO_TEMP_DIR") or str(
Path(tempfile.gettempdir()) / "gradio"
)
Expand Down
50 changes: 40 additions & 10 deletions client/python/gradio_client/documentation.py
Expand Up @@ -3,18 +3,13 @@
from __future__ import annotations

import inspect
import warnings
from collections import defaultdict
from functools import lru_cache
from typing import Callable

classes_to_document = {}
classes_to_document = defaultdict(list)
classes_inherit_documentation = {}
documentation_group = None


def set_documentation_group(m):
global documentation_group # noqa: PLW0603
documentation_group = m
if m not in classes_to_document:
classes_to_document[m] = []


def extract_instance_attr_doc(cls, attr):
Expand Down Expand Up @@ -42,7 +37,33 @@ def extract_instance_attr_doc(cls, attr):
return doc_string


def document(*fns, inherit=False):
_module_prefixes = [
("gradio._simple_templates", "component"),
("gradio.block", "block"),
("gradio.chat", "chatinterface"),
("gradio.component", "component"),
("gradio.events", "helpers"),
("gradio.exceptions", "helpers"),
("gradio.external", "helpers"),
("gradio.flag", "flagging"),
("gradio.helpers", "helpers"),
("gradio.interface", "interface"),
("gradio.layout", "layout"),
("gradio.route", "routes"),
("gradio.theme", "themes"),
("gradio_client", "py-client"),
]


@lru_cache(maxsize=10)
def _get_module_documentation_group(modname) -> str:
for prefix, group in _module_prefixes:
if modname.startswith(prefix):
return group
raise ValueError(f"No known documentation group for module {modname!r}")


def document(*fns, inherit=False, documentation_group=None):
"""
Defines the @document decorator which adds classes or functions to the Gradio
documentation at www.gradio.app/docs.
Expand All @@ -52,13 +73,22 @@ def document(*fns, inherit=False):
- Put @document("fn1", "fn2") above a class to also document methods fn1 and fn2.
- Put @document("*fn3") with an asterisk above a class to document the instance attribute methods f3.
"""
_documentation_group = documentation_group

def inner_doc(cls):
functions = list(fns)
if hasattr(cls, "EVENTS"):
functions += cls.EVENTS
if inherit:
classes_inherit_documentation[cls] = None

documentation_group = _documentation_group # avoid `nonlocal` reassignment
if _documentation_group is None:
try:
modname = inspect.getmodule(cls).__name__ # type: ignore
documentation_group = _get_module_documentation_group(modname)
except Exception as exc:
warnings.warn(f"Could not get documentation group for {cls}: {exc}")
classes_to_document[documentation_group].append((cls, functions))
return cls

Expand Down
4 changes: 1 addition & 3 deletions gradio/_simple_templates/simpleimage.py
Expand Up @@ -5,14 +5,12 @@
from pathlib import Path
from typing import Any

from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio.components.base import Component
from gradio.data_classes import FileData
from gradio.events import Events

set_documentation_group("component")


@document()
class SimpleImage(Component):
Expand Down
3 changes: 1 addition & 2 deletions gradio/blocks.py
Expand Up @@ -24,7 +24,7 @@
import httpx
from anyio import CapacityLimiter
from gradio_client import utils as client_utils
from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio import (
analytics,
Expand Down Expand Up @@ -77,7 +77,6 @@
except Exception:
spaces = None

set_documentation_group("blocks")

if TYPE_CHECKING: # Only import for type checking (is False at runtime).
from fastapi.applications import FastAPI
Expand Down
4 changes: 1 addition & 3 deletions gradio/chat_interface.py
Expand Up @@ -10,7 +10,7 @@

import anyio
from gradio_client import utils as client_utils
from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio.blocks import Blocks
from gradio.components import (
Expand All @@ -30,8 +30,6 @@
from gradio.themes import ThemeClass as Theme
from gradio.utils import SyncToAsyncIterator, async_iteration

set_documentation_group("chatinterface")


@document()
class ChatInterface(Blocks):
Expand Down
4 changes: 2 additions & 2 deletions gradio/components/annotated_image.py
Expand Up @@ -6,14 +6,14 @@

import numpy as np
import PIL.Image
from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio import processing_utils, utils
from gradio.components.base import Component
from gradio.data_classes import FileData, GradioModel
from gradio.events import Events

set_documentation_group("component")
PIL.Image.init() # fixes https://github.com/gradio-app/gradio/issues/2843


class Annotation(GradioModel):
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/audio.py
Expand Up @@ -9,16 +9,14 @@
import httpx
import numpy as np
from gradio_client import utils as client_utils
from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio import processing_utils, utils
from gradio.components.base import Component, StreamingInput, StreamingOutput
from gradio.data_classes import FileData
from gradio.events import Events
from gradio.exceptions import Error

set_documentation_group("component")


@dataclasses.dataclass
class WaveformOptions:
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/bar_plot.py
Expand Up @@ -6,12 +6,10 @@

import altair as alt
import pandas as pd
from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio.components.plot import AltairPlot, AltairPlotData, Plot

set_documentation_group("component")


@document()
class BarPlot(Plot):
Expand Down
5 changes: 0 additions & 5 deletions gradio/components/base.py
Expand Up @@ -14,8 +14,6 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable

from gradio_client.documentation import set_documentation_group

from gradio import utils
from gradio.blocks import Block, BlockContext
from gradio.component_meta import ComponentMeta
Expand All @@ -32,9 +30,6 @@ class DataframeData(TypedDict):
data: list[list[str | int | bool]]


set_documentation_group("component")


class _Keywords(Enum):
NO_VALUE = "NO_VALUE" # Used as a sentinel to determine if nothing is provided as a argument for `value` in `Component.update()`
FINISHED_ITERATING = "FINISHED_ITERATING" # Used to skip processing of a component's value (needed for generators + state)
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/button.py
Expand Up @@ -4,13 +4,11 @@

from typing import Any, Callable, Literal

from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio.components.base import Component
from gradio.events import Events

set_documentation_group("component")


@document()
class Button(Component):
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/chatbot.py
Expand Up @@ -7,15 +7,13 @@
from typing import Any, Callable, List, Literal, Optional, Tuple, Union

from gradio_client import utils as client_utils
from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio import processing_utils, utils
from gradio.components.base import Component
from gradio.data_classes import FileData, GradioModel, GradioRootModel
from gradio.events import Events

set_documentation_group("component")


class FileMessage(GradioModel):
file: FileData
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/checkbox.py
Expand Up @@ -4,13 +4,11 @@

from typing import Any, Callable

from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio.components.base import FormComponent
from gradio.events import Events

set_documentation_group("component")


@document()
class Checkbox(FormComponent):
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/checkboxgroup.py
Expand Up @@ -4,13 +4,11 @@

from typing import Any, Callable, Literal

from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio.components.base import FormComponent
from gradio.events import Events

set_documentation_group("component")


@document()
class CheckboxGroup(FormComponent):
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/clear_button.py
Expand Up @@ -6,15 +6,13 @@
import json
from typing import Any, Literal

from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio.components import Button, Component
from gradio.context import Context
from gradio.data_classes import GradioModel, GradioRootModel
from gradio.utils import resolve_singleton

set_documentation_group("component")


@document("add")
class ClearButton(Button):
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/code.py
Expand Up @@ -5,13 +5,11 @@
from pathlib import Path
from typing import Any, Literal

from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio.components.base import Component
from gradio.events import Events

set_documentation_group("component")


@document("languages")
class Code(Component):
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/color_picker.py
Expand Up @@ -4,13 +4,11 @@

from typing import Any, Callable

from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio.components.base import Component
from gradio.events import Events

set_documentation_group("component")


@document()
class ColorPicker(Component):
Expand Down
5 changes: 1 addition & 4 deletions gradio/components/dataframe.py
Expand Up @@ -18,7 +18,7 @@
import numpy as np
import pandas as pd
import semantic_version
from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document
from pandas.io.formats.style import Styler

from gradio.components import Component
Expand Down Expand Up @@ -48,9 +48,6 @@ class DataframeData(GradioModel):
metadata: Optional[Dict[str, Optional[List[Any]]]] = None


set_documentation_group("component")


@document()
class Dataframe(Component):
"""
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/dataset.py
Expand Up @@ -4,7 +4,7 @@

from typing import Any, Literal

from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio import processing_utils
from gradio.components.base import (
Expand All @@ -13,8 +13,6 @@
)
from gradio.events import Events

set_documentation_group("component")


@document()
class Dataset(Component):
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/dropdown.py
Expand Up @@ -5,13 +5,11 @@
import warnings
from typing import Any, Callable, Literal

from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document

from gradio.components.base import FormComponent
from gradio.events import Events

set_documentation_group("component")


@document()
class Dropdown(FormComponent):
Expand Down

0 comments on commit 0fddd0f

Please sign in to comment.