python-fasthtml 0.14.22__tar.gz → 0.14.24__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. {python_fasthtml-0.14.22/python_fasthtml.egg-info → python_fasthtml-0.14.24}/PKG-INFO +1 -1
  2. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/__init__.py +1 -1
  3. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/jupyter.py +8 -2
  4. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/proxy.py +28 -23
  5. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24/python_fasthtml.egg-info}/PKG-INFO +1 -1
  6. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/tests/test_fh.py +4 -1
  7. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/CONTRIBUTING.md +0 -0
  8. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/LICENSE +0 -0
  9. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/MANIFEST.in +0 -0
  10. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/README.md +0 -0
  11. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/_modidx.py +0 -0
  12. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/authmw.py +0 -0
  13. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/basics.py +0 -0
  14. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/cli.py +0 -0
  15. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/common.py +0 -0
  16. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/components.py +0 -0
  17. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/components.pyi +0 -0
  18. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/core.py +0 -0
  19. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/core.pyi +0 -0
  20. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/fastapp.py +0 -0
  21. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/ft.py +0 -0
  22. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/js.py +0 -0
  23. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/katex.js +0 -0
  24. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/live_reload.py +0 -0
  25. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/oauth.py +0 -0
  26. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/pico.py +0 -0
  27. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/starlette.py +0 -0
  28. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/stripe_otp.py +0 -0
  29. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/svg.py +0 -0
  30. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/toaster.py +0 -0
  31. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/xtend.py +0 -0
  32. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/fasthtml/xtend.pyi +0 -0
  33. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/pyproject.toml +0 -0
  34. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/python_fasthtml.egg-info/SOURCES.txt +0 -0
  35. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/python_fasthtml.egg-info/dependency_links.txt +0 -0
  36. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/python_fasthtml.egg-info/entry_points.txt +0 -0
  37. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/python_fasthtml.egg-info/requires.txt +0 -0
  38. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/python_fasthtml.egg-info/top_level.txt +0 -0
  39. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/setup.cfg +0 -0
  40. {python_fasthtml-0.14.22 → python_fasthtml-0.14.24}/tests/test_proxy.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-fasthtml
3
- Version: 0.14.22
3
+ Version: 0.14.24
4
4
  Summary: The fastest way to create an HTML app
5
5
  Author-email: Jeremy Howard and contributors <github@jhoward.fastmail.fm>
6
6
  License-Expression: Apache-2.0
@@ -5,5 +5,5 @@ Modules:
5
5
  - `fasthtml.core`: The `FastHTML` subclass of `Starlette`.
6
6
  - `fasthtml.proxy`: Streaming HTTP responses and bidirectional WebSocket forwarding"""
7
7
 
8
- __version__ = "0.14.22"
8
+ __version__ = "0.14.24"
9
9
  from .core import *
@@ -32,8 +32,14 @@ def nb_serve(app, log_level="error", port=8000, host='0.0.0.0', daemon=False, **
32
32
  async def nb_serve_async(app, log_level="error", port=8000, host='0.0.0.0', **kwargs):
33
33
  "Async version of `nb_serve`"
34
34
  server = uvicorn.Server(uvicorn.Config(app, log_level=log_level, host=host, port=port, **kwargs))
35
- server._serve_task = asyncio.get_running_loop().create_task(server.serve())
36
- while not server.started: await asyncio.sleep(0.01)
35
+ async def serve():
36
+ try: await server.serve()
37
+ except SystemExit as e: raise RuntimeError(f'Uvicorn exited with status {e.code}') from e
38
+ server._serve_task = asyncio.get_running_loop().create_task(serve())
39
+ while not server.started and not server._serve_task.done(): await asyncio.sleep(0.01)
40
+ if not server.started:
41
+ await server._serve_task
42
+ raise RuntimeError('Server exited before startup completed')
37
43
  return server
38
44
 
39
45
  # %% ../nbs/api/06_jupyter.ipynb #654b36bb
@@ -6,7 +6,7 @@ Register proxy handlers with Starlette's `Route` and `WebSocketRoute`. FastHTML'
6
6
 
7
7
  Connection-nominated headers and explicitly excluded names are removed. Repeated end-to-end values remain separate, with lowercase names.
8
8
 
9
- `proxy_websocket` connects upstream before accepting the caller. It forwards the selected subprotocol and relays text and binary frames. Either peer disconnecting cancels the other forwarding task and closes the upstream connection. Supply a complete URL, including any query parameters.
9
+ `proxy_websocket` connects upstream before accepting the caller. Upstream HTTP 401, 403 and 404 responses accept and close the caller with code 1008. It forwards the selected subprotocol and relays text and binary frames. Either peer disconnecting cancels the other forwarding task and closes the upstream connection. Supply a complete URL, including any query parameters.
10
10
 
11
11
  The relay reads raw ASGI events from `receive()`. A `websocket.receive` event carries `text` or `bytes`. A `websocket.disconnect` event carries a close `code`. `receive_text()` and `receive_bytes()` extract one data field instead.
12
12
 
@@ -75,26 +75,31 @@ async def proxy_websocket(
75
75
  headers=None, # Additional upstream handshake headers
76
76
  ):
77
77
  "Relay text and binary frames until either peer disconnects."
78
- async with websockets.connect(url, additional_headers=headers,
79
- subprotocols=websocket.scope['subprotocols'], max_size=None) as target:
80
- await websocket.accept(subprotocol=target.subprotocol)
81
- async def to_target():
82
- while True:
83
- message = await websocket.receive()
84
- if message['type']=='websocket.disconnect': return
85
- await target.send(message['text'] if 'text' in message else message['bytes'])
86
- async def to_client():
87
- async for message in target:
88
- if isinstance(message, str): await websocket.send_text(message)
89
- else: await websocket.send_bytes(message)
90
- tasks = [asyncio.create_task(to_target()), asyncio.create_task(to_client())]
91
- try:
92
- done,_ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
93
- for task in done: task.result()
94
- finally:
95
- for task in tasks: task.cancel()
96
- await asyncio.gather(*tasks, return_exceptions=True)
97
- if websocket.client_state.name!='DISCONNECTED':
98
- code = target.close_code if target.close_code not in (None, 1005, 1006, 1015) else 1011
99
- await websocket.close(code=code, reason=target.close_reason or '')
78
+ try:
79
+ async with websockets.connect(url, additional_headers=headers,
80
+ subprotocols=websocket.scope['subprotocols'] or None, max_size=None) as target:
81
+ await websocket.accept(subprotocol=target.subprotocol)
82
+ async def to_target():
83
+ while True:
84
+ message = await websocket.receive()
85
+ if message['type']=='websocket.disconnect': return
86
+ await target.send(message['text'] if 'text' in message else message['bytes'])
87
+ async def to_client():
88
+ async for message in target:
89
+ if isinstance(message, str): await websocket.send_text(message)
90
+ else: await websocket.send_bytes(message)
91
+ tasks = [asyncio.create_task(to_target()), asyncio.create_task(to_client())]
92
+ try:
93
+ done,_ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
94
+ for task in done: task.result()
95
+ finally:
96
+ for task in tasks: task.cancel()
97
+ await asyncio.gather(*tasks, return_exceptions=True)
98
+ if websocket.client_state.name!='DISCONNECTED':
99
+ code = target.close_code if target.close_code not in (None, 1005, 1006, 1015) else 1011
100
+ await websocket.close(code=code, reason=target.close_reason or '')
101
+ except websockets.exceptions.InvalidStatus as e:
102
+ if e.response.status_code not in (401, 403, 404): raise
103
+ await websocket.accept()
104
+ await websocket.close(code=1008, reason=f'Upstream refused connection ({e.response.status_code})')
100
105
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-fasthtml
3
- Version: 0.14.22
3
+ Version: 0.14.24
4
4
  Summary: The fastest way to create an HTML app
5
5
  Author-email: Jeremy Howard and contributors <github@jhoward.fastmail.fm>
6
6
  License-Expression: Apache-2.0
@@ -69,6 +69,7 @@ async def echo_http(request):
69
69
  return response
70
70
 
71
71
  async def echo_ws(ws):
72
+ if status := ws.query_params.get('deny'): return await ws.send_denial_response(Response(status_code=int(status)))
72
73
  await ws.accept(subprotocol='echo')
73
74
  await ws.send_text(await ws.receive_text())
74
75
  await ws.send_bytes(await ws.receive_bytes())
@@ -79,7 +80,7 @@ def test_proxy():
79
80
  with running_server(upstream) as url:
80
81
  async def forward_http(request):
81
82
  return ProxyResponse(request, url, headers=proxy_headers(request.headers, exclude=(b'authorization',)))
82
- async def forward_ws(ws): await proxy_websocket(ws, url.replace('http', 'ws')+'/ws')
83
+ async def forward_ws(ws): await proxy_websocket(ws, url.replace('http', 'ws')+'/ws?'+ws.url.query)
83
84
  app = Starlette(routes=[Route('/', forward_http, methods=['PATCH']), WebSocketRoute('/ws', forward_ws)])
84
85
  with TestClient(app) as proxy:
85
86
  body = b'["a", {"b":2}]'
@@ -97,3 +98,5 @@ def test_proxy():
97
98
  ws.send_bytes(b'\x00bytes')
98
99
  assert ws.receive_bytes() == b'\x00bytes'
99
100
  assert ws.receive()['code'] == 1000
101
+ for status in (401, 403, 404):
102
+ with proxy.websocket_connect(f'/ws?deny={status}') as ws: assert ws.receive()['code'] == 1008