advanced-selection.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. import random
  17. from typing import List
  18. import numpy
  19. from taipy.gui import Gui
  20. # x = [0..20]
  21. x = list(range(0, 21))
  22. data = {
  23. "x": x,
  24. # A list of random values within [1, 10]
  25. "y": [random.uniform(1, 10) for _ in x],
  26. }
  27. layout = {
  28. # Force the Box select tool
  29. "dragmode": "select",
  30. # Remove all margins around the plot
  31. "margin": {"l": 0, "r": 0, "b": 0, "t": 0},
  32. }
  33. config = {
  34. # Hide Plotly's mode bar
  35. "displayModeBar": False
  36. }
  37. selected_indices: List = []
  38. mean_value = 0.0
  39. def on_change(state, var, val):
  40. if var == "selected_indices":
  41. state.mean_value = numpy.mean([data["y"][idx] for idx in val]) if len(val) else 0
  42. page = """
  43. # Advanced - Selection
  44. ## Mean of <|{len(selected_indices)}|raw|> selected points: <|{mean_value}|format=%.2f|raw|>
  45. <|{data}|chart|selected={selected_indices}|layout={layout}|plot_config={config}|>
  46. """
  47. Gui(page).run()