slider-date-range.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if __name__ == "__main__":
  23. # Create the list of dates (all year 2000)
  24. all_dates = {}
  25. all_dates_str = []
  26. start_date = date(2000, 1, 1)
  27. end_date = date(2001, 1, 1)
  28. a_date = start_date
  29. while a_date < end_date:
  30. date_str = a_date.strftime("%Y/%m/%d")
  31. all_dates_str.append(date_str)
  32. all_dates[date_str] = a_date
  33. a_date += timedelta(days=1)
  34. # Initial selection: first and last day
  35. dates = [all_dates_str[1], all_dates_str[-1]]
  36. # These two variables are used in text controls
  37. start_sel = all_dates[dates[0]]
  38. end_sel = all_dates[dates[1]]
  39. page = """
  40. # Slider - Date range
  41. <|{dates}|slider|lov={all_dates_str}|>
  42. Start: <|{start_sel}|text|format=d MMM|>
  43. End: <|{end_sel}|text|format=d MMM|>
  44. """
  45. Gui(page).run()