Skip to main content

A custom wrapper on top of the reflex library to ease creating and interacting with custom components.

Project description

reflex-wrapper

reflex-wrapper is a Python module that provides a wrapper on top of the reflex library. It mostly behaves just like the reflex module, but simplifies the public API to make creating custom components more user-friendly. The idea behind this wrapper was that, in reflex, States are pydantic models (classes) who are only instantiated per-session by the server itself. This means that you never should instantiate state classes directly in your reflex code. As a matter of fact, what would be considered "instances" of a State in reflex are actualy subclasses of an initial pydantic rx.State class (but still classes!). This design choice, while being very elegant on a technical point of view for input validation and multi-session state management, also made the objet model less intuitive to a regular python developper, who expects to create 3 independant stateful components by instantiating the component class three times and that's it. To work around this, I created a custom Component class that abstracts away these pydantic state shenanigans and allows to use reflex components as normal instances of their Component subclass.

All standard reflex objects accessed from the rx wrapper should be automatically converted into these Components, or into objects supporting them, so that you don't have to bother about any additional overhead introduced by the wrapper and just focus on creating your app.

I'm rather new to Reflex, this small project is meant as an exercise and reflects my current (limited) understanding of the library. I may have neglected to cover some important functionalities. Feel free to check the source code and suggest improvements.

Installation

pip install reflex-wrapper

Usage

from reflex_wrapper import rx

# Define a custom component by subclassing the rx.Component class

class Counter(rx.Component):

    count: int = 0

    @rx.var
    def twice_the_count(self):
        return 2*self.count

    def increment(self):
        self.count += 1

    def decrement(self):
        self.count -= 1

    # Above is the state definition with a state var, a computed var and event handlers
    #----------------------------------------------------------------------
    # Below is the get_component method that returns the state-dependant layout of the component

    def get_component(self,*children, **props):
        return rx.hstack(
            rx.button("Decrement", on_click=self.decrement),
            rx.text(self.count),
            rx.button("Increment", on_click=self.increment),
            **props,
        )

# Notice that get_component is not defined as a class method but a regular instance method
# Notice also that state variables and event handlers can be accessed directly from self

# Now we can reuse this component to create a page layout
# No need to define counter=Counter.create, the Counter class can be instantiated directly into Component objects

@rx.page() # Decorators still work
def index():
    cnt1=Counter(count=10,background='blue') # we can intialize the count state value directly from props
    cnt2=Counter()
    cnt2.count=5
    cnt2.background='green' # we can also edit via attribute style access after instantiation
    btn1=rx.button("Click to add 2 to the first counter",on_click=cnt1.set_count(cnt1.count+2)) # state variables / methods / setters can be accessed directly from the component instance
    cnt3=Counter(count=cnt1.twice_the_count) # we can enforce cnt3's count to echo some (computed) state var of cnt1. (methods modifying cnt3's count will have no visible effect anymore)
    lbl1=rx.text(cnt3.count) # will show cnt1.twice_the_count 
    box=rx.box(
        cnt1,
        cnt2,
        cnt3,
        lbl1,
        btn1
    )
    return box

app=rx.App()

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

reflex_wrapper-0.0.3.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

reflex_wrapper-0.0.3-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file reflex_wrapper-0.0.3.tar.gz.

File metadata

  • Download URL: reflex_wrapper-0.0.3.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for reflex_wrapper-0.0.3.tar.gz
Algorithm Hash digest
SHA256 6e5469d12673671b86d1568b78431d4e19b2aaf58a9d082fcf245cb580df6be2
MD5 e76f98774e47dea1e1d69dc01d106dc0
BLAKE2b-256 d0c1c0b1e8647821f0b34c607a47f2d1f219f52a95c1a1478aa1a6acc812cb4a

See more details on using hashes here.

File details

Details for the file reflex_wrapper-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for reflex_wrapper-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 78464d704cf2e4abeccdef302aad6a398f023e78f9293ad5fc54cb5d1f06211c
MD5 a1c511e1b2064a70a9e96e109f6a10bb
BLAKE2b-256 650f7549616311a5bb784843f6868560910b83e2f6bf091c21b19d0e9cefa758

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