ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Advanced Callback
    plotly dash 2023. 6. 15. 19:02

    https://dash.plotly.com/advanced-callbacks 의 내용을 정리한다.

     

    PreventUpdate 사용

    • dash app 은 기본적으로 시작시 등록된 callback 을 한번씩 호출해 초기화 한다.
    • 시작시 callback 이 한번씩 호출되어 초기화 되는 것을 막으려면  prevent_initial_callbacks 사용
    app = Dash(__name__, prevent_initial_callbacks=True)
    or
    @app.callback(Output('output', 'children'), Input('input', 'value'), prevent_initial_call=True)

     

    • 특정 callback 만 초기화를 위해 호출 되는 것을 막으려면 PreventUpdate Exception 활용
    @app.callback(
        Output(component_id='body-div', component_property='children'),
        Input(component_id='show-secret', component_property='n_clicks')
    )
    def update_output(n_clicks):
        if n_clicks is None:
            raise PreventUpdate
        else:
            return "Elephants are the only animal that can't jump"

     

    dash.no_update 사용

    Output 이 특정 케이스의 경우 callback 에 의해 업데이트되지 않길 원한다면 (기존 값을 유지하고 싶다면) dash.no_update 를 사용한다.

    @app.callback(
        Output('out', 'children'),
        Output('err', 'children'),
        Input('num', 'value')
    )
    def show_factors(num):
        if num is None:
            # PreventUpdate prevents ALL outputs updating
            raise dash.exceptions.PreventUpdate
    
        factors = prime_factors(num)
        if len(factors) == 1:
            # dash.no_update prevents any single output updating
            # (note: it's OK to use for a single-output callback too)
            return dash.no_update, '{} is prime!'.format(num)
    
        return '{} is {}'.format(num, ' * '.join(str(n) for n in factors)), ''

     

    As an Indirect Result of User Interaction

    https://dash.plotly.com/advanced-callbacks#as-an-indirect-result-of-user-interaction 를 이해할 필요가 있다.

     

    callback graph 가 아래와 같을 때

    button_4 (5초가 걸리는 slow callback) 버튼 클릭 후 (slow callback 이 완료되기 전에) 바로 button_3 (fast callback) 을 클릭하면 어떻게 될까? 

    • button_4 (slow callback) 에 의해
      • 5초 뒤 second_output_3 업데이트 예정 
      • second_output_3 업데이트 예정 third_output_3 업데이트
    • button_3 (fast callback) 에 의해
      • first_output_3 즉시 업데이트 시도 --> 즉시 업데이트 됨
      • first_output_3 가 업데이트 되었으니 third_output_3 업데이트를 시도함
        • 그런데 앞의 slow callback 에서 이미 third_output_3 업데이트를 시도하고 있으며, 아직 완료되지 않았음
        • 결국 button_3 (fast callback) 에 의한 시도는 무시됨?

    'plotly dash' 카테고리의 다른 글

    Clientside Callbacks  (0) 2023.12.13
    admin (multi-page) 화면 개발  (0) 2023.07.15
    Interval  (0) 2022.06.14
    Tab  (0) 2022.06.14
    RangeSlider  (0) 2022.06.14

    댓글

Designed by Tistory.