Utils

Configuration loading, saving, and conversion utilities

Configuration File Operations


source

load_config

 load_config (schema_name:str, config_dir:Optional[pathlib.Path]=None)

Load saved configuration for a schema.

Type Default Details
schema_name str Name of the schema/configuration to load
config_dir Optional None Directory where config files are stored
Returns Dict Loaded configuration dictionary (empty dict if file doesn’t exist)

source

save_config

 save_config (schema_name:str, config:Dict[str,Any],
              config_dir:Optional[pathlib.Path]=None)

Save configuration for a schema.

Type Default Details
schema_name str Name of the schema/configuration to save
config Dict Configuration dictionary to save
config_dir Optional None Directory where config files are stored
Returns bool True if save succeeded, False otherwise

Schema Utilities


source

get_default_values_from_schema

 get_default_values_from_schema (schema:Dict[str,Any])

Extract default values from a JSON schema.

Type Details
schema Dict JSON Schema dictionary
Returns Dict Dictionary of default values extracted from schema
# Example: Extract defaults from a schema
from cjm_fasthtml_settings.core.config import get_app_config_schema

schema = get_app_config_schema(app_title="Test App", server_port=8000)
defaults = get_default_values_from_schema(schema)

print("Default values:")
for key, value in defaults.items():
    print(f"  {key}: {value}")
Default values:
  app_title: Test App
  config_dir: configs
  auto_open_browser: True
  server_port: 8000
  server_host: 0.0.0.0
  debug_mode: False
  reload_on_change: False
  max_upload_size_mb: 100
  session_timeout_minutes: 60

source

get_config_with_defaults

 get_config_with_defaults (schema_name:str, schema:Dict[str,Any],
                           config_dir:Optional[pathlib.Path]=None)

Get configuration with defaults merged with saved values.

Type Default Details
schema_name str Name of the schema (or unique_id for grouped schemas)
schema Dict JSON Schema dictionary
config_dir Optional None Directory where config files are stored
Returns Dict Merged configuration with defaults and saved values

Form Data Conversion


source

convert_form_data_to_config

 convert_form_data_to_config (form_data:dict, schema:Dict[str,Any])

Convert form data to configuration dict based on schema.

Type Details
form_data dict Raw form data from request
schema Dict JSON Schema for type conversion
Returns dict Converted configuration dictionary
# Example: Convert form data
from cjm_fasthtml_settings.core.config import get_app_config_schema

schema = get_app_config_schema()

# Simulate form data
form_data = {
    "app_title": "My App",
    "server_port": "8080",  # String from form
    "auto_open_browser": "on",  # Checkbox value
    "max_upload_size_mb": "250.5",  # String number
}

config = convert_form_data_to_config(form_data, schema)

print("Converted config:")
for key, value in config.items():
    print(f"  {key}: {value} ({type(value).__name__})")
Converted config:
  app_title: My App (str)
  server_port: 8080 (int)
  auto_open_browser: True (bool)
  max_upload_size_mb: 250.5 (float)
  debug_mode: False (bool)
  reload_on_change: False (bool)