builder.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 random import randrange
  17. import taipy.gui.builder as tgb
  18. from taipy.gui import Gui
  19. x_range = range(0, 11)
  20. # The dataset is made of three arrays:
  21. # x: an integer value from 0 to 5
  22. # y: the square of the value in the "x" column
  23. # z: a random integer betweewn 0 and 5
  24. data = {"x": x_range, "y": [x * x for x in x_range], "z": [randrange(6) for _ in x_range]}
  25. def xy_style(_1, _2, index, _3, column_name):
  26. return (
  27. # The background color of the 'x' column alternates between two shades of green,
  28. # varying in lightness.
  29. ("greenish" if index % 2 else "lightgreenish")
  30. if column_name == "x"
  31. # The background color of the 'y' column alternates between two shades of green,
  32. # varying in lightness
  33. else ("reddish" if index % 2 else "lightreddish")
  34. )
  35. def z_style(_, value):
  36. # Build a CSS classname from the value.
  37. # The lower the value is, the lighter the color is.
  38. return f"col{value}"
  39. with tgb.Page(
  40. style={
  41. ".reddish": {"color": "white", "background-color": "#bf1313"},
  42. ".lightreddish": {"color": "black", "background-color": "#ff1919", "font-weight": "bold"},
  43. ".greenish": {"color": "white", "background-color": "#75bf75"},
  44. ".lightgreenish": {"color": "black", "background-color": "#9cff9c", "font-weight": "bold"},
  45. ".col0": {"background-color": "#d0d0d0"},
  46. ".col1": {"background-color": "#a4a0cf"},
  47. ".col2": {"background-color": "#7970cf"},
  48. ".col3": {"background-color": "#4e40cf", "color": "white"},
  49. ".col4": {"background-color": "#2410cf", "color": "white"},
  50. ".col5": {"background-color": "#1b02a8", "color": "white"},
  51. }
  52. ) as page:
  53. tgb.table("{data}", style__x=xy_style, style__y=xy_style, style__z=z_style, show_all=True)
  54. # Using a lambda function instead of z_style:
  55. #tgb.table("{data}", style__x=xy_style, style__y=xy_style, style__z=lambda _, v: f"col{v}", show_all=True)
  56. if __name__ == "__main__":
  57. Gui(page).run(title="Table - Styling cells")