Reactive user interfaces with pure Python
—
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.
Configuration for debugging and development features:
# Debug mode toggle
REACTPY_DEBUG_MODE: bool = False
# Default timeout for testing operations
REACTPY_TESTING_DEFAULT_TIMEOUT: float = 3.0Environment Variables:
REACTPY_DEBUG_MODE: Enable debug mode ("true"/"false")REACTPY_TESTING_DEFAULT_TIMEOUT: Default test timeout in secondsUsage Examples:
import os
from reactpy import config
# Set debug mode programmatically
config.REACTPY_DEBUG_MODE = True
# Or via environment variable
os.environ["REACTPY_DEBUG_MODE"] = "true"
# Configure test timeouts
config.REACTPY_TESTING_DEFAULT_TIMEOUT = 5.0Settings for backend server behavior:
# Backend server host
REACTPY_BACKEND_HOST: str = "127.0.0.1"
# Backend server port
REACTPY_BACKEND_PORT: int = 8000
# CORS allow all origins
REACTPY_BACKEND_CORS_ALLOW_ALL: bool = FalseEnvironment Variables:
REACTPY_BACKEND_HOST: Host address for backend serverREACTPY_BACKEND_PORT: Port number for backend serverREACTPY_BACKEND_CORS_ALLOW_ALL: Allow CORS from all origins ("true"/"false")Usage Examples:
from reactpy import config, run
# Configure backend settings
config.REACTPY_BACKEND_HOST = "0.0.0.0"
config.REACTPY_BACKEND_PORT = 3000
config.REACTPY_BACKEND_CORS_ALLOW_ALL = True
@component
def App():
return html.h1("My App")
# Run with configured settings
run(App)
# Or configure via environment
import os
os.environ["REACTPY_BACKEND_HOST"] = "0.0.0.0"
os.environ["REACTPY_BACKEND_PORT"] = "3000"
os.environ["REACTPY_BACKEND_CORS_ALLOW_ALL"] = "true"Settings for JavaScript module integration:
# Directory for web modules
REACTPY_WEB_MODULES_DIR: str = "./web_modules"
# URL prefix for serving web modules
REACTPY_WEB_MODULES_URL_PREFIX: str = "/_reactpy/modules"Environment Variables:
REACTPY_WEB_MODULES_DIR: Directory path for storing web modulesREACTPY_WEB_MODULES_URL_PREFIX: URL path prefix for serving modulesUsage Examples:
from reactpy import config
from reactpy.web import module_from_file
# Configure web modules directory
config.REACTPY_WEB_MODULES_DIR = "./static/js"
config.REACTPY_WEB_MODULES_URL_PREFIX = "/js"
# Modules will be served from /js/ path
my_module = module_from_file("utils", "./static/js/utilities.js")Settings for asynchronous rendering behavior:
# Enable async rendering
REACTPY_ASYNC_RENDERING: bool = TrueEnvironment Variables:
REACTPY_ASYNC_RENDERING: Enable asynchronous component rendering ("true"/"false")Usage Examples:
from reactpy import config
# Disable async rendering for debugging
config.REACTPY_ASYNC_RENDERING = False
@component
async def AsyncComponent():
# This will run synchronously if async rendering is disabled
await asyncio.sleep(0.1)
return html.div("Async content")Development Configuration:
# development.py
from reactpy import config
config.REACTPY_DEBUG_MODE = True
config.REACTPY_BACKEND_HOST = "127.0.0.1"
config.REACTPY_BACKEND_PORT = 8000
config.REACTPY_BACKEND_CORS_ALLOW_ALL = True
config.REACTPY_TESTING_DEFAULT_TIMEOUT = 10.0Production Configuration:
# production.py
from reactpy import config
config.REACTPY_DEBUG_MODE = False
config.REACTPY_BACKEND_HOST = "0.0.0.0"
config.REACTPY_BACKEND_PORT = 80
config.REACTPY_BACKEND_CORS_ALLOW_ALL = False
config.REACTPY_ASYNC_RENDERING = TrueDocker Environment Configuration:
# Dockerfile
FROM python:3.11
# Set ReactPy configuration via environment variables
ENV REACTPY_DEBUG_MODE=false
ENV REACTPY_BACKEND_HOST=0.0.0.0
ENV REACTPY_BACKEND_PORT=8000
ENV REACTPY_BACKEND_CORS_ALLOW_ALL=false
ENV REACTPY_WEB_MODULES_DIR=/app/static/js
COPY . /app
WORKDIR /app
RUN pip install reactpy
EXPOSE 8000
CMD ["python", "app.py"]Access and modify configuration at runtime:
from reactpy import config
def configure_for_environment(env: str):
"""Configure ReactPy for different environments"""
if env == "development":
config.REACTPY_DEBUG_MODE = True
config.REACTPY_BACKEND_CORS_ALLOW_ALL = True
config.REACTPY_TESTING_DEFAULT_TIMEOUT = 10.0
elif env == "production":
config.REACTPY_DEBUG_MODE = False
config.REACTPY_BACKEND_CORS_ALLOW_ALL = False
config.REACTPY_ASYNC_RENDERING = True
elif env == "testing":
config.REACTPY_DEBUG_MODE = True
config.REACTPY_TESTING_DEFAULT_TIMEOUT = 30.0
config.REACTPY_ASYNC_RENDERING = False
# Usage
import os
environment = os.getenv("ENVIRONMENT", "development")
configure_for_environment(environment)Validate configuration settings:
from reactpy import config
def validate_config():
"""Validate ReactPy configuration"""
errors = []
# Validate host
if not config.REACTPY_BACKEND_HOST:
errors.append("REACTPY_BACKEND_HOST cannot be empty")
# Validate port
if not (1 <= config.REACTPY_BACKEND_PORT <= 65535):
errors.append("REACTPY_BACKEND_PORT must be between 1 and 65535")
# Validate timeout
if config.REACTPY_TESTING_DEFAULT_TIMEOUT <= 0:
errors.append("REACTPY_TESTING_DEFAULT_TIMEOUT must be positive")
# Validate modules directory
import os
if not os.path.exists(config.REACTPY_WEB_MODULES_DIR):
errors.append(f"REACTPY_WEB_MODULES_DIR does not exist: {config.REACTPY_WEB_MODULES_DIR}")
if errors:
raise ValueError("Configuration errors: " + "; ".join(errors))
return True
# Validate before starting application
try:
validate_config()
print("Configuration is valid")
except ValueError as e:
print(f"Configuration error: {e}")Environment-Specific Configuration:
# config.py
import os
from reactpy import config
# Load configuration from environment with defaults
config.REACTPY_DEBUG_MODE = os.getenv("REACTPY_DEBUG_MODE", "false").lower() == "true"
config.REACTPY_BACKEND_HOST = os.getenv("REACTPY_BACKEND_HOST", "127.0.0.1")
config.REACTPY_BACKEND_PORT = int(os.getenv("REACTPY_BACKEND_PORT", "8000"))
# Validate critical settings
if config.REACTPY_DEBUG_MODE and config.REACTPY_BACKEND_HOST == "0.0.0.0":
print("WARNING: Debug mode enabled with public host binding")
# Configure logging based on debug mode
import logging
logging.basicConfig(
level=logging.DEBUG if config.REACTPY_DEBUG_MODE else logging.INFO
)Configuration with .env Files:
# Using python-dotenv
from dotenv import load_dotenv
import os
from reactpy import config
# Load environment variables from .env file
load_dotenv()
# Apply configuration
config.REACTPY_DEBUG_MODE = os.getenv("REACTPY_DEBUG_MODE", "false").lower() == "true"
config.REACTPY_BACKEND_HOST = os.getenv("REACTPY_BACKEND_HOST", "127.0.0.1")
config.REACTPY_BACKEND_PORT = int(os.getenv("REACTPY_BACKEND_PORT", "8000")).env file example:
REACTPY_DEBUG_MODE=true
REACTPY_BACKEND_HOST=0.0.0.0
REACTPY_BACKEND_PORT=3000
REACTPY_BACKEND_CORS_ALLOW_ALL=true
REACTPY_WEB_MODULES_DIR=./static/modules
REACTPY_TESTING_DEFAULT_TIMEOUT=15.0Install with Tessl CLI
npx tessl i tessl/pypi-reactpy