main_md.py 2.9 KB

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