Skip to content

Commit

Permalink
Determine documentation group automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jan 18, 2024
1 parent 3f139c7 commit 98915e5
Show file tree
Hide file tree
Showing 57 changed files with 95 additions and 186 deletions.
5 changes: 1 addition & 4 deletions client/python/gradio_client/client.py
Original file line number Diff line number Diff line change
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: 39 additions & 11 deletions client/python/gradio_client/documentation.py
Original file line number Diff line number Diff line change
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
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,32 @@ def extract_instance_attr_doc(cls, attr):
return doc_string


def document(*fns, inherit=False):
_module_prefixes = [
("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,14 +72,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
global documentation_group
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
3 changes: 1 addition & 2 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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: 1 addition & 3 deletions gradio/components/annotated_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
from typing import Any, List

import numpy as np
from gradio_client.documentation import document, set_documentation_group
from gradio_client.documentation import document
from PIL import Image as _Image # using _ to minimize namespace pollution

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")

_Image.init() # fixes https://github.com/gradio-app/gradio/issues/2843


Expand Down
4 changes: 1 addition & 3 deletions gradio/components/audio.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 0 additions & 2 deletions gradio/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable

from gradio_client.documentation import set_documentation_group
from PIL import Image as _Image # using _ to minimize namespace pollution

from gradio import utils
Expand All @@ -33,7 +32,6 @@ class DataframeData(TypedDict):
data: list[list[str | int | bool]]


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


Expand Down
4 changes: 1 addition & 3 deletions gradio/components/button.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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 All @@ -22,9 +22,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
4 changes: 1 addition & 3 deletions gradio/components/duplicate_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

from typing import Literal

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

from gradio.components import Button
from gradio.context import Context
from gradio.utils import get_space

set_documentation_group("component")


@document()
class DuplicateButton(Button):
Expand Down
4 changes: 1 addition & 3 deletions gradio/components/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
from pathlib import Path
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.data_classes import FileData, ListFiles
from gradio.events import Events
from gradio.utils import NamedString

set_documentation_group("component")


@document()
class File(Component):
Expand Down
Loading

0 comments on commit 98915e5

Please sign in to comment.