0
# Configuration
1
2
Configuration options and environment variables for customizing ReactPy behavior. ReactPy provides various configuration settings that can be adjusted to suit different deployment scenarios and development needs.
3
4
## Capabilities
5
6
### Debug and Development Settings
7
8
Configuration for debugging and development features:
9
10
```python { .api }
11
# Debug mode toggle
12
REACTPY_DEBUG_MODE: bool = False
13
14
# Default timeout for testing operations
15
REACTPY_TESTING_DEFAULT_TIMEOUT: float = 3.0
16
```
17
18
**Environment Variables:**
19
- `REACTPY_DEBUG_MODE`: Enable debug mode ("true"/"false")
20
- `REACTPY_TESTING_DEFAULT_TIMEOUT`: Default test timeout in seconds
21
22
**Usage Examples:**
23
24
```python
25
import os
26
from reactpy import config
27
28
# Set debug mode programmatically
29
config.REACTPY_DEBUG_MODE = True
30
31
# Or via environment variable
32
os.environ["REACTPY_DEBUG_MODE"] = "true"
33
34
# Configure test timeouts
35
config.REACTPY_TESTING_DEFAULT_TIMEOUT = 5.0
36
```
37
38
### Backend Configuration
39
40
Settings for backend server behavior:
41
42
```python { .api }
43
# Backend server host
44
REACTPY_BACKEND_HOST: str = "127.0.0.1"
45
46
# Backend server port
47
REACTPY_BACKEND_PORT: int = 8000
48
49
# CORS allow all origins
50
REACTPY_BACKEND_CORS_ALLOW_ALL: bool = False
51
```
52
53
**Environment Variables:**
54
- `REACTPY_BACKEND_HOST`: Host address for backend server
55
- `REACTPY_BACKEND_PORT`: Port number for backend server
56
- `REACTPY_BACKEND_CORS_ALLOW_ALL`: Allow CORS from all origins ("true"/"false")
57
58
**Usage Examples:**
59
60
```python
61
from reactpy import config, run
62
63
# Configure backend settings
64
config.REACTPY_BACKEND_HOST = "0.0.0.0"
65
config.REACTPY_BACKEND_PORT = 3000
66
config.REACTPY_BACKEND_CORS_ALLOW_ALL = True
67
68
@component
69
def App():
70
return html.h1("My App")
71
72
# Run with configured settings
73
run(App)
74
75
# Or configure via environment
76
import os
77
os.environ["REACTPY_BACKEND_HOST"] = "0.0.0.0"
78
os.environ["REACTPY_BACKEND_PORT"] = "3000"
79
os.environ["REACTPY_BACKEND_CORS_ALLOW_ALL"] = "true"
80
```
81
82
### Web Modules Configuration
83
84
Settings for JavaScript module integration:
85
86
```python { .api }
87
# Directory for web modules
88
REACTPY_WEB_MODULES_DIR: str = "./web_modules"
89
90
# URL prefix for serving web modules
91
REACTPY_WEB_MODULES_URL_PREFIX: str = "/_reactpy/modules"
92
```
93
94
**Environment Variables:**
95
- `REACTPY_WEB_MODULES_DIR`: Directory path for storing web modules
96
- `REACTPY_WEB_MODULES_URL_PREFIX`: URL path prefix for serving modules
97
98
**Usage Examples:**
99
100
```python
101
from reactpy import config
102
from reactpy.web import module_from_file
103
104
# Configure web modules directory
105
config.REACTPY_WEB_MODULES_DIR = "./static/js"
106
config.REACTPY_WEB_MODULES_URL_PREFIX = "/js"
107
108
# Modules will be served from /js/ path
109
my_module = module_from_file("utils", "./static/js/utilities.js")
110
```
111
112
### Async Rendering Configuration
113
114
Settings for asynchronous rendering behavior:
115
116
```python { .api }
117
# Enable async rendering
118
REACTPY_ASYNC_RENDERING: bool = True
119
```
120
121
**Environment Variables:**
122
- `REACTPY_ASYNC_RENDERING`: Enable asynchronous component rendering ("true"/"false")
123
124
**Usage Examples:**
125
126
```python
127
from reactpy import config
128
129
# Disable async rendering for debugging
130
config.REACTPY_ASYNC_RENDERING = False
131
132
@component
133
async def AsyncComponent():
134
# This will run synchronously if async rendering is disabled
135
await asyncio.sleep(0.1)
136
return html.div("Async content")
137
```
138
139
### Configuration in Different Environments
140
141
**Development Configuration:**
142
143
```python
144
# development.py
145
from reactpy import config
146
147
config.REACTPY_DEBUG_MODE = True
148
config.REACTPY_BACKEND_HOST = "127.0.0.1"
149
config.REACTPY_BACKEND_PORT = 8000
150
config.REACTPY_BACKEND_CORS_ALLOW_ALL = True
151
config.REACTPY_TESTING_DEFAULT_TIMEOUT = 10.0
152
```
153
154
**Production Configuration:**
155
156
```python
157
# production.py
158
from reactpy import config
159
160
config.REACTPY_DEBUG_MODE = False
161
config.REACTPY_BACKEND_HOST = "0.0.0.0"
162
config.REACTPY_BACKEND_PORT = 80
163
config.REACTPY_BACKEND_CORS_ALLOW_ALL = False
164
config.REACTPY_ASYNC_RENDERING = True
165
```
166
167
**Docker Environment Configuration:**
168
169
```dockerfile
170
# Dockerfile
171
FROM python:3.11
172
173
# Set ReactPy configuration via environment variables
174
ENV REACTPY_DEBUG_MODE=false
175
ENV REACTPY_BACKEND_HOST=0.0.0.0
176
ENV REACTPY_BACKEND_PORT=8000
177
ENV REACTPY_BACKEND_CORS_ALLOW_ALL=false
178
ENV REACTPY_WEB_MODULES_DIR=/app/static/js
179
180
COPY . /app
181
WORKDIR /app
182
183
RUN pip install reactpy
184
185
EXPOSE 8000
186
CMD ["python", "app.py"]
187
```
188
189
### Runtime Configuration
190
191
Access and modify configuration at runtime:
192
193
```python
194
from reactpy import config
195
196
def configure_for_environment(env: str):
197
"""Configure ReactPy for different environments"""
198
199
if env == "development":
200
config.REACTPY_DEBUG_MODE = True
201
config.REACTPY_BACKEND_CORS_ALLOW_ALL = True
202
config.REACTPY_TESTING_DEFAULT_TIMEOUT = 10.0
203
204
elif env == "production":
205
config.REACTPY_DEBUG_MODE = False
206
config.REACTPY_BACKEND_CORS_ALLOW_ALL = False
207
config.REACTPY_ASYNC_RENDERING = True
208
209
elif env == "testing":
210
config.REACTPY_DEBUG_MODE = True
211
config.REACTPY_TESTING_DEFAULT_TIMEOUT = 30.0
212
config.REACTPY_ASYNC_RENDERING = False
213
214
# Usage
215
import os
216
environment = os.getenv("ENVIRONMENT", "development")
217
configure_for_environment(environment)
218
```
219
220
### Configuration Validation
221
222
Validate configuration settings:
223
224
```python
225
from reactpy import config
226
227
def validate_config():
228
"""Validate ReactPy configuration"""
229
230
errors = []
231
232
# Validate host
233
if not config.REACTPY_BACKEND_HOST:
234
errors.append("REACTPY_BACKEND_HOST cannot be empty")
235
236
# Validate port
237
if not (1 <= config.REACTPY_BACKEND_PORT <= 65535):
238
errors.append("REACTPY_BACKEND_PORT must be between 1 and 65535")
239
240
# Validate timeout
241
if config.REACTPY_TESTING_DEFAULT_TIMEOUT <= 0:
242
errors.append("REACTPY_TESTING_DEFAULT_TIMEOUT must be positive")
243
244
# Validate modules directory
245
import os
246
if not os.path.exists(config.REACTPY_WEB_MODULES_DIR):
247
errors.append(f"REACTPY_WEB_MODULES_DIR does not exist: {config.REACTPY_WEB_MODULES_DIR}")
248
249
if errors:
250
raise ValueError("Configuration errors: " + "; ".join(errors))
251
252
return True
253
254
# Validate before starting application
255
try:
256
validate_config()
257
print("Configuration is valid")
258
except ValueError as e:
259
print(f"Configuration error: {e}")
260
```
261
262
### Configuration Best Practices
263
264
**Environment-Specific Configuration:**
265
266
```python
267
# config.py
268
import os
269
from reactpy import config
270
271
# Load configuration from environment with defaults
272
config.REACTPY_DEBUG_MODE = os.getenv("REACTPY_DEBUG_MODE", "false").lower() == "true"
273
config.REACTPY_BACKEND_HOST = os.getenv("REACTPY_BACKEND_HOST", "127.0.0.1")
274
config.REACTPY_BACKEND_PORT = int(os.getenv("REACTPY_BACKEND_PORT", "8000"))
275
276
# Validate critical settings
277
if config.REACTPY_DEBUG_MODE and config.REACTPY_BACKEND_HOST == "0.0.0.0":
278
print("WARNING: Debug mode enabled with public host binding")
279
280
# Configure logging based on debug mode
281
import logging
282
logging.basicConfig(
283
level=logging.DEBUG if config.REACTPY_DEBUG_MODE else logging.INFO
284
)
285
```
286
287
**Configuration with .env Files:**
288
289
```python
290
# Using python-dotenv
291
from dotenv import load_dotenv
292
import os
293
from reactpy import config
294
295
# Load environment variables from .env file
296
load_dotenv()
297
298
# Apply configuration
299
config.REACTPY_DEBUG_MODE = os.getenv("REACTPY_DEBUG_MODE", "false").lower() == "true"
300
config.REACTPY_BACKEND_HOST = os.getenv("REACTPY_BACKEND_HOST", "127.0.0.1")
301
config.REACTPY_BACKEND_PORT = int(os.getenv("REACTPY_BACKEND_PORT", "8000"))
302
```
303
304
**.env file example:**
305
306
```env
307
REACTPY_DEBUG_MODE=true
308
REACTPY_BACKEND_HOST=0.0.0.0
309
REACTPY_BACKEND_PORT=3000
310
REACTPY_BACKEND_CORS_ALLOW_ALL=true
311
REACTPY_WEB_MODULES_DIR=./static/modules
312
REACTPY_TESTING_DEFAULT_TIMEOUT=15.0
313
```