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 to ease creating custom components as well as adding syntactic sugar to setup and interact with components after they are instantiated. It is meant to be "without loss", meaning that all that was possible to achieve with reflex is still possible using the wrapper, and as easily or more.

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 double_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():
    cnt=Counter(count=10,background='blue') # we can intialize the count state value directly from props
    cnt.count=5
    cnt.background='green' # we can also edit via attribute style access after instantiation
    btn1=rx.button("Click to add 2",on_click=cnt.set_count(cnt.count+2)) # we can use state setters like this
    cnt2=Counter(count=cnt.double_count) # we can link a second counter's state to the first's, thus synchronizing their states.
    box=rx.box(
        cnt,
        cnt2
        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.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

reflex_wrapper-0.0.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: reflex_wrapper-0.0.1.tar.gz
  • Upload date:
  • Size: 6.0 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.1.tar.gz
Algorithm Hash digest
SHA256 d5a6dbe1efdc9c64eced7905f18d8d1993db7f9723c2bf1c529cc7d8224cd5c2
MD5 3c136083462ace93eea68ab1dba0401b
BLAKE2b-256 8107f94d0796ef551f87b068ddcd0e9d7ced7f0d53d5c30e6e5385461a7beac5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for reflex_wrapper-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 794855da815a504565da649d2b78f5f10d8f3836fa5fee0030c6bc6f9ca094f5
MD5 f71f74a3a91c33a51c74792b9d6f5402
BLAKE2b-256 da62bcbb9133b86d1cd89b887906a71dd0045e0f5244a7c824caa275e8163e34

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