Skip to content

Commit f4ad8de

Browse files
Add environment variables documentation to API reference (#1326)
* Add environment variables documentation to API reference Co-Authored-By: Alek Petuskey <[email protected]> * Fix table width to prevent horizontal scrolling Co-Authored-By: Alek Petuskey <[email protected]> * Remove right sidebar from environment variables page Co-Authored-By: Alek Petuskey <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alek Petuskey <[email protected]>
1 parent 0db2721 commit f4ad8de

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-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: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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.box(
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-[20%] justify-start pl-4 font-bold"),
76+
rx.table.column_header_cell("Type", class_name="font-small text-slate-12 text-normal w-[15%] justify-start pl-4 font-bold"),
77+
rx.table.column_header_cell("Default", class_name="font-small text-slate-12 text-normal w-[15%] justify-start pl-4 font-bold"),
78+
rx.table.column_header_cell("Description", class_name="font-small text-slate-12 text-normal w-[50%] 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+
class_name="w-[20%]",
87+
),
88+
rx.table.cell(
89+
rx.code(str(var.type_.__name__ if hasattr(var.type_, "__name__") else str(var.type_)), class_name="code-style"),
90+
class_name="w-[15%]",
91+
),
92+
rx.table.cell(
93+
rx.code(str(var.default), class_name="code-style"),
94+
class_name="w-[15%]",
95+
),
96+
rx.table.cell(
97+
markdown(cls.get_env_var_docstring(name) or ""),
98+
class_name="font-small text-slate-11 w-[50%]",
99+
),
100+
)
101+
for name, var in env_vars
102+
],
103+
),
104+
width="100%",
105+
overflow_x="visible",
106+
class_name="w-full",
107+
),
108+
)
109+
110+
111+
def env_vars_page():
112+
"""Generate the environment variables documentation page.
113+
114+
Returns:
115+
A Reflex component containing the documentation.
116+
"""
117+
return rx.box(
118+
h1_comp(text="Environment Variables"),
119+
rx.code("reflex.config.EnvironmentVariables", class_name="code-style text-[18px]"),
120+
rx.divider(),
121+
markdown(
122+
"""
123+
Reflex provides a number of environment variables that can be used to configure the behavior of your application.
124+
These environment variables can be set in your shell environment or in a `.env` file.
125+
126+
This page documents all available environment variables in Reflex.
127+
"""
128+
),
129+
h2_comp(text="Environment Variables"),
130+
EnvVarDocs.generate_env_var_table(include_internal=False),
131+
)
132+
133+
134+
env_vars_doc = docpage("/docs/api-reference/environment-variables/", "Environment Variables", right_sidebar=False)(env_vars_page)
135+
env_vars_doc.title = "Environment Variables"

0 commit comments

Comments
 (0)