Added classes to support formatting

This commit is contained in:
2026-01-26 21:32:26 +01:00
parent 3083f3b1fd
commit 9abb9dddfe
14 changed files with 2059 additions and 21 deletions

View File

@@ -1,5 +1,32 @@
# DataGrid Formatting
## Implementation Status
| Component | Status | Location |
|-----------|--------|----------|
| **Core Module** | | `src/myfasthtml/core/formatting/` |
| Dataclasses (Condition, Style, Formatter, FormatRule) | :white_check_mark: Implemented | `dataclasses.py` |
| Style Presets (DaisyUI 5) | :white_check_mark: Implemented | `presets.py` |
| Formatter Presets (EUR, USD, etc.) | :white_check_mark: Implemented | `presets.py` |
| ConditionEvaluator (12 operators) | :white_check_mark: Implemented | `condition_evaluator.py` |
| StyleResolver | :white_check_mark: Implemented | `style_resolver.py` |
| FormatterResolver (Number, Date, Boolean, Text, Enum) | :white_check_mark: Implemented | `formatter_resolver.py` |
| FormattingEngine (facade + conflict resolution) | :white_check_mark: Implemented | `engine.py` |
| **Condition Features** | | |
| `col` parameter (row-level conditions) | :white_check_mark: Implemented | |
| `row` parameter (column-level conditions) | :x: Not implemented | |
| Column reference in value `{"col": "..."}` | :white_check_mark: Implemented | |
| **DataGrid Integration** | | |
| Integration in `mk_body_cell_content()` | :x: Not implemented | |
| DataGridsManager (global presets) | :white_check_mark: Implemented | `DataGridsManager.py` |
| **Tests** | | `tests/core/formatting/` |
| test_condition_evaluator.py | :white_check_mark: ~45 test cases | |
| test_style_resolver.py | :white_check_mark: ~12 test cases | |
| test_formatter_resolver.py | :white_check_mark: ~40 test cases | |
| test_engine.py | :white_check_mark: ~18 test cases | |
---
## Overview
This document describes the formatting capabilities for the DataGrid component.
@@ -79,17 +106,19 @@ For `value = -5`: Rule 3 wins (same specificity as rule 2, but defined later).
### Fields
| Field | Type | Default | Required | Description |
|------------------|------------------------|---------|----------|---------------------------------------------|
| `operator` | string | - | Yes | Comparison operator |
| `value` | scalar / list / object | - | Depends | Value to compare against |
| `not` | bool | `false` | No | Inverts the condition result |
| `case_sensitive` | bool | `false` | No | Case-sensitive string comparison |
| `col` | string | - | No | Reference column (for row-level conditions) |
| `row` | int | - | No | Reference row (for column-level conditions) |
| Field | Type | Default | Required | Description | Status |
|------------------|------------------------|---------|----------|---------------------------------------------|--------|
| `operator` | string | - | Yes | Comparison operator | :white_check_mark: |
| `value` | scalar / list / object | - | Depends | Value to compare against | :white_check_mark: |
| `not` | bool | `false` | No | Inverts the condition result | :white_check_mark: (as `negate`) |
| `case_sensitive` | bool | `false` | No | Case-sensitive string comparison | :white_check_mark: |
| `col` | string | - | No | Reference column (for row-level conditions) | :white_check_mark: |
| `row` | int | - | No | Reference row (for column-level conditions) | :x: Not implemented |
### Operators
All operators are :white_check_mark: **implemented**.
| Operator | Description | Value Required |
|--------------|--------------------------|------------------|
| `==` | Equal | Yes |
@@ -217,6 +246,8 @@ String comparisons are **case-insensitive by default**.
## Style Structure
:white_check_mark: **Fully implemented** in `style_resolver.py`
### Fields
| Field | Type | Default | Description |
@@ -292,6 +323,8 @@ If formatting fails (e.g., non-numeric value for `number` formatter), display `"
## Formatter Types
All formatter types are :white_check_mark: **implemented** in `formatter_resolver.py`.
### `number`
For numbers, currencies, and percentages.
@@ -347,7 +380,7 @@ For mapping values to display labels. Also used for Select dropdowns.
#### Source Types
**Static mapping:**
**Static mapping:** :white_check_mark: Implemented
```json
{
@@ -364,7 +397,7 @@ For mapping values to display labels. Also used for Select dropdowns.
}
```
**From another DataGrid:**
**From another DataGrid:** :white_check_mark: Implemented (requires `lookup_resolver` injection)
```json
{
@@ -429,13 +462,15 @@ formatter_presets = {
## Storage Architecture
:warning: **Structures exist but integration with formatting engine not implemented**
### Format Storage Location
| Level | Storage | Key |
|------------|------------------------------|---------|
| **Column** | `DataGridColumnState.format` | - |
| **Row** | `DataGridRowState.format` | - |
| **Cell** | `DatagridState.cell_formats` | Cell ID |
| Level | Storage | Key | Status |
|------------|------------------------------|---------|--------|
| **Column** | `DataGridColumnState.format` | - | Structure exists |
| **Row** | `DataGridRowState.format` | - | Structure exists |
| **Cell** | `DatagridState.cell_formats` | Cell ID | Structure exists |
### Cell ID Format
@@ -447,18 +482,42 @@ tcell_{datagrid_id}-{row_index}-{col_index}
## DataGridsManager
Global settings stored in `DataGridsManager`:
:white_check_mark: **Implemented** in `src/myfasthtml/controls/DataGridsManager.py`
| Property | Type | Description |
|---------------------|--------|-------------------------------------------|
| `style_presets` | dict | Style presets (primary, success, etc.) |
| `formatter_presets` | dict | Formatter presets (EUR, percentage, etc.) |
| `default_locale` | string | Default locale for number/date formatting |
Global presets stored as instance attributes:
| Property | Type | Description | Status |
|---------------------|--------|-------------------------------------------|--------|
| `style_presets` | dict | Style presets (primary, success, etc.) | :white_check_mark: |
| `formatter_presets` | dict | Formatter presets (EUR, percentage, etc.) | :white_check_mark: |
| `default_locale` | string | Default locale for number/date formatting | :x: Not implemented |
**Methods:**
| Method | Description |
|--------|-------------|
| `get_style_presets()` | Get the global style presets |
| `get_formatter_presets()` | Get the global formatter presets |
| `add_style_preset(name, preset)` | Add or update a style preset |
| `add_formatter_preset(name, preset)` | Add or update a formatter preset |
| `remove_style_preset(name)` | Remove a style preset |
| `remove_formatter_preset(name)` | Remove a formatter preset |
**Usage:**
```python
# Add custom presets
manager.add_style_preset("highlight", {"background-color": "yellow", "color": "black"})
manager.add_formatter_preset("CHF", {"type": "number", "prefix": "CHF ", "precision": 2})
```
---
## Future Considerations
All items below are :x: **not implemented**.
- **`row` parameter for column-level conditions**: Evaluate condition on a specific row
- **AND/OR conditions**: Add explicit `and`/`or` operators if `between`/`in` prove insufficient
- **Cell references**: Extend to `{"col": "x", "row": 0}` for specific cell and `{"col": "x", "row_offset": -1}` for
relative references
@@ -478,3 +537,4 @@ Global settings stored in `DataGridsManager`:
- **API source for enum**: `{"type": "api", "value": "https://...", ...}`
- **Searchable enum**: For large option lists
- **Formatter chaining**: Apply multiple formatters in sequence
- **DataGrid integration**: Connect `FormattingEngine` to `DataGrid.mk_body_cell_content()`