liveBook · Manning

Configuration

As explained in Build Python Web Apps with Streamlit

A configuration file is a file that stores declarative settings and structured application data separately from program logic.

Adding a unit to a converter should not mean hunting through UI and calculation code. Keep the supported quantities, units, and conversion factors in one configuration file. Other parts of the app should read that file rather than name specific units themselves. section 3.5.2

unit_config = {
    "Mass": {
        "Kilograms": 1, # Standard unit
        "Grams": 0.001,
        "Pounds": 0.453592,
        # ...
    },
    "Length": {
        "Meters": 1,  # Standard unit
        "Centimeters": 0.01,
        # ...
    },
}

A nested dictionary shows the basic arrangement: each quantity has units, and each unit has a conversion factor relative to its standard unit. section 3.6.2

When units also need abbreviations and quantities need an explicitly named standard unit, use Unit and Quantity dataclasses. In unit_config.py, each quantity then holds its standard-unit name and a dictionary of units with their abbreviations and factors. section 3.6.2

The same configuration serves both sides of the app: its quantity names populate the selector, its units populate the dropdowns, and its factors let the backend calculate a result. To support another unit, add it in the configuration rather than hard-coding it into the UI. section 3.5.3 section 3.5.2

How Build Python Web Apps with Streamlit covers it

How deep it goes
Works through a Python configuration module, dataclass structure, and backend lookups rather than an external configuration format.
Examples
Shows mass and length configurations with units including kilograms, grams, meters, and centimeters.
What it assumes
Introduces dataclasses rather than assuming them, while using Python dictionaries and imports in the implementation.
What you can do after
Build it from scratch as unit_config.py and add units without changing quantity-specific UI or conversion code.
How it connects
Connects configuration to dictionaries, dataclasses, frontend/backend separation, UI selectors, and unit conversion.

Also explained in

Compare how 23 books explain configuration →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.