sales.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. # Example on Page Modules.
  13. # Sales page.
  14. # -----------------------------------------------------------------------------------------
  15. from taipy.gui import Markdown, Page
  16. # SalesPage inherits from taipy.gui.Page
  17. class SalesPage(Page):
  18. def __init__(self) -> None:
  19. self.quarter = "Q1" # Initial selected quarter
  20. self.currency = "$" # Initial currency
  21. super().__init__()
  22. @staticmethod
  23. def compute_total(quarter: str, currency: str, data: dict[str, list[float]]) -> float:
  24. """Compute the total sales revenue for the selected quarter and currency."""
  25. # Sales data for the chosen quarter
  26. sold = data[f"Sales {quarter}"]
  27. # Prices in the chosen currency
  28. price = data[f"Price {currency}"]
  29. # Compute and return total revenue
  30. return sum(s * p for s, p in zip(sold, price))
  31. def create_page(self):
  32. """Create and return the page content."""
  33. return Markdown("""
  34. # Sales
  35. <|1 4|layout
  36. Select quarter: <|{quarter}|selector|lov=Q1;Q2;Q3|>
  37. Total: <|{SalesPage.compute_total(quarter, currency, data)}|format=%.02f|><br/>
  38. Currency: <|{currency}|toggle|lov=$;€|>
  39. |>
  40. <|{data}|table|columns=Items;Sales Q1;Sales Q2;Sales Q3|>
  41. [Goto Stock](stock)
  42. """)