From 20e0811d83290ca6cae5c4017e23dcbeb3b22d2b Mon Sep 17 00:00:00 2001 From: Hans Kallekleiv Date: Thu, 30 Jan 2020 14:35:36 +0100 Subject: [PATCH] Function to calculate rounded step values for RangeSlider (#186) --- webviz_config/utils/__init__.py | 1 + webviz_config/utils/_dash_component_utils.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 webviz_config/utils/_dash_component_utils.py diff --git a/webviz_config/utils/__init__.py b/webviz_config/utils/__init__.py index df5b989a..043a00a5 100644 --- a/webviz_config/utils/__init__.py +++ b/webviz_config/utils/__init__.py @@ -1,2 +1,3 @@ from ._available_port import get_available_port from ._silence_flask_startup import silence_flask_startup +from ._dash_component_utils import calculate_slider_step diff --git a/webviz_config/utils/_dash_component_utils.py b/webviz_config/utils/_dash_component_utils.py new file mode 100644 index 00000000..38b60a71 --- /dev/null +++ b/webviz_config/utils/_dash_component_utils.py @@ -0,0 +1,16 @@ +import math + + +def calculate_slider_step(min_value: float, max_value: float, steps=100): + """Calculates a step value for use in e.g. dcc.RangeSlider() component + that will always be rounded. + + The number of steps will be atleast the number + of input steps, but might not be precisely the same due to use of the floor function. + + This function is necessary since there is currently no precision control in the underlying + React component (https://github.com/react-component/slider/issues/275). + + """ + + return 10 ** math.floor(math.log10((max_value - min_value) / steps))