Skip to content

Commit

Permalink
fixes #519
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Oct 15, 2024
1 parent d23a649 commit 31fdb75
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 56 deletions.
2 changes: 1 addition & 1 deletion fasthtml/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from fastcore.xml import *
from fastcore.meta import use_kwargs, delegates
from fastcore.test import *
from .core import fh_cfg
from .core import fh_cfg, unqid

import types, json

Expand Down
6 changes: 4 additions & 2 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ async def _find_p(req, arg:str, p:Parameter):
if anno is empty:
if 'request'.startswith(arg.lower()): return req
if 'session'.startswith(arg.lower()): return req.scope.get('session', {})
if arg.lower()=='scope': return dict2obj(req.scope)
if arg.lower()=='auth': return req.scope.get('auth', None)
if arg.lower()=='htmx': return _get_htmx(req.headers)
if arg.lower()=='app': return req.scope['app']
Expand Down Expand Up @@ -229,6 +230,7 @@ def _find_wsp(ws, data, hdrs, arg:str, p:Parameter):
if issubclass(anno, Starlette): return ws.scope['app']
if anno is empty:
if arg.lower()=='ws': return ws
if arg.lower()=='scope': return dict2obj(ws.scope)
if arg.lower()=='data': return data
if arg.lower()=='htmx': return _get_htmx(hdrs)
if arg.lower()=='app': return ws.scope['app']
Expand Down Expand Up @@ -554,8 +556,8 @@ def _not_found(req, exc): return Response('404 Not Found', status_code=404)

def ws(self, path:str, conn=None, disconn=None, name=None):
"Add a websocket route at `path`"
def f(func):
self.router.add_ws(path, func, conn=conn, disconn=disconn, name=name)
def f(func=None):
self.router.add_ws(path, func or noop, conn=conn, disconn=disconn, name=name)
return func
return f

Expand Down
4 changes: 2 additions & 2 deletions fasthtml/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
for o in _all_: g[o] = partial(ft_hx, o[0].lower() + o[1:])

# %% ../nbs/api/05_svg.ipynb
def Svg(*args, viewBox=None, h=None, w=None, height=None, width=None, **kwargs):
def Svg(*args, viewBox=None, h=None, w=None, height=None, width=None, xmlns="http://www.w3.org/2000/svg", **kwargs):
"An SVG tag; xmlns is added automatically, and viewBox defaults to height and width if not provided"
if h: height=h
if w: width=w
if not viewBox and height and width: viewBox=f'0 0 {width} {height}'
return ft_svg('svg', *args, xmlns="http://www.w3.org/2000/svg", viewBox=viewBox, height=height, width=width, **kwargs)
return ft_svg('svg', *args, xmlns=xmlns, viewBox=viewBox, height=height, width=width, **kwargs)

# %% ../nbs/api/05_svg.ipynb
@delegates(ft_hx)
Expand Down
20 changes: 6 additions & 14 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, 11, 14, 0)"
"datetime.datetime(2024, 10, 15, 14, 0)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -588,7 +588,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ce8805c5",
"id": "9246153f",
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -631,6 +631,7 @@
" if anno is empty:\n",
" if 'request'.startswith(arg.lower()): return req\n",
" if 'session'.startswith(arg.lower()): return req.scope.get('session', {})\n",
" if arg.lower()=='scope': return dict2obj(req.scope)\n",
" if arg.lower()=='auth': return req.scope.get('auth', None)\n",
" if arg.lower()=='htmx': return _get_htmx(req.headers)\n",
" if arg.lower()=='app': return req.scope['app']\n",
Expand Down Expand Up @@ -793,6 +794,7 @@
" if issubclass(anno, Starlette): return ws.scope['app']\n",
" if anno is empty:\n",
" if arg.lower()=='ws': return ws\n",
" if arg.lower()=='scope': return dict2obj(ws.scope)\n",
" if arg.lower()=='data': return data\n",
" if arg.lower()=='htmx': return _get_htmx(hdrs)\n",
" if arg.lower()=='app': return ws.scope['app']\n",
Expand Down Expand Up @@ -1381,8 +1383,8 @@
"\n",
" def ws(self, path:str, conn=None, disconn=None, name=None):\n",
" \"Add a websocket route at `path`\"\n",
" def f(func):\n",
" self.router.add_ws(path, func, conn=conn, disconn=disconn, name=name)\n",
" def f(func=None):\n",
" self.router.add_ws(path, func or noop, conn=conn, disconn=disconn, name=name)\n",
" return func\n",
" return f"
]
Expand Down Expand Up @@ -1415,16 +1417,6 @@
" return f(path) if callable(path) else f"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a82b103f",
"metadata": {},
"outputs": [],
"source": [
"%ai reset"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
2 changes: 1 addition & 1 deletion nbs/api/01_components.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"from fastcore.xml import *\n",
"from fastcore.meta import use_kwargs, delegates\n",
"from fastcore.test import *\n",
"from fasthtml.core import fh_cfg\n",
"from fasthtml.core import fh_cfg, unqid\n",
"\n",
"import types, json\n",
"\n",
Expand Down
67 changes: 31 additions & 36 deletions nbs/api/05_svg.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,11 @@
"\n",
"To create and modify SVGs using a Python API, use the FT elements in `fasthtml.svg`, discussed below.\n",
"\n",
"**Note**: `fasthtml.common` does NOT automatically export SVG elements. To get access to them, you need to import `fasthtml.svg` like so\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fasthtml.svg import *"
"**Note**: `fasthtml.common` does NOT automatically export SVG elements. To get access to them, you need to import `fasthtml.svg` like so\n",
"\n",
"```python\n",
"from fasthtml.svg import *\n",
"```"
]
},
{
Expand All @@ -120,12 +115,12 @@
"outputs": [],
"source": [
"#| export\n",
"def Svg(*args, viewBox=None, h=None, w=None, height=None, width=None, **kwargs):\n",
"def Svg(*args, viewBox=None, h=None, w=None, height=None, width=None, xmlns=\"http://www.w3.org/2000/svg\", **kwargs):\n",
" \"An SVG tag; xmlns is added automatically, and viewBox defaults to height and width if not provided\"\n",
" if h: height=h\n",
" if w: width=w\n",
" if not viewBox and height and width: viewBox=f'0 0 {width} {height}'\n",
" return ft_svg('svg', *args, xmlns=\"http://www.w3.org/2000/svg\", viewBox=viewBox, height=height, width=width, **kwargs)"
" return ft_svg('svg', *args, xmlns=xmlns, viewBox=viewBox, height=height, width=width, **kwargs)"
]
},
{
Expand Down Expand Up @@ -657,7 +652,7 @@
{
"data": {
"text/html": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewbox=\"0 0 50 50\" height=\"50\" width=\"50\"><path fill=\"none\" stroke=\"purple\" stroke-width=\"2\" d=\"M10 10 L40 10 L40 40 L10 40 Z\"></path></svg>"
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewbox=\"0 0 50 50\" height=\"50\" width=\"50\"><path d=\" M10 10 L40 10 L40 40 L10 40 Z\" fill=\"none\" stroke=\"purple\" stroke-width=\"2\"></path></svg>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
Expand Down Expand Up @@ -688,7 +683,7 @@
{
"data": {
"text/html": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewbox=\"0 0 100 50\" height=\"50\" width=\"100\"><path fill=\"none\" stroke=\"purple\" stroke-width=\"2\" d=\"M25 25 C25 25 20 20 30 20 C40 20 40 30 30 30 C20 30 20 15 35 15 C50 15 50 35 25 35 C0 35 0 10 40 10 C80 10 80 40 25 40\"></path></svg>"
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewbox=\"0 0 100 50\" height=\"50\" width=\"100\"><path d=\" M25 25 C25 25 20 20 30 20 C40 20 40 30 30 30 C20 30 20 15 35 15 C50 15 50 35 25 35 C0 35 0 10 40 10 C80 10 80 40 25 40\" fill=\"none\" stroke=\"purple\" stroke-width=\"2\"></path></svg>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
Expand Down Expand Up @@ -726,7 +721,7 @@
{
"data": {
"text/html": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewbox=\"0 0 50 50\" height=\"50\" width=\"50\"><path fill=\"red\" d=\"M25 45 C25 45 10 35 10 25 A15 15 0 1 1 40 25 C40 35 25 45 25 45 Z\"></path></svg>"
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewbox=\"0 0 50 50\" height=\"50\" width=\"50\"><path d=\" M25 45 C25 45 10 35 10 25 A15 15 0 1 1 40 25 C40 35 25 45 25 45 Z\" fill=\"red\"></path></svg>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
Expand Down Expand Up @@ -763,7 +758,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"M25 45 C25 45 10 35 10 25 A15 15 0 1 1 40 25 C40 35 25 45 25 45 Z\n"
" M25 45 C25 45 10 35 10 25 A15 15 0 1 1 40 25 C40 35 25 45 25 45 Z\n"
]
}
],
Expand Down Expand Up @@ -804,7 +799,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L86){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L117){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.M\n",
"\n",
Expand All @@ -815,7 +810,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L86){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L117){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.M\n",
"\n",
Expand Down Expand Up @@ -843,7 +838,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L90){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L121){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.L\n",
"\n",
Expand All @@ -854,7 +849,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L90){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L121){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.L\n",
"\n",
Expand Down Expand Up @@ -882,7 +877,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L94){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L125){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.H\n",
"\n",
Expand All @@ -893,7 +888,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L94){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L125){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.H\n",
"\n",
Expand Down Expand Up @@ -921,7 +916,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L98){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L129){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.V\n",
"\n",
Expand All @@ -932,7 +927,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L98){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L129){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.V\n",
"\n",
Expand Down Expand Up @@ -960,7 +955,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L102){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L133){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.Z\n",
"\n",
Expand All @@ -971,7 +966,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L102){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L133){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.Z\n",
"\n",
Expand Down Expand Up @@ -999,7 +994,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L106){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L137){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.C\n",
"\n",
Expand All @@ -1010,7 +1005,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L106){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L137){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.C\n",
"\n",
Expand Down Expand Up @@ -1038,7 +1033,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L110){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L141){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.S\n",
"\n",
Expand All @@ -1049,7 +1044,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L110){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L141){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.S\n",
"\n",
Expand Down Expand Up @@ -1077,7 +1072,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L114){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L145){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.Q\n",
"\n",
Expand All @@ -1088,7 +1083,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L114){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L145){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.Q\n",
"\n",
Expand Down Expand Up @@ -1116,7 +1111,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L118){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L149){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.T\n",
"\n",
Expand All @@ -1127,7 +1122,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L118){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L149){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.T\n",
"\n",
Expand Down Expand Up @@ -1155,7 +1150,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L122){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L153){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.A\n",
"\n",
Expand All @@ -1166,7 +1161,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L122){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/fasthtml/blob/main/fasthtml/svg.py#L153){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### PathFT.A\n",
"\n",
Expand Down

0 comments on commit 31fdb75

Please sign in to comment.