Skip to content

Commit

Permalink
fixes #521
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Oct 16, 2024
1 parent 9fab6ee commit c3bc15e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 16 deletions.
3 changes: 2 additions & 1 deletion fasthtml/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,5 @@
'fasthtml.xtend.loose_format': ('api/xtend.html#loose_format', 'fasthtml/xtend.py'),
'fasthtml.xtend.replace_css_vars': ('api/xtend.html#replace_css_vars', 'fasthtml/xtend.py'),
'fasthtml.xtend.run_js': ('api/xtend.html#run_js', 'fasthtml/xtend.py'),
'fasthtml.xtend.undouble_braces': ('api/xtend.html#undouble_braces', 'fasthtml/xtend.py')}}}
'fasthtml.xtend.undouble_braces': ('api/xtend.html#undouble_braces', 'fasthtml/xtend.py'),
'fasthtml.xtend.with_sid': ('api/xtend.html#with_sid', 'fasthtml/xtend.py')}}}
4 changes: 2 additions & 2 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def flat_xt(lst):
for item in lst:
if isinstance(item, (list,tuple)): result.extend(item)
else: result.append(item)
return result
return tuple(result)

# %% ../nbs/api/00_core.ipynb
class Beforeware:
Expand Down Expand Up @@ -347,7 +347,7 @@ def _find_targets(req, resp):
def _apply_ft(o):
if isinstance(o, tuple): o = tuple(_apply_ft(c) for c in o)
if hasattr(o, '__ft__'): o = o.__ft__()
if isinstance(o, FT): o.children = [_apply_ft(c) for c in o.children]
if isinstance(o, FT): o.children = tuple(_apply_ft(c) for c in o.children)
return o

def _to_xml(req, resp, indent):
Expand Down
8 changes: 6 additions & 2 deletions fasthtml/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ def stop(self):
# %% ../nbs/api/06_jupyter.ipynb
# The script lets an iframe parent know of changes so that it can resize automatically.
_iframe_scr = Script("""
function sendmsg() {window.parent.postMessage({height: document.documentElement.offsetHeight}, '*')}
function sendmsg() {
window.parent.postMessage({height: document.documentElement.offsetHeight}, '*');
}
window.onload = function() {
sendmsg();
document.body.addEventListener('htmx:afterSettle', sendmsg);
document.body.addEventListener('htmx:afterSettle', sendmsg);
document.body.addEventListener('htmx:wsAfterMessage', sendmsg);
};""")

# %% ../nbs/api/06_jupyter.ipynb
Expand All @@ -93,6 +96,7 @@ def HTMX(path="", host='localhost', port=8000, iframe_height="auto"):
return HTML(f'<iframe src="http://{host}:{port}{str(path)}" style="width: 100%; height: {iframe_height}; border: none;" ' + """onload="{
let frame = this;
window.addEventListener('message', function(e) {
if (e.source !== frame.contentWindow) return; // Only proceed if the message is from this iframe
if (e.data.height) frame.style.height = (e.data.height+1) + 'px';
}, 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> """)
Expand Down
10 changes: 9 additions & 1 deletion fasthtml/xtend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# %% auto 0
__all__ = ['sid_scr', 'A', 'AX', 'Form', 'Hidden', 'CheckboxX', 'Script', 'Style', 'double_braces', 'undouble_braces',
'loose_format', 'ScriptX', 'replace_css_vars', 'StyleX', 'Nbsp', 'Surreal', 'On', 'Prev', 'Now', 'AnyNow',
'run_js', 'HtmxOn', 'jsd', 'Titled', 'Socials', 'Favicon', 'clear']
'run_js', 'HtmxOn', 'jsd', 'Titled', 'Socials', 'Favicon', 'clear', 'with_sid']

# %% ../nbs/api/02_xtend.ipynb
from dataclasses import dataclass, asdict
Expand All @@ -15,6 +15,7 @@
from fastcore.xtras import partial_format
from fastcore.xml import *
from fastcore.meta import use_kwargs, delegates
from .core import *
from .components import *

try: from IPython import display
Expand Down Expand Up @@ -232,3 +233,10 @@ def clear(id): return Div(hx_swap_oob='innerHTML', id=id)
}
});
''')

# %% ../nbs/api/02_xtend.ipynb
def with_sid(app, dest, path='/'):
id = unqid()
@app.route(path)
def get():
return Div(id=id, hx_get=dest, hx_trigger=f'load delay:0.001s', hx_target=f'#{id}', hx_swap='outerHTML')
16 changes: 8 additions & 8 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
{
"data": {
"text/plain": [
"datetime.datetime(2024, 10, 15, 14, 0)"
"datetime.datetime(2024, 10, 16, 14, 0)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -731,7 +731,7 @@
" for item in lst:\n",
" if isinstance(item, (list,tuple)): result.extend(item)\n",
" else: result.append(item)\n",
" return result"
" return tuple(result)"
]
},
{
Expand All @@ -742,8 +742,8 @@
"outputs": [],
"source": [
"x = ft('a',1)\n",
"test_eq(flat_xt([x, x, [x,x]]), [x]*4)\n",
"test_eq(flat_xt(x), [x])"
"test_eq(flat_xt([x, x, [x,x]]), (x,)*4)\n",
"test_eq(flat_xt(x), (x,))"
]
},
{
Expand Down Expand Up @@ -1017,7 +1017,7 @@
"def _apply_ft(o):\n",
" if isinstance(o, tuple): o = tuple(_apply_ft(c) for c in o)\n",
" if hasattr(o, '__ft__'): o = o.__ft__()\n",
" if isinstance(o, FT): o.children = [_apply_ft(c) for c in o.children]\n",
" if isinstance(o, FT): o.children = tuple(_apply_ft(c) for c in o.children)\n",
" return o\n",
"\n",
"def _to_xml(req, resp, indent):\n",
Expand Down Expand Up @@ -2243,13 +2243,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Set to 2024-10-11 09:46:57.944986\n"
"Set to 2024-10-16 09:46:44.561572\n"
]
},
{
"data": {
"text/plain": [
"'Session time: 2024-10-11 09:46:57.944986'"
"'Session time: 2024-10-16 09:46:44.561572'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2480,7 +2480,7 @@
{
"data": {
"text/plain": [
"'Cookie was set at time 09:46:58.348630'"
"'Cookie was set at time 09:46:45.017783'"
]
},
"execution_count": null,
Expand Down
16 changes: 16 additions & 0 deletions nbs/api/02_xtend.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"from fastcore.xtras import partial_format\n",
"from fastcore.xml import *\n",
"from fastcore.meta import use_kwargs, delegates\n",
"from fasthtml.core import *\n",
"from fasthtml.components import *\n",
"\n",
"try: from IPython import display\n",
Expand Down Expand Up @@ -589,6 +590,21 @@
"''')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "579e1f33",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def with_sid(app, dest, path='/'):\n",
" id = unqid()\n",
" @app.route(path)\n",
" def get():\n",
" return Div(id=id, hx_get=dest, hx_trigger=f'load delay:0.001s', hx_target=f'#{id}', hx_swap='outerHTML')"
]
},
{
"cell_type": "markdown",
"id": "474e14b4",
Expand Down
8 changes: 6 additions & 2 deletions nbs/api/06_jupyter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,13 @@
"#| export\n",
"# The script lets an iframe parent know of changes so that it can resize automatically. \n",
"_iframe_scr = Script(\"\"\"\n",
" function sendmsg() {window.parent.postMessage({height: document.documentElement.offsetHeight}, '*')}\n",
" function sendmsg() {\n",
" window.parent.postMessage({height: document.documentElement.offsetHeight}, '*');\n",
" }\n",
" window.onload = function() {\n",
" sendmsg();\n",
" document.body.addEventListener('htmx:afterSettle', sendmsg);\n",
" document.body.addEventListener('htmx:afterSettle', sendmsg);\n",
" document.body.addEventListener('htmx:wsAfterMessage', sendmsg);\n",
" };\"\"\")"
]
},
Expand Down Expand Up @@ -297,6 +300,7 @@
" return HTML(f'<iframe src=\"http://{host}:{port}{str(path)}\" style=\"width: 100%; height: {iframe_height}; border: none;\" ' + \"\"\"onload=\"{\n",
" let frame = this;\n",
" window.addEventListener('message', function(e) {\n",
" if (e.source !== frame.contentWindow) return; // Only proceed if the message is from this iframe\n",
" if (e.data.height) frame.style.height = (e.data.height+1) + 'px';\n",
" }, false);\n",
" }\" 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> \"\"\")"
Expand Down

0 comments on commit c3bc15e

Please sign in to comment.