feat: expose Jupyter shell through web app

This commit is contained in:
lda
2026-08-30 19:19:49 +07:00 Verified
parent a77f146a72
commit 6315235e16
11 changed files with 220 additions and 9 deletions
+61
View File
@@ -0,0 +1,61 @@
import json
import unittest
import httpx
from ipython_webapp.jupyter.app import (
InputReply,
app,
reply_to_input,
run_code,
shell_app,
)
class JupyterWebAppTests(unittest.IsolatedAsyncioTestCase):
async def test_run_endpoint_streams_json_events_with_mime_data(self):
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(
transport=transport,
base_url="http://testserver",
) as client:
try:
response = await client.post(
"/shells/new/run",
content="{'answer': 42}",
headers={"content-type": "text/plain"},
)
finally:
await shell_app.shutdown()
self.assertEqual(response.status_code, 200)
self.assertTrue(
response.headers["content-type"].startswith("application/x-ndjson")
)
events = [json.loads(line) for line in response.text.splitlines()]
result = next(event for event in events if event["msg_type"] == "execute_result")
self.assertEqual(result["content"]["data"]["text/plain"], "{'answer': 42}")
async def test_input_request_can_be_replied_to_while_run_streams(self):
try:
response = await run_code("new", "answer = input('name? '); answer")
self.assertEqual(response.media_type, "application/x-ndjson")
events = []
async for line in response.body_iterator:
event = json.loads(line)
events.append(event)
if event["type"] == "input_request":
reply = await reply_to_input(
event["call_id"],
InputReply(value="Ada"),
)
self.assertEqual(reply.status_code, 204)
finally:
await shell_app.shutdown()
result = next(event for event in events if event.get("msg_type") == "execute_result")
self.assertEqual(result["content"]["data"]["text/plain"], "'Ada'")
if __name__ == "__main__":
unittest.main()