Skip to content

Commit

Permalink
Disable FastAPI docs by default (#3589)
Browse files Browse the repository at this point in the history
* disable FastAPI docs by default

* allow custom docs urls

* formatting

* activate fastapi_docs for the endpoint tests

---------

Co-authored-by: Falko Schindler <falko@zauberzeug.com>
  • Loading branch information
rodja and falkoschindler authored Aug 26, 2024
1 parent c4cccd9 commit c69db4d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nicegui/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class State(Enum):
class App(FastAPI):

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
super().__init__(**kwargs, docs_url=None, redoc_url=None, openapi_url=None)
self.native = NativeConfig()
self.storage = Storage()
self.urls = ObservableSet()
Expand Down
11 changes: 11 additions & 0 deletions nicegui/ui_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def run(*,
language: Language = 'en-US',
binding_refresh_interval: float = 0.1,
reconnect_timeout: float = 3.0,
fastapi_docs: bool = False,
show: bool = True,
on_air: Optional[Union[str, Literal[True]]] = None,
native: bool = False,
Expand Down Expand Up @@ -63,6 +64,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 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)
Expand Down Expand Up @@ -104,6 +106,15 @@ def run(*,
if route.path == '/' or route.path in Client.page_routes.values():
route.include_in_schema = endpoint_documentation in {'page', 'all'}

if fastapi_docs:
if not core.app.docs_url:
core.app.docs_url = '/docs'
if not core.app.redoc_url:
core.app.redoc_url = '/redoc'
if not core.app.openapi_url:
core.app.openapi_url = '/openapi.json'
core.app.setup()

if on_air:
core.air = Air('' if on_air is True else on_air)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_endpoint_docs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from typing import Set

import pytest
import requests

from nicegui import __version__
from nicegui.testing import Screen


@pytest.fixture(autouse=True)
def activate_fastapi_docs(screen: Screen):
screen.ui_run_kwargs['fastapi_docs'] = True


def get_openapi_paths() -> Set[str]:
return set(requests.get(f'http://localhost:{Screen.PORT}/openapi.json', timeout=5).json()['paths'])

Expand Down

0 comments on commit c69db4d

Please sign in to comment.