0

Working on a Shiny app where the user can press a button and receive all tests that evaluate to False. I'm able to successfully insert these test messages into the UI; however, every time I press the button to rerun these tests, the UI adds onto the test results from before. How can I properly remove UI before inserting more UI?

from shiny import App, reactive, ui, run_app


# for reference, I run itertools.filterfalse over this to obtain a list of messages whose conditions evaluate to False
test_results = {'Test message #1':True, 'Test message #2':False, 'Test message #3': False}
test_results_output = ['Test message #2', 'Test message #3']


app_ui = ui.page_fluid(
    ui.input_action_button('button_scan_directory', 'Scan directory'),
    ui.output_ui('output_scan_report')
)

def server(input, output):
    @reactive.Effect
    @reactive.event(input.button_scan_directory, ignore_init=True, ignore_none=True)
    def output_scan_report():
        for test in test_results_output:
            ui.remove_ui(selector="p:has(> #App Error)")
            ui.insert_ui(
                ui.p(f"App Error :: {test}"),
                selector='#output_scan_report',
                where='afterEnd'
            )


app = App(app_ui, server)
run_app(app)

Jan
  • 2,245
  • 1
  • 2
  • 16

1 Answers1

0

Working solution for now - would prefer not resorting to ui.output_text_verbatim but that's how I'm getting newlines and non-duplicated messages.

### server function
@render.text
def output_test_messages:
    return '\n'.join(f"- {test}" for test in tests_results_output)

### app ui
ui.output_text_verbatim('output_test_messages')