Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks for components classes. #464

Open
T4rk1n opened this issue Nov 21, 2018 · 2 comments
Open

Hooks for components classes. #464

T4rk1n opened this issue Nov 21, 2018 · 2 comments

Comments

@T4rk1n
Copy link
Contributor

T4rk1n commented Nov 21, 2018

The generated components gets overwritten every time we run the generation. We can't add python code to them because of that. It would be useful if we could add code that could modify the props before sending it to the front-end.

Solution proposal:

  • Generate a _ComponentClass.py file that gets overwritten.
  • Generate ComponentClass.py only if it doesn't exist, it import _ComponentClass and subclass it.
@bpostlethwaite
Copy link
Member

Great yes this could be a way to add direct Dataframe parsing support to plotly/dash-table

@T4rk1n
Copy link
Contributor Author

T4rk1n commented Nov 22, 2018

I refactored the component generation, now every prop will have a descriptor that fires a prop change event (for the backend code only). The returned value from the handler will be the actual value used by the component.

Example dataframe serialization:

def on_prop_change(component, prop_name, old_value, new_value, prop_type):
    if isinstance(new_value, pd.DataFrame):
        return new_value.to_dict('records')
    return new_value

Two options for the prop changes events:

Generate the _component_class._ComponentClass and component_class.Component so the user can override methods & attributes

PROS:

  • Totally customizable class.
  • Hooks only for the single component in an overridden method.

CONS:

  • Less docstring support in not so smart editors.
  • Can only modify the class from the component package.

Add handlers to global object (ComponentRegistry)

PROS:

  • No need to change any class code, just add a function to the registry.
  • More control over the api.
  • Proper docstring support, the generated docstring is on the main class.
  • Support for patching component props externally from the component package. (any dash app)

CONS:

  • Only props value setter is customizable.
  • Prop changes need to go thru all handlers or filter the handlers.

I favor the second option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants