styling_dynamic.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 taipy.gui import Gui, Markdown
  17. # The word provided by the user
  18. word = ""
  19. # A potential error message
  20. error_text = ""
  21. # Is the Validate button enabled?
  22. valid = False
  23. # CSS class to add to control showing a problem
  24. error_cls = None
  25. def on_change(state, var_name, value):
  26. # If the provided word has changed
  27. if var_name == "word":
  28. # If not empty and does not have five characters
  29. if value and len(value) != 5:
  30. # Set the error message
  31. state.error_text = " Five characters are required"
  32. # Disable the Validate button
  33. state.valid = False
  34. # Add the invalid-value class, used by the input control and error message
  35. state.error_cls = "invalid-value"
  36. # The text is valid.
  37. else:
  38. # Remove error message
  39. state.error_text = ""
  40. # Enable the Validate button if the word is not empty
  41. state.valid = bool(value)
  42. # Remove the invalid-value class, used by the input control and error message
  43. state.error_cls = None
  44. page = Markdown(
  45. """
  46. Enter a five-letter word:
  47. <|{word}|input|class_name={error_cls}|><|{error_text}|text|class_name={error_cls}|>
  48. <|Validate|button|active={valid}|>
  49. """,
  50. style={".invalid-value": {"background-color": "red"}},
  51. )
  52. if __name__ == "__main__":
  53. Gui(page).run(title="Styling - Dynamic styling")