makes favicon path configurable

By passing a path to another image to the `favicon_path` argument in
pydase.Server, the user can change the default favicon icon.
This commit is contained in:
Mose Müller 2024-11-26 11:28:15 +01:00
parent 204d426663
commit 187d8bcf28

View File

@ -60,6 +60,8 @@ class WebServer:
css: css:
Path to a custom CSS file for styling the frontend. If None, no custom Path to a custom CSS file for styling the frontend. If None, no custom
styles are applied. Defaults to None. styles are applied. Defaults to None.
favicon_path:
Path to a custom favicon.ico file. Defaults to None.
enable_cors: enable_cors:
Flag to enable or disable CORS policy. When True, CORS is enabled, allowing Flag to enable or disable CORS policy. When True, CORS is enabled, allowing
cross-origin requests. Defaults to True. cross-origin requests. Defaults to True.
@ -78,7 +80,9 @@ class WebServer:
data_service_observer: DataServiceObserver, data_service_observer: DataServiceObserver,
host: str, host: str,
port: int, port: int,
*,
css: str | Path | None = None, css: str | Path | None = None,
favicon_path: str | Path | None = None,
enable_cors: bool = True, enable_cors: bool = True,
config_dir: Path = ServiceConfig().config_dir, config_dir: Path = ServiceConfig().config_dir,
generate_web_settings: bool = WebServerConfig().generate_web_settings, generate_web_settings: bool = WebServerConfig().generate_web_settings,
@ -92,6 +96,11 @@ class WebServer:
self.css = css self.css = css
self.enable_cors = enable_cors self.enable_cors = enable_cors
self.frontend_src = frontend_src self.frontend_src = frontend_src
self.favicon_path: Path | str = favicon_path # type: ignore
if self.favicon_path is None:
self.favicon_path = self.frontend_src / "favicon.ico"
self._service_config_dir = config_dir self._service_config_dir = config_dir
self._generate_web_settings = generate_web_settings self._generate_web_settings = generate_web_settings
self._loop: asyncio.AbstractEventLoop self._loop: asyncio.AbstractEventLoop
@ -136,6 +145,11 @@ class WebServer:
f"{escaped_prefix}/assets/", f"{escaped_prefix}/assets/",
) )
modified_html = modified_html.replace(
"/favicon.ico",
f"{escaped_prefix}/favicon.ico",
)
return aiohttp.web.Response( return aiohttp.web.Response(
text=modified_html, content_type="text/html" text=modified_html, content_type="text/html"
) )
@ -174,7 +188,7 @@ class WebServer:
self, self,
request: aiohttp.web.Request, request: aiohttp.web.Request,
) -> aiohttp.web.FileResponse: ) -> aiohttp.web.FileResponse:
return aiohttp.web.FileResponse(self.frontend_src / "favicon.ico") return aiohttp.web.FileResponse(self.favicon_path)
async def _service_properties_route( async def _service_properties_route(
self, self,