Skip to main content

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.

Metadata

Release files for reflex-wrapper 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reflex-wrapper 0.0.4
File Size Uploaded
reflex_wrapper-0.0.4.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for reflex-wrapper 0.0.4
File Interpreter ABI Platform
reflex_wrapper-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 14.7 kB

Release files / reflex_wrapper-0.0.4.tar.gz

Download URL reflex_wrapper-0.0.4.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8eb15176caf8e7bdb49203f30949f0f58422e3b1f8d129796930f61fe033e4c6
BLAKE2b-256 checksum
How to use checksums
75ce8d6784b4c39531192f10870fa3d563835765a63f39672a567321e225c287
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / reflex_wrapper-0.0.4-py3-none-any.whl

Download URL reflex_wrapper-0.0.4-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
323193d59c904cff7ba61d5bea13a558a2779d19cab77aa9417d629990268fdf
BLAKE2b-256 checksum
How to use checksums
fa6d8ab3eb007fd3f9a5d3261b5ff890757328fb470626420879d7514bcc1c47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page