# Callback debugging

**URL:** <https://community.plotly.com/t/callback-debugging/8001>\
**Category:** Dash Python\
**Created:** [January 28, 2018, 6:49am UTC](https://community.plotly.com/t/callback-debugging/8001 "2018-01-28T06:49:32Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![nedned](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nedned/32/8795_2.png) [@nedned](https://community.plotly.com/u/nedned)\
**Post date:** [January 28, 2018, 6:49am UTC](https://community.plotly.com/t/callback-debugging/8001/1 "2018-01-28T06:49:32Z")

</div>

With a change that went into Dash recently, it’s possible now to introspect details of registered callback functions. I’ve taken advantage of this to make a function which prints out the details of all registered callbacks:

```
    callback router @ router.py:25       
    Output dash-container.children     
    Inputs 1 url.pathname                
    States 0     
    Events 0     
                     

    callback update_nav @ router.py:34   
    Output navbar.children             
    Inputs 1 url.pathname                
    States 0     
    Events 0     
                     

    callback callback @ app.py:10        
    Output graph.figure                
    Inputs 3 normalize.value, sort-type.value, text-input.value                            
    States 0     
    Events 0     

```

This is particularly useful for larger apps where it can be harder to keep track of callbacks, especially if they are being created in bulk. It’s also helpful for debugging the callback logic, where the first thing I want to do when reading someone else code is work out what callbacks are defined and what their input and output targets are, before working out what they return. If this is something people found useful, I could put together a PR.

```python
def show_callbacks(app):

    def format_regs(registrations, padding=10):
        # TODO: -- switch to single line printing if > 79 chars                                                                                                                                
        vals = sorted("{}.{}".format(i['id'], i['property'])
                      for i in registrations)
        return ", ".join(vals)

    output_list = []

    for callback_id, callback in app.callback_map.items():
        wrapped_func = callback['callback']. __wrapped__
        inputs = callback['inputs']
        states = callback['state']
        events = callback['events']

        str_values = {
            'callback': wrapped_func. __name__ ,
            'output': callback_id,
            'filename': os.path.split(wrapped_func. __code__.co_filename)[-1],
            'lineno': wrapped_func. __code__.co_firstlineno,
            'num_inputs': len(inputs),
            'num_states': len(states),
            'num_events': len(events),
            'inputs': format_regs(inputs),
            'states': format_regs(states),
            'events': format_regs(events)
        }

        output = """                                                                                                                                                                           
        callback {callback} @ {filename}:{lineno}                                                                                                                                         
        Output {output}                                                                                                                                                                 
        Inputs {num_inputs:>4} {inputs}                                                                                                                                                      
        States {num_states:>4} {states}                                                                                                                                                      
        Events {num_events:>4} {events}                                                                                                                                                      
        """.format(**str_values)

        output_list.append(output)
    return "\n".join(output_list)

```

---

<div class="post-metadata">

**Author:** ![dbill](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@dbill](https://community.plotly.com/u/dbill)\
**Post date:** [February 7, 2018, 3:04pm UTC](https://community.plotly.com/t/callback-debugging/8001/2 "2018-02-07T15:04:55Z")

</div>

Thanks for posting this. I added this inside my dash app that has quite a few callbacks but get the following error when I run it. ‘function’ object has no attribute ‘ **wrapped** ’

Any ideas on what is wrong?

---

<div class="post-metadata">

**Author:** ![nedned](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nedned/32/8795_2.png) [@nedned](https://community.plotly.com/u/nedned)\
**Post date:** [February 7, 2018, 3:10pm UTC](https://community.plotly.com/t/callback-debugging/8001/3 "2018-02-07T15:10:51Z")

</div>

Ah the change to Dash necessary to support this has not yet gone into the latest release. If you’re keen to access this now you can install the latest version from github or just manually patch [dash.py](http://dash.py) as per this [diff](https://github.com/plotly/dash/commit/9a4c50c415d77f68f4b19ab7e25836b8e5c3683a#diff-228c9aefac729977642672aa1416baca), as it’s only a two line change.

---

<div class="post-metadata">

**Author:** ![dbill](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@dbill](https://community.plotly.com/u/dbill)\
**Post date:** [February 7, 2018, 4:09pm UTC](https://community.plotly.com/t/callback-debugging/8001/4 "2018-02-07T16:09:59Z")

</div>

Thanks very much for the info. I’ll give it a try.

---

<div class="post-metadata">

**Author:** ![nicolaskruchten](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nicolaskruchten/32/7018_2.png) [@nicolaskruchten](https://community.plotly.com/u/nicolaskruchten)\
**Post date:** [March 5, 2018, 8:13pm UTC](https://community.plotly.com/t/callback-debugging/8001/5 "2018-03-05T20:13:03Z")

</div>

Nice job @nedned ! I took this function and wired it up to Graphvis to make this little Dash component that shows you the callback chain in visual form: [https://github.com/nicolaskruchten/dash\_callback\_chain](https://github.com/nicolaskruchten/dash_callback_chain)

here’s a sample:

[![](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/6/6de262edcd5a46acb3eacbe80dbd797e50cc9124.png) ](https://raw.githubusercontent.com/nicolaskruchten/dash_callback_chain/master/example.png)

---

<div class="post-metadata">

**Author:** ![dbill](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@dbill](https://community.plotly.com/u/dbill)\
**Post date:** [April 11, 2018, 5:15pm UTC](https://community.plotly.com/t/callback-debugging/8001/6 "2018-04-11T17:15:44Z")

</div>

This is very cool. I like it.

---

<div class="post-metadata">

**Author:** ![OliverBrace](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/oliverbrace/32/4039_2.png) [@OliverBrace](https://community.plotly.com/u/OliverBrace)\
**Post date:** [August 15, 2018, 3:52pm UTC](https://community.plotly.com/t/callback-debugging/8001/7 "2018-08-15T15:52:40Z")

</div>

This is insanely cool

---

<div class="post-metadata">

**Author:** ![SashaFin](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@SashaFin](https://community.plotly.com/u/SashaFin)\
**Post date:** [December 18, 2018, 12:05pm UTC](https://community.plotly.com/t/callback-debugging/8001/8 "2018-12-18T12:05:33Z")

</div>

> [@nedned](#):
>
> ge to Dash necessary to support this has not yet gone into the latest release. If you’re keen to

Awsome. really helpful

---

<div class="post-metadata">

**Author:** ![nedned](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nedned/32/8795_2.png) [@nedned](https://community.plotly.com/u/nedned)\
**Post date:** [March 25, 2019, 5:29am UTC](https://community.plotly.com/t/callback-debugging/8001/9 "2019-03-25T05:29:24Z")

</div>

There’s been a few changes to Dash since I posted this snippet, notably including the removal of Events and the addition of multi outputs. Here’s an updated version of this function that handles these changes:

```python
def show_callbacks(app):

    def wrap_list(items, padding=24):
        return ("\n"+" "*padding).join(items)

    def format_regs(registrations):
        vals = sorted("{}.{}".format(i['id'], i['property'])
                      for i in registrations)
        return wrap_list(vals)

    output_list = []

    for callback_id, callback in app.callback_map.items():
        wrapped_func = callback["callback"]. __wrapped__
        inputs = callback["inputs"]
        states = callback["state"]

        if callback_id.startswith(".."):
            outputs = callback_id.strip(".").split("...")
        else:
            outputs = [callback_id]

        str_values = {
            "callback": wrapped_func. __name__ ,
            "outputs": wrap_list(outputs),
            "filename": os.path.split(wrapped_func. __code__.co_filename)[-1],
            "lineno": wrapped_func. __code__.co_firstlineno,
            "num_inputs": len(inputs),
            "num_states": len(states),
            "inputs": format_regs(inputs),
            "states": format_regs(states),
            "num_outputs": len(outputs),
        }

        output = dedent(
            """                                                                                                                                                                                                      
            callback {callback} @ {filename}:{lineno}                                                                                                                                                             
            Outputs{num_outputs:>3} {outputs}                                                                                                                                                                       
            Inputs{num_inputs:>4} {inputs}                                                                                                                                                                          
            States{num_states:>4} {states}                                                                                                                                                                          
            """.format(**str_values)
        )

        output_list.append(output)
    return "\n".join(output_list)

```

---

<div class="post-metadata">

**Author:** ![mbkupfer](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/mbkupfer/32/4334_2.png) [@mbkupfer](https://community.plotly.com/u/mbkupfer)\
**Post date:** [April 16, 2020, 5:09pm UTC](https://community.plotly.com/t/callback-debugging/8001/10 "2020-04-16T17:09:46Z")

</div>

@nedned I’m curious, have you come up with a witty solution on using an interactive python debugger in callbacks?

Since dash is threaded, there is no way for me to set a trace and jump into an interactive session where I can inspect the code.

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [April 16, 2020, 5:22pm UTC](https://community.plotly.com/t/callback-debugging/8001/11 "2020-04-16T17:22:22Z")

</div>

> [@mbkupfer](#):
>
> Since dash is threaded, there is no way for me to set a trace and jump into an interactive session where I can inspect the code.

Using `ipdb` has worked for me:  
`import ipdb; ipdb.set_trace()`

Also, the debugger in VSCode has also worked for me.

 ![stack](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/2/2a1cda08a0a2182bc7b04bb3cf42e2c36c5b5ee1.gif)

---

<div class="post-metadata">

**Author:** ![mbkupfer](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/mbkupfer/32/4334_2.png) [@mbkupfer](https://community.plotly.com/u/mbkupfer)\
**Post date:** [April 16, 2020, 5:31pm UTC](https://community.plotly.com/t/callback-debugging/8001/12 "2020-04-16T17:31:31Z")

</div>

Of course it now works for me, but most the time, me setting a trace in a running dash app results in the program getting hung up. (I’ll try to post an example next time I run into that)

Has this never happened to you?

---

<div class="post-metadata">

**Author:** ![nedned](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nedned/32/8795_2.png) [@nedned](https://community.plotly.com/u/nedned)\
**Post date:** [April 27, 2020, 1:07pm UTC](https://community.plotly.com/t/callback-debugging/8001/13 "2020-04-27T13:07:55Z")

</div>

I have seen this before, but I think only when running with multiple workers or threads. Definitely make sure you’re using a single thread/worker when you run your app for debugging.

I usually have a command set up to run Dash in prod mode (eg with gunicorn and multiple workers) and a command to run for debugging (eg using Flask dev server). If you want to debug your prod environment while running with gunicorn etc you need to make sure you’re using a single worker/thread and also disable/extend the request timeout, otherwise the WSGI server will kill and respawn the worker.

---

<div class="post-metadata">

**Author:** ![mbkupfer](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/mbkupfer/32/4334_2.png) [@mbkupfer](https://community.plotly.com/u/mbkupfer)\
**Post date:** [April 27, 2020, 7:14pm UTC](https://community.plotly.com/t/callback-debugging/8001/14 "2020-04-27T19:14:52Z")

</div>

Yes, when I pass threaded=False to `run_server` it now works fine.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Akshay91](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Akshay91](https://community.plotly.com/u/Akshay91)\
**Post date:** [November 12, 2022, 3:29am UTC](https://community.plotly.com/t/callback-debugging/8001/15 "2022-11-12T03:29:56Z")

</div>

I didn’t understood. Can you explain, how to use ipdb to debugg Dash callback function?
