add favicon support

This commit is contained in:
Abubakar Abid 2022-01-13 22:41:21 -06:00
parent 245ec0f75f
commit b832d03913
3 changed files with 24 additions and 7 deletions

View File

@ -117,6 +117,14 @@ def static_resource(path: str):
raise HTTPException(status_code=404, detail="Static file not found")
@app.get('/favicon.ico')
async def favicon():
if app.favicon_path:
return FileResponse(app.favicon_path)
else:
raise HTTPException(status_code=404, detail="Favicon file not found")
@app.get("/api", response_class=HTMLResponse) # Needed for Spaces
@app.get("/api/", response_class=HTMLResponse)
def api_docs(request: Request):
@ -307,6 +315,9 @@ if __name__ == '__main__': # Run directly for debugging: python app.py
app.interface.config = app.interface.get_config_file()
app.interface.show_error = True
app.interface.flagging_callback.setup(app.interface.flagging_dir)
app.favicon_path = None
app.tokens = {}
auth = True
if auth:
app.interface.auth = ("a", "b")
@ -314,5 +325,5 @@ if __name__ == '__main__': # Run directly for debugging: python app.py
app.interface.auth_message = None
else:
app.auth = None
app.tokens = {}
uvicorn.run(app)

View File

@ -518,7 +518,8 @@ class Interface:
height: int = 500,
width: int = 900,
encrypt: bool = False,
cache_examples: bool = False
cache_examples: bool = False,
favicon_path: Optional[str] = None,
) -> Tuple[flask.Flask, str, str]:
"""
Launches the webserver that serves the UI for the interface.
@ -540,6 +541,7 @@ class Interface:
height (int): The height in pixels of the <iframe> element containing the interface (used if inline=True)
encrypt (bool): If True, flagged data will be encrypted by key provided by creator at launch
cache_examples (bool): If True, examples outputs will be processed and cached in a folder, and will be used if a user uses an example input.
favicon_path (str): If a path to an file (.png, .gif, or .ico) is provided, it will be used as the favicon for the web page.
Returns:
app (flask.Flask): Flask app object
path_to_local_server (str): Locally accessible link
@ -556,6 +558,7 @@ class Interface:
self.show_error = show_error
self.height = self.height or height
self.width = self.width or width
self.favicon_path = favicon_path
if self.encrypt is None:
self.encrypt = encrypt
@ -575,7 +578,7 @@ class Interface:
cache_interface_examples(self)
server_port, path_to_local_server, app, server = networking.start_server(
self, server_name, server_port, self.auth)
self, server_name, server_port)
self.local_url = path_to_local_server
self.server_port = server_port

View File

@ -100,7 +100,6 @@ def start_server(
interface: Interface,
server_name: Optional[str] = None,
server_port: Optional[int] = None,
auth: Optional[Callable | Tuple[str, str] | List[Tuple[str, str]]] = None,
) -> Tuple[int, str, fastapi.FastAPI, threading.Thread, None]:
"""Launches a local server running the provided Interface
Parameters:
@ -126,6 +125,7 @@ def start_server(
url_host_name = "localhost" if server_name == "0.0.0.0" else server_name
path_to_local_server = "http://{}:{}/".format(url_host_name, port)
auth = interface.auth
if auth is not None:
if not callable(auth):
app.auth = {account[0]: account[1] for account in auth}
@ -135,6 +135,9 @@ def start_server(
app.auth = None
app.interface = interface
app.cwd = os.getcwd()
app.favicon_path = interface.favicon_path
app.tokens = {}
if app.interface.enable_queue:
if auth is not None or app.interface.encrypt:
raise ValueError("Cannot queue with encryption or authentication enabled.")
@ -143,7 +146,7 @@ def start_server(
app.queue_thread.start()
if interface.save_to is not None: # Used for selenium tests
interface.save_to["port"] = port
app.tokens = {}
config = uvicorn.Config(app=app, port=port, host=server_name,
log_level="warning")
server = Server(config=config)