Skip to content

Commit d6ec2f4

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

File tree

14 files changed

+183
-35
lines changed

14 files changed

+183
-35
lines changed

fps/app.py

Lines changed: 17 additions & 34 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from fps_dashboard._version import __version__ # noqa
Lines changed: 2 additions & 0 deletions
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))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .dashboard import Dashboard
2+
3+
4+
def app():
5+
Dashboard.run(title="API Summary")
Lines changed: 10 additions & 0 deletions
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")
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import asyncio
2+
import atexit
3+
import json
4+
import sys
5+
6+
from rich.table import Table
7+
from textual import events
8+
from textual.app import App
9+
from textual.widgets import ScrollView
10+
11+
FPS = None
12+
13+
14+
def stop_fps():
15+
if FPS is not None:
16+
FPS.terminate()
17+
18+
19+
atexit.register(stop_fps)
20+
21+
22+
async def show(queue: asyncio.Queue):
23+
24+
console = Console()
25+
with console.capture() as capture:
26+
console.print()
27+
console.print(table)
28+
29+
str_output = capture.get()
30+
print(str_output)
31+
32+
33+
class Dashboard(App):
34+
"""An example of a very simple Textual App"""
35+
36+
async def on_load(self, event: events.Load) -> None:
37+
await self.bind("q", "quit", "Quit")
38+
39+
async def on_mount(self, event: events.Mount) -> None:
40+
41+
self.body = body = ScrollView(auto_width=True)
42+
43+
await self.view.dock(body)
44+
45+
async def add_content():
46+
global FPS
47+
cmd = ["fps-uvicorn", "--fps.show_endpoints"] + sys.argv[1:]
48+
FPS = await asyncio.create_subprocess_exec(
49+
*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
50+
)
51+
queue = asyncio.Queue()
52+
asyncio.create_task(get_log(queue))
53+
endpoint_marker = "ENDPOINT:"
54+
endpoints = []
55+
get_endpoint = False
56+
while True:
57+
line = await queue.get()
58+
if endpoint_marker in line:
59+
get_endpoint = True
60+
elif get_endpoint:
61+
break
62+
if get_endpoint:
63+
i = line.find(endpoint_marker) + len(endpoint_marker)
64+
line = line[i:].strip()
65+
if not line:
66+
break
67+
endpoint = json.loads(line)
68+
endpoints.append(endpoint)
69+
70+
table = Table(title="API Summary")
71+
table.add_column("Path", justify="left", style="cyan", no_wrap=True)
72+
table.add_column("Methods", justify="right", style="green")
73+
table.add_column("Plugin", style="magenta")
74+
75+
for endpoint in endpoints:
76+
path = endpoint["path"]
77+
methods = ", ".join(endpoint["methods"])
78+
plugin = ", ".join(endpoint["plugin"])
79+
if "WEBSOCKET" in methods:
80+
path = f"[cyan on red]{path}[/]"
81+
table.add_row(path, methods, plugin)
82+
83+
await body.update(table)
84+
85+
await self.call_later(add_content)
86+
87+
88+
async def get_log(queue):
89+
while True:
90+
line = await FPS.stderr.readline()
91+
if line:
92+
await queue.put(line.decode().strip())
93+
else:
94+
break

plugins/dashboard/setup.cfg

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
textual
26+
27+
[options.entry_points]
28+
fps_config =
29+
fps_dashboard_config = fps_dashboard.config
30+
31+
console_scripts =
32+
fps-dashboard = fps_dashboard.cli:app

plugins/dashboard/setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import setuptools
2+
3+
setuptools.setup()

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ install_requires =
2424
fastapi
2525
pluggy>=1.0,<2.0
2626
click
27-
rich
2827

2928
[options.extras_require]
3029
uvicorn =

0 commit comments

Comments
 (0)