Giter Site home page Giter Site logo

chaoyingz / flect Goto Github PK

View Code? Open in Web Editor NEW
144.0 3.0 2.0 1.49 MB

A full-stack framework that integrates FastAPI and React.

Home Page: https://flect-docs.fly.dev/docs/introduction/

License: MIT License

Makefile 0.44% JavaScript 3.65% HTML 0.22% TypeScript 50.87% CSS 0.92% Python 43.92%
fastapi shadcn-ui pydantic react-router next reactjs fastui

flect's Introduction

flect

Turning ideas into web app fast.

Test pypi versions license

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.
  • Custom Components: You can conveniently use your own built React components in flect.

You can also view the documentation website, which is completely built with flect, source code can be found here.

Why use flect?

flect enables developers to harness the combined power of Python and JavaScript ecosystems, facilitating the creation of web applications with efficiency and ease:

If you're a Python developer — you can build responsive web applications using React without writing a single line of JavaScript, or touching npm.

If you're a frontend developer — you can concentrate on building magical components that are truly reusable, no copy-pasting components for each view.

For everyone — a true separation of concerns, the backend defines the entire application; while the frontend is free to implement just the user interface.

From FastUI

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 Optional

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

# Define a model for creating new todo items with a single 'task' field
class TodoInCreate(BaseModel):
    task: 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(
        body=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",
                    model=TodoInDB
                )
            ]
        )
    )

# 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.

flect's People

Contributors

chaoyingz avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

flect's Issues

Project Development Roadmap

Below is a list of planned updates and optimizations for our project, focusing on the following areas: documentation, coding practices, testing, component additions, styling enhancements, and feature enhancements.

Documentation Improvements

  • Provide more detailed documentation.
  • Optimize the get-started documentation. The current “todo” example does not effectively showcase the project’s features, such as folder routing, which is not demonstrated.
  • Add more examples (e.g., Auth, Chatbot)
  • Add a feature to view the code corresponding to the content in the documentation's preview block.

Enhanced Testing

  • Increase the number of tests to ensure code quality.

Coding Practices Optimization

  • Optimize the handling of group routing. The current code for this task is somewhat complex and needs simplification.
  • Optimize the render_to_string function. Currently, rendering values require escaping, which is too repetitive and needs streamlining.

Component Additions

  • Add more components to achieve consistency with shadcn/ui.

Styling Enhancements

  • Optimize the TailwindCSS safelist. It’s currently not quite reasonable.
  • Add support for CSS calc.

Feature Enhancements

  • Add support for custom components.
  • Add hot reload for dev
  • 🚧 Separate the core and component code, making the component an optional dependency.

Feel free to share your thoughts, ideas, and feedback on our project’s development roadmap. We are eager to hear from everyone interested in making our project even better.

v0.2.8 Failed to load styles since latest npm release is v0.2.7

Describe the bug
When creating a new project with flect v0.2.8 either via the cookiecutter or by hand, styles are not loaded as the flect v0.2.8 package does not exist on npm

To Reproduce

  1. Create a new flect project via cookiecutter template for example
  2. rye sync
  3. make dev
  4. Go to http://127.0.0.1:8000
  5. Confirm styles are missing

Expected behavior
Expect styles to load from flect npm package

Screenshots
2024-03-26-142529_2814x1442_scrot

Desktop (please complete the following information):

  • OS: Linux
  • Browser Chrome
  • Version 123.0.6312.58

Additional context
Workaround for now, change line 18 of flect/render.py to FLECT_PREBUILT_URI = "https://unpkg.com/@chaoying/[email protected]/dist/assets" which is the latest version available on NPM

Docs link broken

Describe the bug
Docs link in README file is broken

To Reproduce
Steps to reproduce the behavior:

Nav to https://flect.celerforge.com/

Expected behavior
Link works well

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.