FastHTML – 纯 Python 中的现代 Web 应用程序
FastHTML – Modern web applications in pure Python

原始链接: https://fastht.ml/

FastHTML 是一个用于构建各种类型 Web 应用程序的工具,包括通用应用程序(例如使用 React、Django、NexJS 等构建的应用程序)、快速仪表板、原型、分析/模型/仪表板交互式报告、自定义博客和内容丰富的网站 动态互动。 部署选项包括支持 Python 的服务或服务器,例如 Railway.app、Vercel、Hugging Face Spaces、Replit 和 PythonAnywhere。 FastHTML 与 FastAPI 相关,受到其用户体验的启发,但旨在创建 HTML 而不是 API。 FastHTML 可以通过返回轻量级“部分”来处理现代单页应用程序 (SPA),这些“部分”会更新文档对象模型 (DOM),而无需执行全页刷新。 HTMX 是一个补充 FastHTML 的 JavaScript 库,支持通过响应整个网页上的事件从 Python 直接修改 DOM。 虽然仅使用 Python 创建基本 Web 应用程序是不必要的,但添加 JS 可以通过与现有 JS 库集成来增强功能。 由于 FastHTML 应用程序的重量较轻,因此其性能可能优于使用大型 JavaScript 库创建的应用程序。 使用 FastHTML 时,JavaScript 知识是可选的,因为大多数常见的 Web 应用程序仍然可以仅使用 Python 来制作。 与 React、Next.js 等流行工具相比,FastHTML 应用程序可能会更慢,具体取决于具体的实现。

Jeremy 介绍了他的项目 FastHTML,这是一个使用 Python 构建的 Web 应用程序框架、带有 HTMX 的基于超媒体的应用程序、ASGI/Uvicorn/Starlette、一个名为 FastTag 的新 Python 组件系统,并受到 FastAPI API 的启发。 他提到他喜欢使用 Python,并且发现与最近的 Web 应用程序开发相比,这种方法更令人愉快。 用户可以对其进行测试并分享反馈。 作者分享说,他将 FastHTML 用于一个业余项目,发现它很有用且用户友好。 他指出,由于自定义选项有限,CSS 样式是基于 Python 的解决方案的一个缺点。 作者建议 FastHTML 改善 Python Web 生态系统。 然而,作者提出了两个方面的担忧: 1. Python 世界中的前端孤立。 虽然该解决方案允许嵌入 JavaScript 代码,但集成非 Python 资源变得具有挑战性。 2. Python 项目的潜在增长可能会导致 Django 组件的采用。 随着 FastAPI 功能变得越来越普遍,并且 Django 中包含的其他功能不断增加,使用 FastHTML 可以开发类似 Django 的系统。 尽管如此,作者承认 FastHTML 非常适合较小的、定义良好的项目。 最后,作者赞扬了 FastHTML 易于交付功能齐全且行为良好的 Web 应用程序,并对探索 FastHTML 内容管理系统 (CMS) 表示兴奋。 此外,他们还提到 WordPress 源于其广泛的插件生态系统的优势是个人或公司无法单独比拟的,这表明时机因素的组合在取得广泛成功方面发挥着至关重要的作用。
相关文章

原文

It's good for: general purpose web applications (i.e anything you'd build with React, Django, NexJS, etc); quick dashboards, prototypes, and in-company apps (e.g. like what you might use gradio/streamlit/etc for); Analytics/models/dashboards interactive reports; Custom blogs and content-heavy sites where you also want some interactive/dynamic content.

You can deploy a FastHTML app to any service or server that supports Python. We have guides and helpers for Railway.app, Vercel, Hugging Face Spaces, Replit, and PythonAnywhere. You can also use any VPS or server, or any on-premise machine with Python installed. All major operating systems are supported.

FastAPI is one of the inspirations for FastHTML. We are fans of its developer experience and tried to make FastHTML extremely familiar for FastAPI users. FastAPI is designed for creating APIs, whereas FastHTML is designed for creating HTML (i.e "Hypermedia applications"). Anything you could create with FastAPI (plus a JS frontend), you could also create with FastHTML, and vice versa -- if you prefer mainly writing JS, you might prefer FastAPI, since you can move a lot of client-side logic into the JS. If you prefer mainly writing Python, you'll probably want to use FastHTML, since you can often avoid using JS entirely.

FastHTML is specifically designed to make writing modern SPA apps as fast and easy as possible, whilst also ensuring the apps you write are scalable and performant. By default, FastHTML routes return lightweight "partials" that update the DOM directly, rather than doing a full page refresh.

HTMX is best thought of as filling in the missing bits of a web browser -- in fact, web browser manufacturers are considering incorporating similar features directly into future browsers. It is a small javascript library that with a single line of HTML lets you respond to any event from any part of a web page by modifying the DOM in any way you like, all directly from Python. Whilst you don't have to use it with FastHTML, it will dramatically increase the amount of stuff you can do!

No, and yes! You can write nearly any standard web app with just Python. However, using a bit of JS can be helpful -- for instance, nearly any existing JS lib can be incorporated into a FastHTML app, and you can sprinkle bits of JS into your pages anywhere you like.

It depends. Apps using FastHTML and HTMX are often faster than JS-based approaches using big libraries, since they can be very lightweight.

联系我们 contact @ memedata.com