Metadata-Version: 2.1
Name: lazyfast
Version: 0.1.0
Summary: 
Author: Nikita Irgashev
Author-email: nik.irg@yandex.ru
Requires-Python: >=3.11,<4.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Requires-Dist: aiohttp (>=3.9.5,<4.0.0)
Requires-Dist: beautifulsoup4 (>=4.12.3,<5.0.0)
Requires-Dist: fastapi (>=0.105.0,<0.106.0)
Requires-Dist: markdown (>=3.6,<4.0)
Requires-Dist: pydantic (>=2.6.4,<3.0.0)
Requires-Dist: python-multipart (>=0.0.9,<0.0.10)
Requires-Dist: requests (>=2.32.3,<3.0.0)
Requires-Dist: uvicorn (>=0.24.0.post1,<0.25.0)
Description-Content-Type: text/markdown

# Viewlet

Viewlet is a lightweight Python library designed for building modern web interfaces using a component-based approach. It enables writing page logic on the server side in Python, integrating seamlessly with FastAPI. With Viewlet, interactive elements like inputs, buttons, and selects trigger component reloads that occur on the server, updating the component's state dynamically.

## Key Features

1. **Component-Based Approach**: Build web interfaces using lazy loaded components that encapsulate logic, state, and presentation. 
2. **Server-Side Logic**: Handle interactions and state management on the server, reducing client-side complexity.
3. **FastAPI Integration**: Each component or page is a FastAPI endpoint, allowing for dependency injection and other FastAPI features.
4. **Lightweight**: The only dependencies are FastAPI for Python and HTMX for JavaScript, which can be included via CDN.
5. **State Management**: Utilize a state manager that can trigger component reloads, ensuring a reactive user experience.

## Installation

To install Viewlet, use pip:

```bash
pip install viewlet
```

## Quick Start

Here's an example application to demonstrate how Viewlet works:

```python
from fastapi import FastAPI, Request
from viewlet import ViewletRouter, Component, tags


# ViewletRouter inherits from FastAPI's APIRouter
router = ViewletRouter()

# Define a lazy-loaded HTML component powered by HTMX
@router.component()
class MyComponent(Component):
    # Define a state field to store the component's value
    # We can pass this value from parent page or component
    # Or we can use it as local component state
    title: str

    # Override the view method to define HTML rendering logic
    # The view method is a FastAPI endpoint supporting dependency injection
    async def view(self, request: Request) -> None:

        # Use familiar HTML tags as Python objects with standard HTML attributes
        # The first parameter is the inner HTML of the tag
        tags.h1(self.title, class_="my-class")

        # Use the "with" operator to add additional HTML tags to the inner HTML
        with tags.div(style="border: 1px solid black"):

            # This span is a child of the div
            tags.span(request.headers)

# Initialize the page dependencies for component rendering
# The page endpoint is also a FastAPI endpoint
@router.page("/{name}")
def root(name: str):
    with tags.div(class_="container mt-6"):

        # Initialize this component to embed an HTMX div and trigger the view method only after the div is rendered
        MyComponent(title=f"Hello, World from {name}")

# Embed the router in a FastAPI app
app = FastAPI()
app.include_router(router)
```


## Documentation

Coming soon.


## License

Viewlet is licensed under the [MIT License](https://github.com/nikirg/renderable/blob/main/LICENSE).
