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

Custom actions do not work when applied through a component that gets added to a union such as ControlType #376

Closed
1 task done
maxschulz-COL opened this issue Mar 20, 2024 · 1 comment
Assignees
Labels
Bug Report 🐛 Issue contains a bug report

Comments

@maxschulz-COL
Copy link
Contributor

maxschulz-COL commented Mar 20, 2024

Description

The code below should to my understanding work, ie the dropdown value should be reflected in the Jumbotron subtitle.

from typing import Literal

from dash import html

import vizro.models as vm
from vizro import Vizro
from vizro.models.types import capture
from dash import callback, Input, Output

@capture("action")
def subtitle_changer(subtitle: str):
    """Custom action."""
    print("SUBTITLE",subtitle)
    return subtitle

# 1. Create new custom component
class Jumbotron(vm.VizroBaseModel):  
    """New custom component `Jumbotron`."""

    type: Literal["jumbotron"] = "jumbotron"  
    title: str  
    subtitle: str
    text: str

    def build(self):  
        return html.Div(
            [
                html.H2(self.title),
                html.H3(id = "subtitle_id_to_target",children=self.subtitle),
                html.P(self.text),
            ]
        )


# 2. Add new components to expected type - here the selector of the parent components
vm.Page.add_type("components", Jumbotron)
vm.Page.add_type("controls", vm.Dropdown)

page = vm.Page(
    title="Custom Component",
    components=[
        Jumbotron(  
            id="my_jumbotron",
            title="Jumbotron",
            subtitle="This is a subtitle to summarize some content.",
            text="This is the main body of text of the Jumbotron.",
        ),
vm.Card(id="my_card", text="Click on a point on the above graph."),
    ],
    controls=[vm.Dropdown(
        id = "my_dropdown",
        options = ["Subtitle 1", "Subtitle 2", "Subtitle 3"],
        multi = False,
        actions = [
            vm.Action(
                function = subtitle_changer(),
                inputs = ["my_dropdown.value"],
                outputs = ["subtitle_id_to_target.children"],
            ),
        ]
    )],
)

dashboard = vm.Dashboard(pages=[page])

Vizro().build(dashboard).run()

Expected behavior

Expected behaviour as described above.

Interestingly the custom action works when we use the Dropdown inside a Filter or Parameter and overwrite the pre-defined action there.

Defining a dash callback with the relevant Input and Output also works!

Which package?

vizro

Package version

0.1.14.dev0

Python version

3.9.12

OS

Mac

How to Reproduce

See above

Output

No response

Code of Conduct

@maxschulz-COL maxschulz-COL added Bug Report 🐛 Issue contains a bug report Needs triage 🔍 Issue needs triaging labels Mar 20, 2024
@maxschulz-COL maxschulz-COL removed the Needs triage 🔍 Issue needs triaging label Mar 20, 2024
@petar-qb
Copy link
Contributor

Solved with #478.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Report 🐛 Issue contains a bug report
Projects
None yet
Development

No branches or pull requests

2 participants