Skip to content

Commit de99a02

Browse files
committed
Create dashboard plugin
1 parent 41a3ed5 commit de99a02

File tree

13 files changed

+160
-34
lines changed

13 files changed

+160
-34
lines changed

fps/app.py

+17-34
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
from types import ModuleType
34
from typing import Callable, Dict, List
@@ -6,8 +7,6 @@
67
from fastapi import FastAPI
78
from fastapi.routing import APIWebSocketRoute
89
from pluggy import PluginManager
9-
from rich.console import Console
10-
from rich.table import Table
1110
from starlette.routing import Mount
1211

1312
from fps import hooks
@@ -156,7 +155,7 @@ def _load_configurations() -> None:
156155
logger.info("No plugin configuration to load")
157156

158157

159-
def _load_routers(app: FastAPI) -> Dict[str, APIWebSocketRoute]:
158+
def _load_routers(app: FastAPI) -> None:
160159

161160
pm = _get_pluggin_manager(HookType.ROUTER)
162161

@@ -281,34 +280,20 @@ def _load_routers(app: FastAPI) -> Dict[str, APIWebSocketRoute]:
281280
else:
282281
logger.info("No plugin API router to load")
283282

284-
return ws_routes
285-
286-
287-
def show_endpoints(app: FastAPI, ws_routes: Dict[str, APIWebSocketRoute]):
288-
table = Table(title="API Summary")
289-
table.add_column("Path", justify="left", style="cyan", no_wrap=True)
290-
table.add_column("Methods", justify="right", style="green")
291-
table.add_column("Plugin", style="magenta")
292-
293-
# HTTP endpoints
294-
openapi = app.openapi()
295-
for k, v in openapi["paths"].items():
296-
path = k
297-
methods = ", ".join([method.upper() for method in v.keys()])
298-
plugin = ", ".join({i["tags"][0] for i in v.values()})
299-
table.add_row(path, methods, plugin)
300-
301-
# websockets endpoints
302-
for plugin, ws_route in ws_routes.items():
303-
table.add_row(f"[cyan on red]{ws_route.path}[/]", "WEBSOCKET", plugin)
304-
305-
console = Console()
306-
with console.capture() as capture:
307-
console.print()
308-
console.print(table)
309-
310-
str_output = capture.get()
311-
logger.info(str_output)
283+
fps_config = Config(FPSConfig)
284+
if fps_config.show_endpoints:
285+
openapi = app.openapi()
286+
logger.info("")
287+
for k, v in openapi["paths"].items():
288+
path = k
289+
methods = [method.upper() for method in v.keys()]
290+
plugin = list({i["tags"][0] for i in v.values()})
291+
o = {"path": path, "methods": methods, "plugin": plugin}
292+
logger.info(f"ENDPOINT: {json.dumps(o)}")
293+
for plugin, ws_route in ws_routes.items():
294+
o = {"path": ws_route.path, "methods": ["WEBSOCKET"], "plugin": [plugin]}
295+
logger.info(f"ENDPOINT: {json.dumps(o)}")
296+
logger.info("")
312297

313298

314299
def create_app():
@@ -321,11 +306,9 @@ def create_app():
321306
fps_config = Config(FPSConfig)
322307
app = FastAPI(**fps_config.__dict__)
323308

324-
ws_routes = _load_routers(app)
309+
_load_routers(app)
325310
_load_exceptions_handlers(app)
326311

327312
Config.check_not_used_sections()
328313

329-
show_endpoints(app, ws_routes)
330-
331314
return app

fps/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class FPSConfig(BaseModel):
3434
# plugins
3535
enabled_plugins: List[str] = []
3636
disabled_plugins: List[str] = []
37+
show_endpoints: bool = False
3738

3839
@validator("enabled_plugins", "disabled_plugins")
3940
def plugins_format(cls, plugins):

plugins/dashboard/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0.0.1 (February 15, 2022)
2+
=========================
3+
4+
Initial release

plugins/dashboard/LICENSE

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright 2021 David Brochart and the FPS contributors.
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
7+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
9+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10+
11+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

plugins/dashboard/MANIFEST.in

Whitespace-only changes.

plugins/dashboard/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# FPS Dashboard
2+
3+
An [FPS](https://github.com/jupyter-server/fps) plugin to show a dashboard using Rich/Textual.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from fps_dashboard._version import __version__ # noqa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version_info = (0, 0, 1)
2+
__version__ = ".".join(map(str, version_info))
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
import atexit
3+
4+
from .dashboard import show
5+
6+
FPS = None
7+
8+
9+
def stop_fps():
10+
FPS.terminate()
11+
12+
13+
atexit.register(stop_fps)
14+
15+
16+
async def main():
17+
global FPS
18+
cmd = ["fps-uvicorn", "--fps.show_endpoints"]
19+
FPS = await asyncio.create_subprocess_exec(
20+
*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
21+
)
22+
queue = asyncio.Queue()
23+
asyncio.create_task(show(queue))
24+
while True:
25+
line = await FPS.stderr.readline()
26+
if line:
27+
await queue.put(line.decode().strip())
28+
else:
29+
break
30+
31+
32+
def app():
33+
asyncio.run(main())
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from fps.config import PluginModel
2+
from fps.hooks import register_config, register_plugin_name
3+
4+
5+
class DashboardConfig(PluginModel):
6+
foo: bool = False
7+
8+
9+
c = register_config(DashboardConfig)
10+
n = register_plugin_name("dashboard")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import json
2+
3+
from rich.console import Console
4+
from rich.table import Table
5+
6+
7+
async def show(queue):
8+
endpoint_marker = "ENDPOINT:"
9+
endpoints = []
10+
get_endpoint = False
11+
while True:
12+
line = await queue.get()
13+
if endpoint_marker in line:
14+
get_endpoint = True
15+
elif get_endpoint:
16+
break
17+
if get_endpoint:
18+
i = line.find(endpoint_marker) + len(endpoint_marker)
19+
line = line[i:].strip()
20+
if not line:
21+
break
22+
endpoint = json.loads(line)
23+
endpoints.append(endpoint)
24+
25+
table = Table(title="API Summary")
26+
table.add_column("Path", justify="left", style="cyan", no_wrap=True)
27+
table.add_column("Methods", justify="right", style="green")
28+
table.add_column("Plugin", style="magenta")
29+
30+
for endpoint in endpoints:
31+
path = endpoint["path"]
32+
methods = ", ".join(endpoint["methods"])
33+
plugin = ", ".join(endpoint["plugin"])
34+
if "WEBSOCKET" in methods:
35+
path = f"[cyan on red]{path}[/]"
36+
table.add_row(path, methods, plugin)
37+
38+
console = Console()
39+
with console.capture() as capture:
40+
console.print()
41+
console.print(table)
42+
43+
str_output = capture.get()
44+
print(str_output)

plugins/dashboard/setup.cfg

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[metadata]
2+
name = fps_dashboard
3+
version = attr: fps_dashboard._version.__version__
4+
description = A dashboard plugin for FPS
5+
long_description = file: README.md
6+
long_description_content_type = text/markdown
7+
license_file = LICENSE
8+
author = David Brochart
9+
author_email = [email protected]
10+
url = https://github.com/jupyter-server/fps
11+
platforms = Windows, Linux, Mac OS X
12+
keywords = server, fastapi, pluggy, plugins, fps, rich
13+
14+
[bdist_wheel]
15+
universal = 1
16+
17+
[options]
18+
include_package_data = True
19+
packages = find:
20+
python_requires = >=3.7
21+
22+
install_requires =
23+
fps-uvicorn
24+
rich
25+
26+
[options.entry_points]
27+
fps_config =
28+
fps_dashboard_config = fps_dashboard.config
29+
30+
console_scripts =
31+
fps-dashboard = fps_dashboard.cli:app

plugins/dashboard/setup.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import setuptools
2+
3+
setuptools.setup()

0 commit comments

Comments
 (0)