markdown.py 2.2 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. import pandas as pd
  17. from taipy.gui import Gui, State
  18. categories = ["Real Estate", "Stocks", "Cash", "Retirement", "Other"]
  19. amounts = [190000, 60000, 100000, 110000, 40000]
  20. # Return an array where each item represents the percentage of the corresponding value in the
  21. # input array:
  22. # [10, 10] -> [50, 50]
  23. # [1, 2, 3] -> [16, 33, 50]
  24. def compute_ratio(amounts: list[int]) -> list[int]:
  25. total = sum(amounts)
  26. return [int((amount / total) * 100) for amount in amounts]
  27. # Initialize the ratio array
  28. ratio = compute_ratio(amounts)
  29. # Create the dataset used by the table
  30. data = pd.DataFrame({"Category": categories, "Amount": amounts, "%": ratio})
  31. # Called when the user edits a cell's value
  32. def update(state: State, var_name: str, payload: dict):
  33. # Batch the updates to the state
  34. with state:
  35. # Invoke the default processing for 'on_edit'
  36. # Because the table data is a Pandas data frame, the cell is updated automatically
  37. state.get_gui().table_on_edit(state, var_name, payload)
  38. # Update ratios based on the new cell value
  39. state.data["%"] = compute_ratio(state.data["Amount"])
  40. # Update the control
  41. state.refresh(var_name)
  42. page = "<|{data}|table|editable[Amount]|on_edit=update|no on_add|no on_delete|show_all|>"
  43. if __name__ == "__main__":
  44. Gui(page).run(title="Table - Action on edit")