Quellcode durchsuchen

add some swagger UI function to ui.run() (#4050)

Add some params to ui.run().
You can add more params into Swagger UI, such as project title,
description, version, etc.
Like this:
```python
ui.run(
        title=siteName, #If the title of the document is not defined separately below, it will default to following this parameter.
        native=False,
        language='zh-CN',
        fastapi_docs=True, #Only if this parameter's value is True will the following content be displayed.
        fastapi_docs_title='NiceGUI',
        fastapi_docs_description='some description',
        fastapi_docs_version='2.5.0',
        fastapi_docs_terms_of_service='https://nicegui.io',
        fastapi_docs_contact={
            'name': 'zauberzeug', 
            'url': 'https://nicegui.io', 
            'email': 'example@example.com'
            },
        fastapi_docs_license_info={
            'name': 'MIT',
            'url': 'https://www.example.com',
        })
```
You can refer to my project:

![image](https://github.com/user-attachments/assets/c9b79cfa-fa0b-469d-a85e-5b75476dc734)

---------

Co-authored-by: Falko Schindler <falko@zauberzeug.com>
于小丘 vor 5 Monaten
Ursprung
Commit
a5bbea668f
1 geänderte Dateien mit 35 neuen und 3 gelöschten Zeilen
  1. 35 3
      nicegui/ui_run.py

+ 35 - 3
nicegui/ui_run.py

@@ -2,7 +2,7 @@ import multiprocessing
 import os
 import sys
 from pathlib import Path
-from typing import Any, List, Literal, Optional, Tuple, Union
+from typing import Any, List, Literal, Optional, Tuple, TypedDict, Union
 
 from starlette.routing import Route
 from uvicorn.main import STARTUP_FAILURE
@@ -21,6 +21,28 @@ from .server import CustomServerConfig, Server
 APP_IMPORT_STRING = 'nicegui:app'
 
 
+class ContactDict(TypedDict):
+    name: Optional[str]
+    url: Optional[str]
+    email: Optional[str]
+
+
+class LicenseInfoDict(TypedDict):
+    name: str
+    identifier: Optional[str]
+    url: Optional[str]
+
+
+class DocsConfig(TypedDict):
+    title: Optional[str]
+    summary: Optional[str]
+    description: Optional[str]
+    version: Optional[str]
+    terms_of_service: Optional[str]
+    contact: Optional[ContactDict]
+    license_info: Optional[LicenseInfoDict]
+
+
 def run(*,
         host: Optional[str] = None,
         port: Optional[int] = None,
@@ -31,7 +53,7 @@ def run(*,
         language: Language = 'en-US',
         binding_refresh_interval: float = 0.1,
         reconnect_timeout: float = 3.0,
-        fastapi_docs: bool = False,
+        fastapi_docs: Union[bool, DocsConfig] = False,
         show: bool = True,
         on_air: Optional[Union[str, Literal[True]]] = None,
         native: bool = False,
@@ -64,7 +86,7 @@ def run(*,
     :param language: language for Quasar elements (default: `'en-US'`)
     :param binding_refresh_interval: time between binding updates (default: `0.1` seconds, bigger is more CPU friendly)
     :param reconnect_timeout: maximum time the server waits for the browser to reconnect (default: 3.0 seconds)
-    :param fastapi_docs: whether to enable FastAPI's automatic documentation with Swagger UI, ReDoc, and OpenAPI JSON (default: `False`)
+    :param fastapi_docs: enable FastAPI's automatic documentation with Swagger UI, ReDoc, and OpenAPI JSON (bool or dictionary as described `here<https://fastapi.tiangolo.com/tutorial/metadata/>`_, default: `False`)
     :param show: automatically open the UI in a browser tab (default: `True`)
     :param on_air: tech preview: `allows temporary remote access <https://nicegui.io/documentation/section_configuration_deployment#nicegui_on_air>`_ if set to `True` (default: disabled)
     :param native: open the UI in a native window of size 800x600 (default: `False`, deactivates `show`, automatically finds an open port)
@@ -113,6 +135,16 @@ def run(*,
             core.app.redoc_url = '/redoc'
         if not core.app.openapi_url:
             core.app.openapi_url = '/openapi.json'
+        if isinstance(fastapi_docs, dict):
+            core.app.title = fastapi_docs.get('title') or title
+            core.app.summary = fastapi_docs.get('summary')
+            core.app.description = fastapi_docs.get('description') or ''
+            core.app.version = fastapi_docs.get('version') or '0.1.0'
+            core.app.terms_of_service = fastapi_docs.get('terms_of_service')
+            contact = fastapi_docs.get('contact')
+            license_info = fastapi_docs.get('license_info')
+            core.app.contact = dict(contact) if contact else None
+            core.app.license_info = dict(license_info) if license_info else None
         core.app.setup()
 
     if on_air: