How pydantic/fastui works
Main Components
• Backend Services:
• The Python “fastui” package (in src/python-fastui) contains the core Pydantic models, component definitions, and REST endpoints. This is used by FastAPI-based applications (as seen in the demo) to deliver UI components in response to HTTP requests.
• The demo folder provides an example FastAPI application that shows how the backend delivers UI by mapping endpoints to the UI components.
• Frontend Packages:
• The React-based components and libraries are provided in multiple npm packages located under src/npm-fastui, src/npm-fastui-bootstrap, and src/npm-fastui-prebuilt.
• “@pydantic/fastui” is the generic TypeScript package that corresponds to the Python interface models.
• “@pydantic/fastui-bootstrap” offers a Bootstrap-themed implementation of FastUI components.
• “@pydantic/fastui-prebuilt” supplies a compiled/prebuilt version of the frontend so that users can deploy the UI without having to build it themselves.
• Documentation & Build Infrastructure:
• The docs folder (with MkDocs configuration in mkdocs.yml) and associated scripts (like build-docs.sh, bump_npm.py) support documentation generation.
• Configuration files (e.g., for ESLint, Prettier, TypeScript, GitHub workflows) indicate that code quality, automated builds, and testing are integrated into the project.
Relationships and Data Flow
• The Backend (Python/FastAPI) exposes HTTP endpoints (such as /api/ and /api/user/{user_id}/) to return FastUI component definitions.
• The Frontend (React) requests these endpoints, receives a JSON representation conforming to the Pydantic models, and then renders the corresponding React components.
• There is a clear separation of concerns: the “backend” is solely responsible for defining the UI and business logic, while the “frontend” serves as a reusable rendering engine (utilizing the prebuilt or customizable npm packages).
• The project also ensures type safety and schema matching between the two sides via TypeScript interfaces, JSON Schema, and Pydantic.
Architectural Patterns and Design Principles
• Component-Based Architecture: Both frontend and backend are built around discrete, self-contained components. In the frontend, these are the React components (for instance, Table, Modal, Navbar, etc.). In the backend, corresponding component models are defined using Pydantic.
• RESTful Communication: The backend provides endpoints that return a series of UI components. This strategy decouples the UI definition from the actual rendering (the “puppet and hand” metaphor used in the README).
• Separation of Concerns: The design clearly splits responsibilities between UI definition (Python) and UI implementation (React/TypeScript).
• Type Safety: Extensive use of typing through Pydantic’s models and TypeScript’s interfaces/compilation safeguards alignment between frontend and backend.