Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Select.from_values class method for initializing with iterator #3743

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8f7fdb0
prototype to use class method to initialize Select object using an it…
azinneck0485 Nov 21, 2023
5793ef1
add docstring and optional arguments to Select.from_values()
azinneck0485 Nov 23, 2023
450553f
add test widget for Select.from_values() and update documentation to …
azinneck0485 Nov 23, 2023
bafaf97
add snapshot tests to Select.from_values() class method and update sn…
azinneck0485 Nov 23, 2023
4fe0ecf
update changelog
azinneck0485 Nov 23, 2023
c9cb679
changes to Select.from_value doc and variables names per PR review co…
azinneck0485 Nov 25, 2023
029d96f
Merge remote-tracking branch 'upstream/main' into Issue3691_AltSelect…
azinneck0485 Nov 25, 2023
fc874aa
fix merge mistakes
azinneck0485 Nov 25, 2023
5cccc2d
fix more merge mistakes
azinneck0485 Nov 25, 2023
2a403c3
fix more merge mistakes (typos)
azinneck0485 Nov 25, 2023
7d869bd
change Select.from_value() input parameters to match changes made fro…
azinneck0485 Nov 25, 2023
e6264b1
re-update snapshots for Select.from_values()
azinneck0485 Nov 25, 2023
c14aa59
add new tests for Select.from() based on related tests added by merge
azinneck0485 Nov 25, 2023
0ee6aff
update changelog
azinneck0485 Nov 25, 2023
5c0ea97
update another snapshot
azinneck0485 Nov 25, 2023
2a6db4b
update Select documentation per code review
azinneck0485 Nov 28, 2023
23bb964
update doc string and variable names in _select.py per code review
azinneck0485 Nov 28, 2023
d305aec
Merge branch 'main' into Issue3691_AltSelectInit
rodrigogiraoserrao Nov 30, 2023
ed3f106
Fix docstrings.
rodrigogiraoserrao Nov 30, 2023
ae1a43a
add return type hint for from_values class method
azinneck0485 Nov 30, 2023
6516920
remove unneeded snapshot tests for Select.from_values and update snap…
azinneck0485 Nov 30, 2023
903920d
Merge remote-tracking branch 'origin/Issue3691_AltSelectInit' into Is…
azinneck0485 Nov 30, 2023
91efd51
update snapshots again after merge
azinneck0485 Dec 1, 2023
4a21734
change type hint for Select.from_values based on review comments
azinneck0485 Dec 1, 2023
3c484a8
docstring and type hint changes to Select.from_values per code review…
azinneck0485 Dec 5, 2023
b9f063b
Add period.
rodrigogiraoserrao Dec 5, 2023
986f14e
Merge branch 'main' into Issue3691_AltSelectInit
rodrigogiraoserrao Dec 5, 2023
81727f9
Merge branch 'main' into Issue3691_AltSelectInit
rodrigogiraoserrao Dec 5, 2023
5a75777
Update snapshot.
rodrigogiraoserrao Dec 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- CSS error reporting will no longer provide links to the files in question https://github.com/Textualize/textual/pull/3582
- inline CSS error reporting will report widget/class variable where the CSS was read from https://github.com/Textualize/textual/pull/3582

### Added
- Added `Select.from_values` class method that can be used to initialize a Select control with an iterator https://github.com/Textualize/textual/issues/3691
azinneck0485 marked this conversation as resolved.
Show resolved Hide resolved

## [0.41.0] - 2023-10-31

### Fixed
Expand All @@ -43,7 +46,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added HorizontalPad to pad.py https://github.com/Textualize/textual/pull/3571
- Added `AwaitComplete` class, to be used for optionally awaitable return values https://github.com/Textualize/textual/pull/3498


### Changed

- Breaking change: `Button.ACTIVE_EFFECT_DURATION` classvar converted to `Button.active_effect_duration` attribute https://github.com/Textualize/textual/pull/3498
Expand Down
26 changes: 26 additions & 0 deletions docs/examples/widgets/select_from_values_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Header, Select

LINES = """I must not fear.
Fear is the mind-killer.
Fear is the little-death that brings total obliteration.
I will face my fear.
I will permit it to pass over me and through me.""".splitlines()


class SelectApp(App):
CSS_PATH = "select.tcss"

def compose(self) -> ComposeResult:
yield Header()
yield Select.from_values(LINES)

@on(Select.Changed)
def select_changed(self, event: Select.Changed) -> None:
self.title = str(event.value)


if __name__ == "__main__":
app = SelectApp()
app.run()
26 changes: 26 additions & 0 deletions docs/widgets/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ The following example presents a `Select` with a number of options.
--8<-- "docs/examples/widgets/select.tcss"
```

## Example
azinneck0485 marked this conversation as resolved.
Show resolved Hide resolved

The following example presents a `Select` created using the `from_values` class method.

=== "Output"

```{.textual path="docs/examples/widgets/select_from_values_widget.py"}
```

=== "Output (expanded)"

```{.textual path="docs/examples/widgets/select_from_values_widget.py" press="tab,enter,down,down"}
```


=== "select_from_values_widget.py"

```python
--8<-- "docs/examples/widgets/select_from_values_widget.py"
```

=== "select.tcss"

```sass
--8<-- "docs/examples/widgets/select.tcss"
```

## Reactive Attributes

Expand Down
38 changes: 38 additions & 0 deletions src/textual/widgets/_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,44 @@ def __init__(
self._value: SelectType | None = value
self._options = options

@classmethod
def from_values(
cls,
opts: [SelectType],
rodrigogiraoserrao marked this conversation as resolved.
Show resolved Hide resolved
*,
prompt: str = "Select",
allow_blank: bool = True,
value: SelectType | None = None,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
):
azinneck0485 marked this conversation as resolved.
Show resolved Hide resolved
"""Initialize the Select control with options specified by an iterator

Args:
options: Options to select from.
rodrigogiraoserrao marked this conversation as resolved.
Show resolved Hide resolved
prompt: Text to show in the control when no option is select.
allow_blank: Allow the selection of a blank option.
value: Initial value (should be one of the values in `options`).
name: The name of the select control.
id: The ID of the control the DOM.
classes: The CSS classes of the control.
disabled: Whether the control is disabled or not.
azinneck0485 marked this conversation as resolved.
Show resolved Hide resolved
"""
azinneck0485 marked this conversation as resolved.
Show resolved Hide resolved
opts_iterator = [(str(o), o) for o in opts]

return cls(
opts_iterator,
azinneck0485 marked this conversation as resolved.
Show resolved Hide resolved
prompt=prompt,
allow_blank=allow_blank,
value=value,
name=name,
id=id,
classes=classes,
disabled=disabled,
)

def set_options(self, options: Iterable[tuple[RenderableType, SelectType]]) -> None:
"""Set the options for the Select.

Expand Down
Loading