Code Monkey home page Code Monkey logo

Comments (2)

LineIndent avatar LineIndent commented on June 26, 2024

I tested this on my end and it worked:

Screenshot 2023-08-29 at 9 27 39 PM

This is what I did:

  1. In the file where you want to put the counter:
from app.core.base import RxBasePage
from app.helpers.nav_helpers import NavHelper
import reflex as rx
from app.states.counterState import CounterState

def CounterComponent():
    return rx.box(
        rx.text("Counter App", font_size="1.5em"),
        rx.spacer(),
        rx.hstack(
            rx.button(
                rx.icon(tag="add"), color="green", on_click=CounterState.increment
            ),
            rx.text(f"{CounterState.count}"),
            rx.button(
                rx.icon(tag="minus"), color="red", on_click=CounterState.decrement
            ),
        ),
    )


class RxPage:
    # Title of page: must match high-level key in config.py
    def __title__(self):
        return "Reflexfy"

    # Page route path: must follow /parent-key/file-name *without .py extension*
    def __route__(self):
        return "/"

    # Left navigation panel: automated based on config navigation order
    def __left_navigation__(self):
        nav: list = NavHelper.__get_left_navigation__(self.__title__())
        return NavHelper.__set_left_navigation__(nav)

    # Right navigation panel: TBD
    def __right__navigation__(self):
        return []

    # Mobile navigation drop down
    def __mobile_navigation__(self):
        return NavHelper.__get_left_navigation__(self.__title__())

    # Main content area: takes in rx.Componenets and passes them to base file
    def __components__(self):
        return [CounterComponent()]

    # Build method: creates a new instance for the page above
    def build(self):
        page = RxBasePage(
            self.__components__(),
            self.__left_navigation__(),
            self.__right__navigation__(),
            self.__mobile_navigation__(),
        )
        return page.build()
  1. Place your states all in the states folder - it'll be easier to organize and keep track of. Inside of states i created a file called counterState.py and placed the following code as such:
from .mainState import MainState

class CounterState(MainState):
    count: int = 0

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

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

This i what I did and the counter worked.
Don't place your own components, like the counter, outside the app folder. This messes with the path, even if you're using Reflex plainly, always place the files inside the main directory. So for example, your content folder, place it inside the app folder and this way you'll have access to it easily.

from reflexify.

fsmosca avatar fsmosca commented on June 26, 2024

Thanks it worked.

from reflexify.

Related Issues (4)

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.