Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to return json on websocket #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions gpustat_web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sys
import traceback
import urllib
import json

import asyncio
import asyncssh
Expand Down Expand Up @@ -161,6 +162,18 @@ def render_gpustat_body():
body += status
return ansi_conv.convert(body, full=False)

def render_gpustat_body_json():
body = []
for host, status in context.host_status.items():
if not status:
continue
try:
parsed = json.loads(status)
except:
continue
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please handle exception properly.

body.append(parsed)
return json.dumps(body)


async def handler(request):
'''Renders the html page.'''
Expand All @@ -184,6 +197,10 @@ async def websocket_handler(request):
async def _handle_websocketmessage(msg):
if msg.data == 'close':
await ws.close()
elif msg.data == 'json':
# send json formatted string as a websocket message.
body = render_gpustat_body_json()
await ws.send_str(body)
else:
# send the rendered HTML body as a websocket message.
body = render_gpustat_body()
Expand Down