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


Release history Release notifications | RSS feed

This version

0.0.4

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.4.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_fasthtml-0.0.4-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file python-fasthtml-0.0.4.tar.gz.

File metadata

  • Download URL: python-fasthtml-0.0.4.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for python-fasthtml-0.0.4.tar.gz
Algorithm Hash digest
SHA256 c88718f84156f2bd4365083347c7ae974b84b693982abb57c4de8d9cd70583e2
MD5 cbedb12f87697d86f056e979d5095203
BLAKE2b-256 66e146a872c86fd2982616611963398f873e4cd6c3391c562911678bbafc8b37

See more details on using hashes here.

File details

Details for the file python_fasthtml-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for python_fasthtml-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5609e38d34db91a14a8d294dd5b708516d32b73279afd266fc09f7fc8bdada7f
MD5 8972aeb3bbecbc67f22fa188db3b7238
BLAKE2b-256 9636520c70bdb98b1a8ada7684de71a8865d6962e19de2dd40f77839418dadd8

See more details on using hashes here.

Supported by

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