The fastest way to create an HTML app
Project description
fasthtml
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.
This is a way to write real web applications, without the fuss.
To learn how to use it, please visit the documentation.
Install
pip install python-fasthtml
How to use
Import fasthtml
, and you’ll probably want the widgets from
fastcore.xml
too.
from fasthtml import *
from fastcore.xml import *
Create your app.
app = FastHTML()
Create 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).
Note 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:
@app.get('/user/{nm}')
def get_nm(nm:str): return f"Good day to you, {nm}!"
Normally you’d save this into a file such as main.py, and then run it in
uvicorn
using:
uvicorn main:app
However, for testing, we can use Starlette’s TestClient
to try it out:
from starlette.testclient import TestClient
client = TestClient(app)
r = client.get('/user/Jeremy')
r
<Response [200 OK]>
TestClient uses httpx
behind the scenes, so it returns a
httpx.Response
, which has a text
attribute with our response body:
r.text
'Good day to you, Jeremy!'
FastHTML 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:
@app.get('/html/{idx}')
async def get_html(idx:int):
return Body(
H4("Wow look here"),
P(f'It looks like you are visitor {idx}! Next is {idx+1}.')
)
from IPython import display
display.HTML(client.get('/html/1').text)
Wow look here
It looks like you are visitor 1! Next is 2.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for python_fasthtml-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3a696d937d7340c83b5b7485985d925356528f77f7e7307ccdc1ac97297e794 |
|
MD5 | 30adf4f7fd78282a6ba195ddb8f503f6 |
|
BLAKE2b-256 | 0c085f759b0d5f9d7da8aca40c3f6c6143b49561c58af121b256d56258646fa0 |