Skip to content
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
27 changes: 16 additions & 11 deletions pcweb/docgen_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ class ReflexDocTransformer(DocumentTransformer[rx.Component]):
the reflex package look identical to the locally-authored ones.
"""

def __init__(self, filename: str = "") -> None:
def __init__(self, virtual_filepath: str = "", filename: str = "") -> None:
self.virtual_filepath = virtual_filepath
self.filename = filename
self.env: dict = {}
Comment on lines +172 to 175
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Unused filename parameter and attribute

After this PR, self.filename is stored in __init__ but never read anywhere in ReflexDocTransformer. All former usages of self.filename (in _exec_code calls and error .add_note() strings) have been replaced with self.virtual_filepath. The filename argument (and its assignment) is now dead code.

If the actual filepath is intentionally kept for potential future use or external callers, a comment explaining that would help; otherwise it can be removed to keep the interface clean.

Suggested change
def __init__(self, virtual_filepath: str = "", filename: str = "") -> None:
self.virtual_filepath = virtual_filepath
self.filename = filename
self.env: dict = {}
def __init__(self, virtual_filepath: str = "") -> None:
self.virtual_filepath = virtual_filepath
self.env: dict = {}

And in render_docgen_document, the call becomes:

transformer = ReflexDocTransformer(virtual_filepath=str(virtual_filepath))


Expand Down Expand Up @@ -234,7 +235,7 @@ def code_block(self, block: CodeBlock) -> rx.Component:

# ``python exec`` only — execute code, produce nothing visible.
if language == "python" and "exec" in flags:
_exec_code(block.content, self.env, self.filename)
_exec_code(block.content, self.env, self.virtual_filepath)
return rx.fragment()

# ``python eval`` (standalone) — eval and return the component directly.
Expand Down Expand Up @@ -378,10 +379,10 @@ def _render_demo(self, content: str, flags: set[str]) -> rx.Component:

try:
if "exec" in flags:
_exec_code(content, self.env, self.filename)
_exec_code(content, self.env, self.virtual_filepath)
comp = self.env[list(self.env.keys())[-1]]()
elif "graphing" in flags:
_exec_code(content, self.env, self.filename)
_exec_code(content, self.env, self.virtual_filepath)
comp = self.env[list(self.env.keys())[-1]]()
parts = content.rpartition("def")
data, code = parts[0], parts[1] + parts[2]
Expand All @@ -393,7 +394,7 @@ def _render_demo(self, content: str, flags: set[str]) -> rx.Component:
comp = eval(content, self.env, self.env)
except Exception as e:
e.add_note(
f"While rendering demo block in {self.filename}:\n{content[:200]}"
f"While rendering demo block in {self.virtual_filepath}:\n{content[:200]}"
)
raise

Expand All @@ -416,10 +417,10 @@ def _render_demo_only(self, content: str, flags: set[str]) -> rx.Component:

try:
if "exec" in flags:
_exec_code(content, self.env, self.filename)
_exec_code(content, self.env, self.virtual_filepath)
comp = self.env[list(self.env.keys())[-1]]()
elif "graphing" in flags:
_exec_code(content, self.env, self.filename)
_exec_code(content, self.env, self.virtual_filepath)
comp = self.env[list(self.env.keys())[-1]]()
parts = content.rpartition("def")
data, code = parts[0], parts[1] + parts[2]
Expand All @@ -430,7 +431,7 @@ def _render_demo_only(self, content: str, flags: set[str]) -> rx.Component:
comp = eval(content, self.env, self.env)
except Exception as e:
e.add_note(
f"While rendering demo-only block in {self.filename}:\n{content[:200]}"
f"While rendering demo-only block in {self.virtual_filepath}:\n{content[:200]}"
)
raise

Expand Down Expand Up @@ -681,10 +682,14 @@ def _parse_doc(filepath: str | Path) -> Document:
return parse_document(source)


def render_docgen_document(filepath: str | Path) -> rx.Component:
def render_docgen_document(
virtual_filepath: str | Path, actual_filepath: str | Path
) -> rx.Component:
"""Parse and render a doc file from the reflex package using reflex_docgen."""
doc = _parse_doc(filepath)
transformer = ReflexDocTransformer(filename=str(filepath))
doc = _parse_doc(actual_filepath)
transformer = ReflexDocTransformer(
virtual_filepath=str(virtual_filepath), filename=str(actual_filepath)
)
return transformer.transform(doc)


Expand Down
6 changes: 4 additions & 2 deletions pcweb/pages/docs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,11 @@ def get_component_docgen(virtual_doc: str, actual_path: str, title: str):
if virtual_doc.startswith("docs/library"):
return handle_library_doc(virtual_doc, actual_path, title, resolved)

def comp(_actual=actual_path):
def comp(_actual=actual_path, _virtual=virtual_doc):
toc = get_docgen_toc(_actual)
rendered = render_docgen_document(_actual)
rendered = render_docgen_document(
virtual_filepath=_virtual, actual_filepath=_actual
)
return (toc, rendered)

return make_docpage(resolved.route, resolved.display_title, virtual_doc, comp)
Expand Down
Loading