slider_date_range.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. # -----------------------------------------------------------------------------------------
  12. # To execute this script, make sure that the taipy-gui package is installed in your
  13. # Python environment and run:
  14. # python <script>
  15. # -----------------------------------------------------------------------------------------
  16. from datetime import date, timedelta
  17. from taipy.gui import Gui
  18. def on_change(state, _, var_value):
  19. # Update the text controls
  20. state.start_sel = all_dates[var_value[0]]
  21. state.end_sel = all_dates[var_value[1]]
  22. # Create the list of dates (all year 2000)
  23. all_dates = {}
  24. all_dates_str = []
  25. start_date = date(2000, 1, 1)
  26. end_date = date(2001, 1, 1)
  27. a_date = start_date
  28. while a_date < end_date:
  29. date_str = a_date.strftime("%Y/%m/%d")
  30. all_dates_str.append(date_str)
  31. all_dates[date_str] = a_date
  32. a_date += timedelta(days=1)
  33. # Initial selection: first and last day
  34. dates = [all_dates_str[1], all_dates_str[-1]]
  35. # These two variables are used in text controls
  36. start_sel = all_dates[dates[0]]
  37. end_sel = all_dates[dates[1]]
  38. page = """
  39. <|{dates}|slider|lov={all_dates_str}|>
  40. Start: <|{start_sel}|text|format=d MMM|>
  41. End: <|{end_sel}|text|format=d MMM|>
  42. """
  43. if __name__ == "__main__":
  44. Gui(page).run(title="Slider - Date range")