Skip to content

Commit bc06fa3

Browse files
Add environment variables documentation to API reference
Co-Authored-By: Alek Petuskey <[email protected]>
1 parent 0db2721 commit bc06fa3

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

pcweb/pages/docs/apiref.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
rx.Var,
2121
]
2222

23+
from .env_vars import env_vars_doc
24+
2325
pages = []
2426
for module in modules:
2527
s = Source(module=module)
@@ -29,3 +31,5 @@
2931
page_data = docpage(f"/docs/api-reference/{name}/", title)(docs)
3032
page_data.title = page_data.title.split("·")[0].strip()
3133
pages.append(page_data)
34+
35+
pages.append(env_vars_doc)

pcweb/pages/docs/env_vars.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
"""Module for documenting Reflex environment variables."""
2+
from __future__ import annotations
3+
4+
import inspect
5+
from typing import Any, Dict, List, Optional, Tuple, Type
6+
7+
import reflex as rx
8+
from reflex.config import EnvironmentVariables, environment
9+
from pcweb.templates.docpage import docpage, h1_comp, h2_comp
10+
from pcweb.flexdown import markdown
11+
12+
13+
class EnvVarDocs:
14+
"""Documentation for Reflex environment variables."""
15+
16+
@classmethod
17+
def get_all_env_vars(cls) -> List[Tuple[str, Any]]:
18+
"""Get all environment variables from the environment class.
19+
20+
Returns:
21+
A list of tuples containing the environment variable name and its EnvVar instance.
22+
"""
23+
env_vars = []
24+
for name, attr in inspect.getmembers(EnvironmentVariables):
25+
if name.startswith('_') or not hasattr(attr, 'name'):
26+
continue
27+
env_vars.append((name, attr))
28+
return env_vars
29+
30+
@classmethod
31+
def get_env_var_docstring(cls, name: str) -> Optional[str]:
32+
"""Get the docstring for an environment variable.
33+
34+
Args:
35+
name: The name of the environment variable.
36+
37+
Returns:
38+
The docstring for the environment variable, or None if not found.
39+
"""
40+
source_code = inspect.getsource(EnvironmentVariables)
41+
lines = source_code.splitlines()
42+
43+
for i, line in enumerate(lines):
44+
if f"{name}:" in line and "EnvVar" in line:
45+
j = i - 1
46+
comments = []
47+
while j >= 0 and lines[j].strip().startswith('#'):
48+
comments.insert(0, lines[j].strip()[1:].strip())
49+
j -= 1
50+
if comments:
51+
return "\n".join(comments)
52+
return None
53+
54+
@classmethod
55+
def generate_env_var_table(cls, include_internal: bool = False) -> rx.Component:
56+
"""Generate a table of environment variables.
57+
58+
Args:
59+
include_internal: Whether to include internal environment variables.
60+
61+
Returns:
62+
A Reflex component containing the table.
63+
"""
64+
env_vars = cls.get_all_env_vars()
65+
66+
if not include_internal:
67+
env_vars = [(name, var) for name, var in env_vars if not getattr(var, 'internal', False)]
68+
69+
env_vars.sort(key=lambda x: x[0])
70+
71+
return rx.scroll_area(
72+
rx.table.root(
73+
rx.table.header(
74+
rx.table.row(
75+
rx.table.column_header_cell("Name", class_name="font-small text-slate-12 text-normal w-auto justify-start pl-4 font-bold"),
76+
rx.table.column_header_cell("Type", class_name="font-small text-slate-12 text-normal w-auto justify-start pl-4 font-bold"),
77+
rx.table.column_header_cell("Default", class_name="font-small text-slate-12 text-normal w-auto justify-start pl-4 font-bold"),
78+
rx.table.column_header_cell("Description", class_name="font-small text-slate-12 text-normal w-auto justify-start pl-4 font-bold"),
79+
)
80+
),
81+
rx.table.body(
82+
*[
83+
rx.table.row(
84+
rx.table.cell(
85+
rx.code(var.name, class_name="code-style"),
86+
),
87+
rx.table.cell(
88+
rx.code(str(var.type_.__name__ if hasattr(var.type_, "__name__") else str(var.type_)), class_name="code-style"),
89+
),
90+
rx.table.cell(
91+
rx.code(str(var.default), class_name="code-style"),
92+
),
93+
rx.table.cell(
94+
markdown(cls.get_env_var_docstring(name) or ""),
95+
class_name="font-small text-slate-11",
96+
),
97+
)
98+
for name, var in env_vars
99+
],
100+
),
101+
),
102+
max_height="35em",
103+
)
104+
105+
106+
def env_vars_page():
107+
"""Generate the environment variables documentation page.
108+
109+
Returns:
110+
A Reflex component containing the documentation.
111+
"""
112+
return rx.box(
113+
h1_comp(text="Environment Variables"),
114+
rx.code("reflex.config.EnvironmentVariables", class_name="code-style text-[18px]"),
115+
rx.divider(),
116+
markdown(
117+
"""
118+
Reflex provides a number of environment variables that can be used to configure the behavior of your application.
119+
These environment variables can be set in your shell environment or in a `.env` file.
120+
121+
This page documents all available environment variables in Reflex.
122+
"""
123+
),
124+
h2_comp(text="Environment Variables"),
125+
EnvVarDocs.generate_env_var_table(include_internal=False),
126+
)
127+
128+
129+
env_vars_doc = docpage("/docs/api-reference/environment-variables/", "Environment Variables")(env_vars_page)
130+
env_vars_doc.title = "Environment Variables"

0 commit comments

Comments
 (0)