Skip to main content

Turning ideas into web app fast.

Project description

flect

Turning ideas into web app fast.

Test

Explore the docs »

Report Bug · Request Feature · 简体中文

What is flect?

flect is a Python framework for building full-stack web applications. It constructs user interfaces by utilizing Pydantic models in the backend that correspond to the properties of React components in the frontend. This integration enables quick development of interactive and beautiful UIs using Python.

The key features are:

  • Fast development: Write your entire app with Python, seamlessly integrating backend logic and frontend UI.
  • Easy Form Validation: Define a single Pydantic model for seamless and consistent form validation across your app, enhancing development speed and reducing potential errors.
  • Folder-Based Routing: Easy route management through folder structure.
  • Client-Side Routing: Fast, smooth page transitions without reloads.
  • SEO Friendly: Supports server-side rendering for better search engine visibility.

Example

In this example, we will demonstrate how to build a simple to-do application using flect. As flect is built on top of FastAPI, so you can define your routes using FastAPI’s syntax.

Note: In real-world flect applications, define page routes and post routes in separate files for better organization.

Below is a simple to-do application.

import json
from typing import Annotated, Optional

from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
from flect import PageResponse
from flect import components as c
from flect import form as f
from flect.actions import Notify
from flect.response import ActionResponse

# Define a model for creating new todo items with a single 'task' field
class TodoInCreate(BaseModel):
    task: Annotated[str, f.Input(placeholder="Enter task...")]

# Define a model for todo items stored in the database, extending the creation model with an 'id' and 'completed' field
class TodoInDB(TodoInCreate):
    id: int
    completed: Optional[bool] = False

# Initialize a list of todo items
todos = [
    TodoInDB(id=1, task="Task 1", completed=False),
    TodoInDB(id=2, task="Task 2", completed=True),
    TodoInDB(id=3, task="Task 3", completed=False),
]

# Define the page
async def page() -> PageResponse:
    return PageResponse(
        element=c.Container(
            # support tailwind css
            class_name="container mx-auto px-32 py-10",
            children=[
                # Add a heading to the page
                c.Heading(
                    level=1,
                    text="Todo App",
                    class_name="text-3xl mb-10",
                ),
                # Add a form for creating new todo items
                c.Form(
                    model=TodoInCreate,
                    submit_url="/",
                    class_name="mb-5 border p-5",
                ),
                # Add a table displaying all todo items
                c.Table(
                    datasets=todos,
                    class_name="border p-5",
                )
            ]
        )
    )

# Define the form handling logic
async def post(form: TodoInCreate) -> ActionResponse:
    todos.append(
        TodoInDB(
            id=len(todos) + 1,
            task=form.task,
            completed=False,
        )
    )
    # Return a notification with the submitted form values
    return ActionResponse(
        action=Notify(
            title="You submitted the following values:",
            description=json.dumps(jsonable_encoder(form), indent=2),
        )
    )

Which renders like this: flect-todo

Learn More

Credits

This project draws inspiration from the following frameworks:

License

This project is licensed under the terms of the MIT license.

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

flect-0.1.1.tar.gz (252.2 kB view details)

Uploaded Source

Built Distribution

flect-0.1.1-py3-none-any.whl (255.5 kB view details)

Uploaded Python 3

File details

Details for the file flect-0.1.1.tar.gz.

File metadata

  • Download URL: flect-0.1.1.tar.gz
  • Upload date:
  • Size: 252.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.2

File hashes

Hashes for flect-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e95aa7519b9d725feceee57f49ec0688ae9b240f4453914fe9eaf95a2df545f9
MD5 249c8672e4028367ea75a13e2bac472d
BLAKE2b-256 7836879e8829e06f7e5623a3165325ed17a1461ed6b7f3a25a7ad6c27749597d

See more details on using hashes here.

File details

Details for the file flect-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: flect-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 255.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.2

File hashes

Hashes for flect-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 169686a092e71a04f425e39c510375249227d8d7363be46e1af56e8903d40992
MD5 8495f269b930ef2f6f68ffbf381b92a5
BLAKE2b-256 4e097f35cf65cac62afd725334e05fdea3c5ca7bf53c64d20d617b0d7c29fd6d

See more details on using hashes here.

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