Repository: Avaiga/taipy
File: taipy/gui/gui.py (Python)

Code before:
        if page:
            self.add_page(name=Gui.__root_page_name, page=page)
        if pages is not None:
            self.add_pages(pages)
        if env_filename is not None:
            self.__env_filename = env_filename
        if libraries is not None:
            for library in libraries:
                Gui.add_library(library)
    def set_on_invalid_data_callback(self, callback: t.Callable[[t.Any], t.Optional[t.Any]]):
        """
        Set the callback function to handle unsupported data types.
        :param callback: A function that takes in unsupported data and returns transformed data.
        """
        if callable(callback):
            self._on_invalid_data_callback = callback
        else:
            raise ValueError("The callback must be a callable function.")

    def on_invalid_data(self, value: t.Any) -> t.Optional[t.Any]:
        """
        Invokes the callback to transform unsupported data into a valid format.
        :param value: The unsupported data type encountered.
        :return: Transformed data or None if no transformation is possible.
        """
        if self._on_invalid_data_callback is not None:
            return self._on_invalid_data_callback(value)
        else:
            # Default behavior if no callback is set
            # print(f"Unsupported data type encountered: {type(value)}")
            return None
    @staticmethod
    def add_library(library: ElementLibrary) -> None:
        """Add a custom visual element library.

        This application will be able to use custom visual elements defined in this library.

        Arguments:
            library: The custom visual element library to add to this application.

        Multiple libraries with the same name can be added. This allows to split multiple custom visual
        elements in several `ElementLibrary^` instances, but still refer to these elements with the same
        prefix in the page definitions.
        """
        if isinstance(library, ElementLibrary):
            _Factory.set_library(library)
            library_name = library.get_name()
            if library_name.isidentifier():
                libs = Gui.__extensions.get(library_name)
                if libs is None:
                    Gui.__extensions[library_name] = [library]

Review comment (suggestion):
on_invalid_data should be handled the same way as on_action

- [definition](https://github.com/Avaiga/taipy/blob/3297952c0e64bd10358fde8e63a3adf6aef4e613/taipy/gui/gui.py#L255)
- [assignment](https://github.com/Avaiga/taipy/blob/3297952c0e64bd10358fde8e63a3adf6aef4e613/taipy/gui/gui.py#L2583)

Code after:
                "sane_lists",
                "tables",
                "attr_list",
                "md_in_html",
                _TaipyMarkdownExtension(gui=self),
            ]
        )

        self.__event_manager = _EventManager()

        # Init Gui Hooks
        _Hooks()._init(self)

        if page:
            self.add_page(name=Gui.__root_page_name, page=page)
        if pages is not None:
            self.add_pages(pages)
        if env_filename is not None:
            self.__env_filename = env_filename
        if libraries is not None:
            for library in libraries:
                Gui.add_library(library)

    @staticmethod
    def add_library(library: ElementLibrary) -> None:
        """Add a custom visual element library.

        This application will be able to use custom visual elements defined in this library.

        Arguments:
            library: The custom visual element library to add to this application.

        Multiple libraries with the same name can be added. This allows to split multiple custom visual
        elements in several `ElementLibrary^` instances, but still refer to these elements with the same
        prefix in the page definitions.
        """
        if isinstance(library, ElementLibrary):
            _Factory.set_library(library)
            library_name = library.get_name()
            if library_name.isidentifier():
                libs = Gui.__extensions.get(library_name)
                if libs is None:
                    Gui.__extensions[library_name] = [library]
                else:
                    libs.append(library)
                _ElementApiGenerator().add_library(library)
            else:
                raise NameError(f"ElementLibrary passed to add_library() has an invalid name: '{library_name}'")
        else:  # pragma: no cover
            raise RuntimeError(
                f"add_library() argument should be a subclass of ElementLibrary instead of '{type(library)}'"