How can I delete data with shiny input_action_button?

I have recently started working with Shiny Python and got stuck at some point.

from pathlib import Path

from shiny import reactive
from shiny.express import input, render, ui


ui.page_opts(title="Stock explorer", fillable=True)

with ui.sidebar():
    ui.input_text("data_one","Insert Number",placeholder="Insert Number 1")
    ui.input_text("data_two","Insert Number",placeholder="Insert Number 2")
    ui.input_action_button("add_data", "Add Data", class_="btn-primary")
    ui.input_action_button("delete_data", "Delete Data", class_= "btn-danger")


with ui.layout_column_wrap(fill=False):
    with ui.value_box():
        "Number One"

        @render.ui
        def data_1():
            return f"{get_data_one()}"

    with ui.value_box():
        "Number 2"

        @render.ui
        def data_2():
            return f"{get_data_two()}"


ui.include_css(Path(__file__).parent / "styles.css")


@reactive.calc
@reactive.event(input.add_data, ignore_none=False)
def get_data_one():
    return input.data_one()

@reactive.calc
@reactive.event(input.add_data, ignore_none=False)
def get_data_two():
    return input.data_two()

@reactive.calc
def data_delete():
    pass

I am stuck as to how I can update the ui.value_box as empty while pressing the Delete Data button. In the reference page Shiny for Python I can see I would be able to update inputs and layout, but not able to get how to update the value_box. I have kept the data_delete() function to be pass as of now.

Any feedback or suggestions will help.

I'm not quite sure what you're trying to do but I think you're trying to use event handling to do things that should really be handled reactively. Here's an example with the two ways to do this type of updating:

from pathlib import Path

from shiny import reactive
from shiny.express import input, render, ui


with ui.sidebar():
    ui.input_text("data_one", "Reactive update", placeholder="Insert Number 1")
    ui.input_text("data_two", "Button update", placeholder="Insert Number 2")
    ui.input_action_button("add_data", "Add Data", class_="btn-primary")
    ui.input_action_button("delete_data", "Delete Data", class_="btn-danger")

with ui.layout_column_wrap(fill=False):
    with ui.value_box():
        "Number One"

        @render.text
        def data_1():
            return f"{input.data_one()}"

    with ui.value_box():
        "Number 2"

        @render.text
        def data_2():
            return f"{data_2_val()}"


data_2_val = reactive.value(None)


@reactive.effect
@reactive.event(input.add_data)
def get_data_two():
    data_2_val.set(input.data_two())
    ui.update_numeric("data_two", value = "")


@reactive.effect
@reactive.event(input.delete_data)
def data_delete():
    data_2_val.set(None)
    ui.update_numeric("data_two", value = "")
    

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.