Skip to content

Commit 77c898b

Browse files
[Feat] Enable dynamic DatePicker (#1039)
Co-authored-by: petar-qb <[email protected]>
1 parent 3d2fd2c commit 77c898b

File tree

7 files changed

+235
-298
lines changed

7 files changed

+235
-298
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
-->
6+
7+
<!--
8+
### Highlights ✨
9+
10+
- A bullet item for the Highlights ✨ category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
11+
12+
-->
13+
<!--
14+
### Removed
15+
16+
- A bullet item for the Removed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
17+
18+
-->
19+
### Added
20+
21+
- `DatePicker` filters update automatically when underlying dynamic data changes. See the [user guide on dynamic filters](https://vizro.readthedocs.io/en/stable/pages/user-guides/data/#filters) for more information. ([#1039](https://github.com/mckinsey/vizro/pull/1039))
22+
23+
<!--
24+
### Changed
25+
26+
- A bullet item for the Changed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
27+
28+
-->
29+
<!--
30+
### Deprecated
31+
32+
- A bullet item for the Deprecated category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
33+
34+
-->
35+
<!--
36+
### Fixed
37+
38+
- A bullet item for the Fixed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
39+
40+
-->
41+
<!--
42+
### Security
43+
44+
- A bullet item for the Security category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
45+
46+
-->

vizro-core/docs/pages/user-guides/data.md

+14-8
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,14 @@ When the page is refreshed, the behavior of a dynamic filter is as follows:
355355

356356
- The filter's selector updates its available values:
357357
- For [categorical selectors](selectors.md#categorical-selectors), `options` updates to give all unique values found in `column` across all the data sources of components in `targets`.
358-
- For [numerical selectors](selectors.md#numerical-selectors), `min` and `max` update to give the overall minimum and maximum values found in `column` across all the data sources of components in `targets`.
358+
- For [numerical selectors](selectors.md#numerical-selectors) and [temporal selectors](selectors.md#temporal-selectors), `min` and `max` update to give the overall minimum and maximum values found in `column` across all the data sources of components in `targets`.
359359
- The value selected on screen by a dashboard user _does not_ change. If the selected value is not already present in the new set of available values then the `options` or `min` and `max` are modified to include it. In this case, the filtering operation might result in an empty DataFrame.
360360
- Even though the values present in a data source can change, the schema should not: `column` should remain present and of the same type in the data sources. The `targets` of the filter and selector type cannot change while the dashboard is running. For example, a `vm.Dropdown` selector cannot turn into `vm.RadioItems`.
361361

362-
For example, let us add two filters to the [dynamic data example](#dynamic-data) above:
362+
For example, to add three filters to the [dynamic data example](#dynamic-data) above:
363363

364364
!!! example "Dynamic filters"
365-
```py hl_lines="10 20 21"
365+
```py hl_lines="10 11 21 22 23"
366366
from vizro import Vizro
367367
import pandas as pd
368368
import vizro.plotly.express as px
@@ -372,7 +372,8 @@ For example, let us add two filters to the [dynamic data example](#dynamic-data)
372372

373373
def load_iris_data():
374374
iris = pd.read_csv("iris.csv")
375-
return iris.sample(5) # (1)!
375+
iris["date_column"] = pd.date_range(start=pd.to_datetime("2025-01-01"), periods=len(iris), freq="D") # (1)!
376+
return iris.sample(5) # (2)!
376377

377378
data_manager["iris"] = load_iris_data
378379

@@ -382,8 +383,9 @@ For example, let us add two filters to the [dynamic data example](#dynamic-data)
382383
vm.Graph(figure=px.box("iris", x="species", y="petal_width", color="species"))
383384
],
384385
controls=[
385-
vm.Filter(column="species"), # (2)!
386-
vm.Filter(column="sepal_length"), # (3)!
386+
vm.Filter(column="species"), # (3)!
387+
vm.Filter(column="sepal_length"), # (4)!
388+
vm.Filter(column="date_column"), # (5)!
387389
],
388390
)
389391

@@ -392,25 +394,29 @@ For example, let us add two filters to the [dynamic data example](#dynamic-data)
392394
Vizro().build(dashboard).run()
393395
```
394396

397+
1. Add a new column `"date_column"` to the `"iris"` data source. This column is used to demonstrate usage of a temporal dynamic filter.
395398
1. We sample only 5 rather than 50 points so that changes to the available values in the filtered columns are more apparent when the page is refreshed.
396399
1. This filter implicitly controls the dynamic data source `"iris"`, which supplies the `data_frame` to the targeted `vm.Graph`. On page refresh, Vizro reloads this data, finds all the unique values in the `"species"` column and sets the categorical selector's `options` accordingly.
397400
1. Similarly, on page refresh, Vizro finds the minimum and maximum values of the `"sepal_length"` column in the reloaded data and sets new `min` and `max` values for the numerical selector accordingly.
401+
1. Similarly, on page refresh, Vizro finds the minimum and maximum values of the `"date_column"` column in the reloaded data and sets new `min` and `max` values for the temporal selector accordingly.
398402

399-
Consider a filter that depends on dynamic data, where you do **not** want the available values to change when the dynamic data changes. You should manually specify the `selector`'s `options` field (categorical selector) or `min` and `max` fields (numerical selector). In the above example, this could be achieved as follows:
403+
Consider a filter that depends on dynamic data, where you do **not** want the available values to change when the dynamic data changes. You should manually specify the `selector`'s `options` field (categorical selector) or `min` and `max` fields (numerical and temporal selector). In the above example, this could be achieved as follows:
400404

401405
```python title="Override selector options to make a dynamic filter static"
402406
controls = [
403407
vm.Filter(column="species", selector=vm.Dropdown(options=["setosa", "versicolor", "virginica"])),
404408
vm.Filter(column="sepal_length", selector=vm.RangeSlider(min=4.3, max=7.9)),
409+
vm.Filter(column="date_column", selector=vm.DatePickerRange(min="2025-01-01", max="2025-05-29")),
405410
]
406411
```
407412

408-
If you [use a specific selector](filters.md#change-selector) for a dynamic filter without manually specifying `options` (categorical selector) or `min` and `max` (numerical selector) then the selector remains dynamic. For example:
413+
If you [use a specific selector](filters.md#change-selector) for a dynamic filter without manually specifying `options` (categorical selector) or `min` and `max` (numerical and temporal selector) then the selector remains dynamic. For example:
409414

410415
```python title="Dynamic filter with specific selector is still dynamic"
411416
controls = [
412417
vm.Filter(column="species", selector=vm.Checklist()),
413418
vm.Filter(column="sepal_length", selector=vm.Slider()),
419+
vm.Filter(column="date_column", selector=vm.DatePicker(range=False)),
414420
]
415421
```
416422

0 commit comments

Comments
 (0)