Skip to content

Commit

Permalink
move ws_client
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Oct 16, 2024
1 parent e511601 commit 1b97dd8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
6 changes: 3 additions & 3 deletions fasthtml/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@
'fasthtml.core.signal_shutdown': ('api/core.html#signal_shutdown', 'fasthtml/core.py'),
'fasthtml.core.snake2hyphens': ('api/core.html#snake2hyphens', 'fasthtml/core.py'),
'fasthtml.core.unqid': ('api/core.html#unqid', 'fasthtml/core.py'),
'fasthtml.core.uri': ('api/core.html#uri', 'fasthtml/core.py'),
'fasthtml.core.ws_client': ('api/core.html#ws_client', 'fasthtml/core.py')},
'fasthtml.core.uri': ('api/core.html#uri', 'fasthtml/core.py')},
'fasthtml.fastapp': { 'fasthtml.fastapp._app_factory': ('api/fastapp.html#_app_factory', 'fasthtml/fastapp.py'),
'fasthtml.fastapp._get_tbl': ('api/fastapp.html#_get_tbl', 'fasthtml/fastapp.py'),
'fasthtml.fastapp.fast_app': ('api/fastapp.html#fast_app', 'fasthtml/fastapp.py')},
Expand All @@ -127,7 +126,8 @@
'fasthtml.jupyter.jupy_app': ('api/jupyter.html#jupy_app', 'fasthtml/jupyter.py'),
'fasthtml.jupyter.nb_serve': ('api/jupyter.html#nb_serve', 'fasthtml/jupyter.py'),
'fasthtml.jupyter.nb_serve_async': ('api/jupyter.html#nb_serve_async', 'fasthtml/jupyter.py'),
'fasthtml.jupyter.wait_port_free': ('api/jupyter.html#wait_port_free', 'fasthtml/jupyter.py')},
'fasthtml.jupyter.wait_port_free': ('api/jupyter.html#wait_port_free', 'fasthtml/jupyter.py'),
'fasthtml.jupyter.ws_client': ('api/jupyter.html#ws_client', 'fasthtml/jupyter.py')},
'fasthtml.live_reload': {},
'fasthtml.oauth': { 'fasthtml.oauth.DiscordAppClient': ('api/oauth.html#discordappclient', 'fasthtml/oauth.py'),
'fasthtml.oauth.DiscordAppClient.__init__': ( 'api/oauth.html#discordappclient.__init__',
Expand Down
17 changes: 1 addition & 16 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
'scopesrc', 'viewport', 'charset', 'all_meths', 'parsed_date', 'snake2hyphens', 'HtmxHeaders', 'HttpHeader',
'HtmxResponseHeaders', 'form2dict', 'parse_form', 'flat_xt', 'Beforeware', 'EventStream', 'signal_shutdown',
'WS_RouteX', 'uri', 'decode_uri', 'flat_tuple', 'Redirect', 'RouteX', 'RouterX', 'get_key', 'def_hdrs',
'FastHTML', 'serve', 'Client', 'cookie', 'reg_re_param', 'MiddlewareBase', 'FtResponse', 'unqid', 'setup_ws',
'ws_client']
'FastHTML', 'serve', 'Client', 'cookie', 'reg_re_param', 'MiddlewareBase', 'FtResponse', 'unqid', 'setup_ws']

# %% ../nbs/api/00_core.ipynb
import json,uuid,inspect,types,uvicorn,signal,asyncio,threading
Expand Down Expand Up @@ -701,17 +700,3 @@ async def send(s):
for o in conns.values(): await o(s)
app._send = send
return send

# %% ../nbs/api/00_core.ipynb
def ws_client(app, nm='', host='localhost', port=8000, ws_connect='/ws', frame=True, link=True, **kwargs):
path = f'/{nm}'
c = Container('', id=unqid())
@app.get(path)
def f():
return Div(c, id=nm or '_dest', hx_trigger='load',
hx_ext="ws", ws_connect=ws_connect, **kwargs)
if link: display(HTML(f'<a href="http://{host}:{port}{path}" target="_blank">open in browser</a>'))
if frame: display(HTMX(path, host=host, port=port))
def send(o): asyncio.create_task(app._send(o))
c.on(send)
return c
18 changes: 16 additions & 2 deletions fasthtml/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

# %% auto 0
__all__ = ['cors_allow', 'nb_serve', 'nb_serve_async', 'is_port_free', 'wait_port_free', 'JupyUvi', 'FastJupy', 'HTMX',
'jupy_app']
'ws_client', 'jupy_app']

# %% ../nbs/api/06_jupyter.ipynb
import asyncio, socket, time, uvicorn
from threading import Thread
from fastcore.utils import *
from .common import *
from IPython.display import HTML,Markdown,IFrame
from IPython.display import HTML,Markdown,IFrame,display
from starlette.middleware.cors import CORSMiddleware
from starlette.middleware import Middleware
from fastcore.parallel import startthread
Expand Down Expand Up @@ -101,6 +101,20 @@ def HTMX(path="", host='localhost', port=8000, iframe_height="auto"):
}, false);
}" allow="accelerometer; autoplay; camera; clipboard-read; clipboard-write; display-capture; encrypted-media; fullscreen; gamepad; geolocation; gyroscope; hid; identity-credentials-get; idle-detection; magnetometer; microphone; midi; payment; picture-in-picture; publickey-credentials-get; screen-wake-lock; serial; usb; web-share; xr-spatial-tracking"></iframe> """)

# %% ../nbs/api/06_jupyter.ipynb
def ws_client(app, nm='', host='localhost', port=8000, ws_connect='/ws', frame=True, link=True, **kwargs):
path = f'/{nm}'
c = Main('', cls="container", id=unqid())
@app.get(path)
def f():
return Div(c, id=nm or '_dest', hx_trigger='load',
hx_ext="ws", ws_connect=ws_connect, **kwargs)
if link: display(HTML(f'<a href="http://{host}:{port}{path}" target="_blank">open in browser</a>'))
if frame: display(HTMX(path, host=host, port=port))
def send(o): asyncio.create_task(app._send(o))
c.on(send)
return c

# %% ../nbs/api/06_jupyter.ipynb
def jupy_app(pico=False, hdrs=None, middleware=None, **kwargs):
"Same as `fast_app` but for Jupyter notebooks"
Expand Down
22 changes: 0 additions & 22 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2686,28 +2686,6 @@
" return send"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c193a6b7",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def ws_client(app, nm='', host='localhost', port=8000, ws_connect='/ws', frame=True, link=True, **kwargs):\n",
" path = f'/{nm}'\n",
" c = Container('', id=unqid())\n",
" @app.get(path)\n",
" def f():\n",
" return Div(c, id=nm or '_dest', hx_trigger='load',\n",
" hx_ext=\"ws\", ws_connect=ws_connect, **kwargs)\n",
" if link: display(HTML(f'<a href=\"http://{host}:{port}{path}\" target=\"_blank\">open in browser</a>'))\n",
" if frame: display(HTMX(path, host=host, port=port))\n",
" def send(o): asyncio.create_task(app._send(o))\n",
" c.on(send)\n",
" return c"
]
},
{
"cell_type": "markdown",
"id": "474e14b4",
Expand Down
24 changes: 23 additions & 1 deletion nbs/api/06_jupyter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"from threading import Thread\n",
"from fastcore.utils import *\n",
"from fasthtml.common import *\n",
"from IPython.display import HTML,Markdown,IFrame\n",
"from IPython.display import HTML,Markdown,IFrame,display\n",
"from starlette.middleware.cors import CORSMiddleware\n",
"from starlette.middleware import Middleware\n",
"from fastcore.parallel import startthread"
Expand Down Expand Up @@ -348,6 +348,28 @@
"server.stop()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "850155aa",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def ws_client(app, nm='', host='localhost', port=8000, ws_connect='/ws', frame=True, link=True, **kwargs):\n",
" path = f'/{nm}'\n",
" c = Main('', cls=\"container\", id=unqid())\n",
" @app.get(path)\n",
" def f():\n",
" return Div(c, id=nm or '_dest', hx_trigger='load',\n",
" hx_ext=\"ws\", ws_connect=ws_connect, **kwargs)\n",
" if link: display(HTML(f'<a href=\"http://{host}:{port}{path}\" target=\"_blank\">open in browser</a>'))\n",
" if frame: display(HTMX(path, host=host, port=port))\n",
" def send(o): asyncio.create_task(app._send(o))\n",
" c.on(send)\n",
" return c"
]
},
{
"cell_type": "markdown",
"id": "1ef8dfe3",
Expand Down

0 comments on commit 1b97dd8

Please sign in to comment.