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