Skip to content
Open
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
30 changes: 19 additions & 11 deletions gradio/cli/commands/components/_docs_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import functools
import inspect
import re
import types
Expand All @@ -20,17 +21,7 @@ def format(code: str, type: str):
if type == "value":
code = f"value = {code}"

ruff_args = ["ruff", "format", "-", "--line-length=60"]

process = Popen(
ruff_args,
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
universal_newlines=True,
)

formatted_code, err = process.communicate(input=str(code))
formatted_code = _format_code_with_ruff(str(code))

if type == "value":
formatted_code = re.sub(
Expand Down Expand Up @@ -921,3 +912,20 @@ def make_markdown(
)

return source


@functools.lru_cache(maxsize=128)
def _format_code_with_ruff(code: str) -> str:
"""Cache Ruff formatter calls for identical input."""
ruff_args = ["ruff", "format", "-", "--line-length=60"]

process = Popen(
ruff_args,
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
universal_newlines=True,
)

formatted_code, err = process.communicate(input=code)
return formatted_code