sales.py 2.0 KB

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