FastUI —— 更快地构建更好的 UI
FastUI 是一种构建由声明式 Python 代码来构建 Web 应用程序用户界面的新方法。
这意味着:
- 如果你是一名 Python 开发人员,可以使用 React 构建响应式 Web 应用程序,而无需编写任何 JavaScript 代码,也无需接触
npm
。 - 如果你是前端开发人员,可以专注于构建真正可重用的神奇组件,无需为每个视图复制粘贴组件。
- 对于每个人来说 —— 真正的关注点分离,后端定义了整个应用程序;而前端可以自由地仅实现用户界面
FastUI 的核心是一组匹配的 Pydantic 模型和 TypeScript interfaces,允许你定义用户界面。其在构建时由 TypeScript 和 Pyright/mypy 进行验证,并在运行时由 Pydantic 进行验证。
FastUI 由 4 部分组成:
fastui
PyPI 包— UI 组件的 Pydantic 模型和一些实用程序。虽然它与FastAPI配合良好,但它不依赖于 FastAPI,并且其中大部分可以与任何 Python Web 框架一起使用。@pydantic/fastui
npm 包— 一个 React TypeScript 包,让你在实现自己的组件时重用 FastUI 的机制和类型@pydantic/fastui-bootstrap
npm 包 — 使用 Bootstrap 实现/定制所有 FastUI 组件@pydantic/fastui-prebuilt
npm 包(在 jsdelivr.com CDN 上提供)提供了 FastUI React 应用程序的预构建版本,因此你无需安装任何 npm 包或自行构建任何内容即可使用它。Python 包提供了一个简单的 HTML 页面来服务此应用程序。
以下是一个简单但完整的 FastAPI 应用程序,它使用 FastUI 来显示一些用户配置文件:
from datetime import date
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from fastui import FastUI, AnyComponent, prebuilt_html, components as c
from fastui.components.display import DisplayMode, DisplayLookup
from fastui.events import GoToEvent, BackEvent
from pydantic import BaseModel, Field
app = FastAPI()
class User(BaseModel):
id: int
name: str
dob: date = Field(title='Date of Birth')
# define some users
users = [
User(id=1, name='John', dob=date(1990, 1, 1)),
User(id=2, name='Jack', dob=date(1991, 1, 1)),
User(id=3, name='Jill', dob=date(1992, 1, 1)),
User(id=4, name='Jane', dob=date(1993, 1, 1)),
]
@app.get("/api/", response_model=FastUI, response_model_exclude_none=True)
def users_table() -> list[AnyComponent]:
"""
Show a table of four users, `/api` is the endpoint the frontend will connect to
when a user fixes `/` to fetch components to render.
"""
return [
c.Page( # Page provides a basic container for components
components=[
c.Heading(text='Users', level=2), # renders `<h2>Users</h2>`
c.Table[User]( # c.Table is a generic component parameterized with the model used for rows
data=users,
# define two columns for the table
columns=[
# the first is the users, name rendered as a link to their profile
DisplayLookup(field='name', on_click=GoToEvent(url='/user/{id}/')),
# the second is the date of birth, rendered as a date
DisplayLookup(field='dob', mode=DisplayMode.date),
],
),
]
),
]
@app.get("/api/user/{user_id}/", response_model=FastUI, response_model_exclude_none=True)
def user_profile(user_id: int) -> list[AnyComponent]:
"""
User profile page, the frontend will fetch this when the user visits `/user/{id}/`.
"""
try:
user = next(u for u in users if u.id == user_id)
except StopIteration:
raise HTTPException(status_code=404, detail="User not found")
return [
c.Page(
components=[
c.Heading(text=user.name, level=2),
c.Link(components=[c.Text(text='Back')], on_click=BackEvent()),
c.Details(data=user),
]
),
]
@app.get('/{path:path}')
async def html_landing() -> HTMLResponse:
"""Simple HTML page which serves the React app, comes last as it matches all paths."""
return HTMLResponse(prebuilt_html(title='FastUI Demo'))

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
新技术 LINT 可强制 LLM 回答有毒问题
美国普渡大学(Purdue University)的研究人员发布了,一篇名为《Make Them Spill the Beans! Coercive Knowledge Extraction from (Production) LLMs》的论文。描述了他们通过利用大模型厂商倾向于公开与提示响应相关的概率数据的特点,设计出了一种可以打破现有 LLM 规则,使其给出“有害”回答的新颖方法。 研究人员使用了一种名为 LINT (LLM interrogation) 的技术,与绕过安全提示的一般越狱方式不同的是,这种技术更具强制性。它涉及到对概率值(logits)或软标签(soft labels)的理解 —— 从统计学上讲,其可以有效区分安全响应与有害响应。 “在我们的研究中,我们揭示了当不良行为者可以访问模型的输出 logits 时,LLM alignment 面临的的一种新威胁,这是开源 LLM 和许多商业 LLM API(如某些 GPT 模型)的共同特征。它并不依赖于制作特定的提示。相反,它利用了这样一个事实:即使 LLM 拒绝了有毒请求,有害响应也往往隐藏在输出 logits 的深处。...
-
下一篇
.NET 8 极致性能优化 - Reflection(反射)
前言 反射一直是性能的瓶颈,所以无论哪个.NET版本反射的优化必然少不了。主要是集中在两个方面优化,分配和缓存。.NET8自然也不例外。本篇看下。 原文:.NET8极致性能优化Reflection 概述 比如针对GetCustomAttributes 通过反射获取属性的优化,以下例子 // dotnet run -c Release -f net7.0 --filter "*" --runtimes net7.0 net8.0public class Tests{ public object[] GetCustomAttributes() => typeof(C).GetCustomAttributes(typeof(MyAttribute), inherit: true); [My(Value1 = 1, Value2 = 2)] class C { } [AttributeUsage(AttributeTargets.All)] public class MyAttribute : Attribute { public int Value1 { get; set; } p...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL数据库在高并发下的优化方案
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Docker容器配置,解决镜像无法拉取问题
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Docker快速安装Oracle11G,搭建oracle11g学习环境