-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6f6529d
Showing
24 changed files
with
7,229 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Sitemap: https://AnswerDotAI.github.io/fasthtml/sitemap.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
[ | ||
{ | ||
"objectID": "index.html", | ||
"href": "index.html", | ||
"title": "fasthtml", | ||
"section": "", | ||
"text": "fasthtml is a library for writing fast and scalable Starlette-powered web applications, without having to learn much (if any!) Starlette. Instead, you just use plain python functions for each page in your app – you don’t even need to learn Javascript. The finished app will be about as fast as a Python web server can be (which is pretty fast – e.g. Instagram runs on Python), and you can create pretty much anything. This isn’t one of those stripped-down dashboard making thingies.\nThis is a way to write real web applications, without the fuss.\nTo learn how to use it, please visit the documentation.", | ||
"crumbs": [ | ||
"fasthtml" | ||
] | ||
}, | ||
{ | ||
"objectID": "index.html#install", | ||
"href": "index.html#install", | ||
"title": "fasthtml", | ||
"section": "Install", | ||
"text": "Install\npip install python-fasthtml", | ||
"crumbs": [ | ||
"fasthtml" | ||
] | ||
}, | ||
{ | ||
"objectID": "index.html#how-to-use", | ||
"href": "index.html#how-to-use", | ||
"title": "fasthtml", | ||
"section": "How to use", | ||
"text": "How to use\nImport fasthtml, and you’ll probably want the widgets from fastcore.xml too.\n\nfrom fasthtml import *\nfrom fastcore.xml import *\n\nCreate your app.\n\napp = FastHTML()\n\nCreate your routes. The syntax is largely the same as the wonderful FastAPI (which is what you should be using instead of this if you’re creating a JSON service. FastHTML is for mainly for making HTML web apps, not APIs).\nNote that you need to include the types of your parameters, so that FastHTML knows what to pass to your function. Here, we’re just expecting a string:\n\[email protected]('/user/{nm}')\ndef get_nm(nm:str): return f\"Good day to you, {nm}!\"\n\nNormally you’d save this into a file such as main.py, and then run it in uvicorn using:\nuvicorn main:app\nHowever, for testing, we can use Starlette’s TestClient to try it out:\n\nfrom starlette.testclient import TestClient\n\n\nclient = TestClient(app)\nr = client.get('/user/Jeremy')\nr\n\n<Response [200 OK]>\n\n\nTestClient uses httpx behind the scenes, so it returns a httpx.Response, which has a text attribute with our response body:\n\nr.text\n\n'Good day to you, Jeremy!'\n\n\nFastHTML has special handling of tags created using fastcore.xml, so you can return web pages without worrying about Jinja, templates, or any of that stuff. This also means you can pip install styled rich component libraries, since it’s all just pure python:\n\[email protected]('/html/{idx}')\nasync def get_html(idx:int):\n return Body(\n H4(\"Wow look here\"),\n P(f'It looks like you are visitor {idx}! Next is {idx+1}.')\n )\n\n\nfrom IPython import display\n\n\ndisplay.HTML(client.get('/html/1').text)\n\n\n \nWow look here\n \n \nIt looks like you are visitor 1! Next is 2.", | ||
"crumbs": [ | ||
"fasthtml" | ||
] | ||
}, | ||
{ | ||
"objectID": "index.html#features", | ||
"href": "index.html#features", | ||
"title": "fasthtml", | ||
"section": "Features", | ||
"text": "Features\nHere’s a brief demo of all the features of the library:\n\nfrom starlette.responses import Response\nfrom datetime import datetime\nfrom fastcore.utils import *\nfrom dataclasses import dataclass, asdict\n\n\ndef todict(req): return {k:str(v) for k,v in req.items()}\n\n\napp = FastHTML()\n\[email protected](\"/\")\ndef root(req): return todict(req.scope)\n\[email protected]('/user/{nm}')\ndef get_nm(nm:str): return f\"Good day to you, {nm}!\"\n\n\nclient = TestClient(app)\nr = client.get('/')\nprint(r.text)\n\n{\"type\":\"http\",\"http_version\":\"1.1\",\"method\":\"GET\",\"path\":\"/\",\"raw_path\":\"b'/'\",\"root_path\":\"\",\"scheme\":\"http\",\"query_string\":\"b''\",\"headers\":\"[(b'host', b'testserver'), (b'accept', b'*/*'), (b'accept-encoding', b'gzip, deflate, br'), (b'connection', b'keep-alive'), (b'user-agent', b'testclient')]\",\"client\":\"['testclient', 50000]\",\"server\":\"['testserver', 80]\",\"extensions\":\"{'http.response.debug': {}}\",\"state\":\"{}\",\"app\":\"<starlette.applications.Starlette object at 0x12f39c990>\",\"starlette.exception_handlers\":\"({<class 'starlette.exceptions.HTTPException'>: <bound method ExceptionMiddleware.http_exception of <starlette.middleware.exceptions.ExceptionMiddleware object at 0x12f39dcd0>>, <class 'starlette.exceptions.WebSocketException'>: <bound method ExceptionMiddleware.websocket_exception of <starlette.middleware.exceptions.ExceptionMiddleware object at 0x12f39dcd0>>}, {})\",\"router\":\"<starlette.routing.Router object at 0x12f39c510>\",\"endpoint\":\"<function _wrap_ep.<locals>._f at 0x129bef6a0>\",\"path_params\":\"{}\"}\n\n\n\nclient.get('/user/jph').text\n\n'Good day to you, jph!'\n\n\n\[email protected]('/html/{idx}')\nasync def get_html(idx:int):\n return Body(\n H4(\"Wow look here\"),\n P(f'It looks like you are visitor {idx}! Next is {idx+1}.')\n )\n\n\ndisplay.HTML(client.get('/html/1').text)\n\n\n \nWow look here\n \n \nIt looks like you are visitor 1! Next is 2.\n \n\n\n\n\nreg_re_param(\"imgext\", \"ico|gif|jpg|jpeg|webm\")\n\[email protected](r'/static/{path:path}{fn}.{ext:imgext}')\ndef get_img(fn:str, path:str, ext:str): return f\"Getting {fn}.{ext} from /{path}\"\n\n\nclient.get('/static/foo/jph.ico').text\n\n'Getting jph.ico from /foo/'\n\n\n\nModelName = str_enum('ModelName', \"alexnet\", \"resnet\", \"lenet\")\n\napp = FastHTML()\[email protected](\"/models/{nm}\")\ndef model(nm:ModelName): return nm\n\[email protected](\"/files/{path}\")\nasync def txt(path: Path): return path.with_suffix('.txt')\n\n\nprint(TestClient(app).get('/models/alexnet').text)\n\nalexnet\n\n\n\nprint(TestClient(app).get('/files/foo').text)\n\nfoo.txt\n\n\n\nfake_db = [{\"name\": \"Foo\"}, {\"name\": \"Bar\"}]\n\[email protected](\"/items/\")\ndef read_item(idx:int|None = 0): return fake_db[idx]\n\n\nprint(TestClient(app).get('/items/?idx=1').text)\n\n{\"name\":\"Bar\"}\n\n\n\nprint(TestClient(app).get('/items/').text)\n\n{\"name\":\"Foo\"}\n\n\n\[email protected](\"/booly/\")\ndef booly(coming:bool=True): return 'Coming' if coming else 'Not coming'\n\n\ncli = TestClient(app)\nprint(cli.get('/booly/?coming=true').text)\n\nComing\n\n\n\nprint(cli.get('/booly/?coming=no').text)\n\nNot coming\n\n\n\[email protected](\"/datie/\")\ndef datie(d:date): return d\n\n\ncli = TestClient(app)\ndate_str = \"17th of May, 2024, 2p\"\nprint(cli.get(f'/datie/?d={date_str}').text)\n\n2024-05-17 14:00:00\n\n\n\n@dataclass\nclass Bodie:\n a:int;b:str\n\n\[email protected](\"/bodie/{nm}/\")\nasync def bodie(nm:str, data:Bodie):\n res = asdict(data)\n res['nm'] = nm\n return res\n\n\ncli.post('/bodie/me', data=dict(a=1, b='foo')).text\n\n'{\"a\":1,\"b\":\"foo\",\"nm\":\"me\"}'\n\n\n\[email protected](\"/setcookie\")\nasync def setc(req):\n now = datetime.now()\n res = Response(f'Set to {now}')\n res.set_cookie('now', str(now))\n return res\n\n\ncli.get('/setcookie').text\n\n'Set to 2024-05-19 01:33:57.762263'\n\n\n\[email protected](\"/getcookie\")\nasync def getc(now:date): return f'Cookie was set at time {now.time()}'\n\n\ncli.get('/getcookie').text\n\n'Cookie was set at time 01:33:57.762263'\n\n\n\[email protected](\"/ua\")\nasync def ua(user_agent:str): return user_agent\n\n\ncli.get('/ua', headers={'User-Agent':'FastHTML'}).text\n\n'FastHTML'", | ||
"crumbs": [ | ||
"fasthtml" | ||
] | ||
}, | ||
{ | ||
"objectID": "components.html", | ||
"href": "components.html", | ||
"title": "Components", | ||
"section": "", | ||
"text": "from pprint import pprint\nfrom IPython import display\n\n\nsource\n\nCheckbox\n\n Checkbox (checked:bool=False, label=None, **kw)\n\n\ndef show_html(xt): return display.HTML(to_xml(xt))\n\n\nshow_html(Checkbox(True, 'Check me out!'))\n\n\n \nCheck me out!\n\n\n\n\nsource\n\n\nHidden\n\n Hidden (value:str='', **kw)\n\n\nsource\n\n\nset_val\n\n set_val (tag, attr, val)\n\n\nsource\n\n\nfind_inps\n\n find_inps (html)\n\n\nsource\n\n\nfill_form\n\n fill_form (form, obj)\n\nModifies form in-place and returns it\n\n@dataclass\nclass TodoItem:\n title:str; id:int; done:bool\n \ntodo = TodoItem(id=2, title=\"Profit\", done=True)\ncheck = Label(Checkbox(id=\"done\"), 'Done')\nform = Form(Fieldset(Input(id=\"title\"), check, Hidden(id=\"id\"), Button(\"Save\")))\nfill_form(form, todo)\n\n<form>\n <fieldset>\n <input id=\"title\" name=\"title\" value=\"Profit\">\n <label>\n <input type=\"checkbox\" id=\"done\" name=\"done\" checked=\"1\">\nDone\n </label>\n <input type=\"hidden\" value=\"2\" id=\"id\" name=\"id\">\n <button>\nSave\n </button>\n </fieldset>\n</form>\n\n\n\nsource\n\n\nfill_dataclass\n\n fill_dataclass (src, dest)\n\nModifies dataclass in-place and returns it\n\nnt = TodoItem('', 0, False)\nfill_dataclass(todo, nt)\nnt\n\nTodoItem(title='Profit', id=2, done=True)", | ||
"crumbs": [ | ||
"Components" | ||
] | ||
}, | ||
{ | ||
"objectID": "core.html", | ||
"href": "core.html", | ||
"title": "FastHTML", | ||
"section": "", | ||
"text": "from IPython import display\nfrom enum import Enum\nfrom pprint import pprint\n\nfrom starlette.testclient import TestClient\n\n\n# if 'HX-Request' not in request.headers:\n# resp = wrap_root(resp, self.headtags)\n\n\nsource\n\ndate\n\n date (s)\n\n\nsource\n\n\nsnake2hyphens\n\n snake2hyphens (s)\n\n\nsource\n\n\nRouteX\n\n RouteX (path, endpoint, *args, **kw)\n\nInitialize self. See help(type(self)) for accurate signature.\n\nsource\n\n\nFastHTML\n\n FastHTML ()\n\nInitialize self. See help(type(self)) for accurate signature.\n\nsource\n\n\nreg_re_param\n\n reg_re_param (m, s)", | ||
"crumbs": [ | ||
"FastHTML" | ||
] | ||
} | ||
] |
Oops, something went wrong.