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

Code before:
        _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)

    def _load_scripts(self, script_paths: t.Optional[t.Union[str, t.List[t.Union[str, Path]]]]):
        if script_paths is None:
            return

        if isinstance(script_paths, (str, Path)):
            script_paths = [script_paths]

        for script_path in script_paths:
            self._load_script(script_path)

        if not self.__script_files:
            raise Exception("No JavaScript files found in the specified script paths.")

    def _load_script(self, script_path: t.Union[str, Path]):
        if isinstance(script_path, str):
            parsed_url = urlparse(script_path)
            if parsed_url.netloc:
                self.__script_files.append(script_path)
                return
            script_path = Path(script_path)

        if script_path.exists() and script_path.is_file() and script_path.suffix == ".js":
            self.__script_files.append(str(script_path))
        else:
            raise ValueError(f"Script path '{script_path}' does not exist, is not a file, or is not a JavaScript file.")


    @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.

Review comment (suggestion):
Note that this could be a local `load_script()` declared in `_load_scripts()`, after script_paths was tested...

Code after:

        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 __load_scripts(self, script_paths: t.Optional[t.Union[str, Path, t.List[t.Union[str, Path]]]]):
        if script_paths is None:
            return
        else:
            if isinstance(script_paths, (str, Path)):
                script_paths = [script_paths]

            for script_path in script_paths:
                if isinstance(script_path, str):
                    parsed_url = urlparse(script_path)
                    if parsed_url.netloc:
                        self.__script_files.append(script_path)
                        continue
                    script_path = Path(script_path)

                if isinstance(script_path,
                              Path) and script_path.exists() and script_path.is_file() and script_path.suffix == ".js":
                    self.__script_files.append(str(script_path))
                else:
                    _warn(f"Script path '{script_path}' does not exist, is not a file, or is not a JavaScript file.")

            if not self.__script_files:
                _warn("No JavaScript files found in the specified script paths.")

    @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()