-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] Enable parametrisation of dynamic data functions (#482)
Signed-off-by: Antony Milne <49395058+antonymilne@users.noreply.github.com> Co-authored-by: Petar Pejovic <108530920+petar-qb@users.noreply.github.com>
- Loading branch information
1 parent
cdd4cec
commit 31d706e
Showing
22 changed files
with
670 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,3 +87,7 @@ Plotly's | |
Gunicorn | ||
dataframe | ||
streamlit | ||
memoization | ||
setosa | ||
versicolor | ||
virginica |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
vizro-core/changelog.d/20240528_133148_petar_pejovic_parametrise_dynamic_data.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<!-- | ||
A new scriv changelog fragment. | ||
Uncomment the section that is right (remove the HTML comment wrapper). | ||
--> | ||
|
||
### Highlights ✨ | ||
|
||
- Enable dynamic data parametrization, so that different data can be loaded while the dashboard is running ([#482](https://github.com/mckinsey/vizro/pull/482)) | ||
|
||
<!-- | ||
### Removed | ||
- 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)) | ||
--> | ||
<!-- | ||
### Added | ||
- A bullet item for the Added 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)) | ||
--> | ||
<!-- | ||
### Changed | ||
- 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)) | ||
--> | ||
<!-- | ||
### Deprecated | ||
- 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)) | ||
--> | ||
<!-- | ||
### Fixed | ||
- 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)) | ||
--> | ||
<!-- | ||
### Security | ||
- 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)) | ||
--> |
Binary file added
BIN
+2.45 MB
vizro-core/docs/assets/user_guides/data/parametrized_dynamic_data.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,101 @@ | ||
"""Dev app to try things out.""" | ||
"""Example to show dashboard configuration.""" | ||
|
||
import numpy as np | ||
import vizro.models as vm | ||
import vizro.plotly.express as px | ||
from flask_caching import Cache | ||
from vizro import Vizro | ||
from vizro.tables import dash_data_table | ||
from vizro.actions import export_data | ||
from vizro.managers import data_manager | ||
|
||
df = px.data.gapminder() | ||
|
||
dropdown_column = "Label" | ||
dropdown_options = ["-- A --", "-- B --", "-- C --"] | ||
# Note need to specify default value if have Filter since that calls data load function | ||
# Then have problem that filter options don't get updated when data source changes | ||
def load_iris_data(points=1, additional_points=1): | ||
"""Load iris data.""" | ||
iris = px.data.iris() | ||
return iris.sample(points + additional_points) | ||
|
||
# Add a 'Label' column to the data where options are randomly selected between 'A', 'B', 'C' | ||
df[dropdown_column] = np.random.choice(dropdown_options, size=len(df)) | ||
|
||
# Drop the 'iso_alpha' and 'iso_num' columns | ||
df.drop(["iso_alpha", "iso_num"], axis=1, inplace=True) | ||
data_manager["iris"] = load_iris_data | ||
|
||
# If you want to cache the data on the page_2 differently from page_1, you can define another data_manager entry with | ||
# the same function and assign it to the page_2 graphs. e.g. `data_manager["iris_2"] = load_iris_data` | ||
|
||
page = vm.Page( | ||
title="Table Page", | ||
# SimpleCache | ||
data_manager.cache = Cache(config={"CACHE_TYPE": "SimpleCache"}) | ||
|
||
# RedisCache | ||
# data_manager.cache = Cache( | ||
# config={"CACHE_TYPE": "RedisCache", "CACHE_REDIS_HOST": "localhost", "CACHE_REDIS_PORT": 6379} | ||
# ) | ||
|
||
# Timeout | ||
data_manager["iris"].timeout = 30 | ||
|
||
|
||
# TEST CASE: | ||
# There are 2 Parameters per page that control the number of points and additional points. | ||
# Set all of them to same number and see that the output for all of them will be the same if cache is configured, | ||
# otherwise the output will be different. | ||
|
||
page_1 = vm.Page( | ||
title="My first page", | ||
components=[ | ||
vm.Graph( | ||
id="graph_1", figure=px.scatter(data_frame="iris", x="sepal_length", y="petal_width", color="species") | ||
), | ||
vm.Graph( | ||
id="graph_2", figure=px.scatter(data_frame="iris", x="sepal_length", y="petal_width", color="species") | ||
), | ||
vm.Button(text="Export", actions=[vm.Action(function=export_data())]), | ||
], | ||
controls=[ | ||
vm.Parameter( | ||
targets=["graph_1.x", "graph_2.x"], selector=vm.RadioItems(options=["sepal_length", "sepal_width"]) | ||
), | ||
vm.Parameter( | ||
targets=["graph_1.data_frame.points", "graph_1.data_frame.additional_points"], | ||
selector=vm.Slider(title="Graph 1 points / Graph 1 additional_points", min=1, max=10, step=1), | ||
), | ||
vm.Parameter( | ||
targets=["graph_2.data_frame.points", "graph_2.data_frame.additional_points"], | ||
selector=vm.Slider(title="Graph 2 points / Graph 2 additional_points", min=1, max=10, step=1), | ||
), | ||
vm.Filter(column="species", selector=vm.Dropdown(options=["setosa", "versicolor", "virginica"])), | ||
], | ||
) | ||
|
||
page_2 = vm.Page( | ||
title="My second page", | ||
components=[ | ||
vm.Table( | ||
title="Table", | ||
figure=dash_data_table( | ||
data_frame=df, | ||
columns=[ | ||
{"name": i, "id": i, "presentation": "dropdown"} if i == dropdown_column else {"name": i, "id": i} | ||
for i in df.columns | ||
], | ||
editable=True, | ||
dropdown={ | ||
dropdown_column: { | ||
"options": [{"label": i, "value": i} for i in dropdown_options], | ||
"clearable": False, | ||
}, | ||
}, | ||
), | ||
vm.Graph( | ||
id="graph_second_1", | ||
figure=px.scatter(data_frame="iris", x="sepal_length", y="petal_width", color="species"), | ||
), | ||
vm.Graph( | ||
id="graph_second_2", | ||
figure=px.scatter(data_frame="iris", x="sepal_length", y="petal_width", color="species"), | ||
), | ||
vm.Button(text="Export", actions=[vm.Action(function=export_data())]), | ||
], | ||
controls=[ | ||
vm.Parameter( | ||
targets=["graph_second_1.x", "graph_second_2.x"], | ||
selector=vm.RadioItems(options=["sepal_length", "sepal_width"]), | ||
), | ||
vm.Parameter( | ||
targets=["graph_second_1.data_frame.points", "graph_second_2.data_frame.points"], | ||
selector=vm.Slider(title="Graph 1 points / Graph 2 points", min=1, max=10, step=1), | ||
), | ||
vm.Parameter( | ||
targets=["graph_second_1.data_frame.additional_points", "graph_second_2.data_frame.additional_points"], | ||
selector=vm.Slider(title="Graph 1 additional_points / Graph 2 additional_points", min=1, max=10, step=1), | ||
), | ||
vm.Filter(column="species", selector=vm.Dropdown(options=["setosa", "versicolor", "virginica"])), | ||
], | ||
controls=[vm.Filter(column="continent")], | ||
) | ||
|
||
dashboard = vm.Dashboard(pages=[page]) | ||
dashboard = vm.Dashboard(pages=[page_1, page_2]) | ||
|
||
if __name__ == "__main__": | ||
Vizro().build(dashboard).run() |
Oops, something went wrong.