Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-fasthtml-0.0.3.tar.gz (18.1 kB view hashes)

Uploaded Source

Built Distribution

python_fasthtml-0.0.3-py3-none-any.whl (17.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page