Git Product home page Git Product logo

dash_oop_components's Introduction

Metrics

dash_oop_components's People

Contributors

dependabot[bot] avatar krylowicz avatar oegedijk avatar quadrismegistus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

dash_oop_components's Issues

Composite Component not registering inner components callbacks

I am trying to build a DashComponent which is a table.
A table row is also a DashComponent containing a button and some component. pressing the button toggles collapse the other component.
the rows should be added to table dynamically.

when I create an app which consists only of a table row (not inside the table component), everything works as it should.
when I try to use the table component as an app and add rows, the callbacks aren't working.

can you provide explanation n how to make this work?

here is my code:

from dash_oop_components import DashComponent
import dash_bootstrap_components as dbc
import dash_html_components as html
import dash


class CollapsibleTableRow(DashComponent):
    component_to_collapse: DashComponent
    collapse_button_id: str
    collapse_id: str

    def __init__(self, component_to_collapse: DashComponent, name):
        super().__init__()
        self.name = name
        self.component_to_collapse = component_to_collapse.layout()
        self.collapse_id = 'collapse' + self.name
        self.collapse_button_id = 'collapse_button' + self.name
        self.collapse_button = self.make_collapse_button()
        self.collapse = self.make_collapse()

    def make_collapse(self):
        collapse = html.Div(
            [
                dbc.Collapse(
                    children=[
                        self.component_to_collapse
                    ],
                    id=self.collapse_id,
                    is_open=False)
            ]
        )
        return collapse

    def make_collapse_button(self):
        button = dbc.Button(
            "Expand",
            id=self.collapse_button_id,
            className="mb-3",
            color="primary",
        )
        return button

    def layout(self):
        row = html.Tr([html.Td(self.collapse_button),
                       html.Td([self.collapse])])

        return row

    def component_callbacks(self, app):
        # this function is used to toggle the is_open property of each Collapse
        @app.callback(
            self.Output(self.collapse_id, "is_open"),
            [self.Input(self.collapse_button_id, "n_clicks")],
            [self.State(self.collapse_id, "is_open")]
        )
        def toggle_collapse(n, is_open):
            if n:
                return not is_open
            return is_open


class ComponentForCollapse(DashComponent):

    def __init__(self, name=None):
        super().__init__()
        self.name = name

    def layout(self):
        return html.Div(html.H2('Element'))


class TestComposite(DashComponent):

    def __init__(self):
        super().__init__()
        self.rows = []
        for i in range(2):
            self.rows.append(CollapsibleTableRow(component_to_collapse=ComponentForCollapse(),
                                                 name='row{0}'.format(i)).layout())

    def layout(self, params=None):
        return html.Div(self.rows)


summary_table = TestComposite()
sapp = dash.Dash()

sapp.layout = summary_table.layout()
summary_table.register_callbacks(sapp)
summary_table.register_components()
sapp.run_server()

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.