# Don't understand callback context documentation

**URL:** <https://community.plotly.com/t/dont-understand-callback-context-documentation/72259>\
**Category:** Dash Python\
**Created:** [February 3, 2023, 5:20am UTC](https://community.plotly.com/t/dont-understand-callback-context-documentation/72259 "2023-02-03T05:20:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ash](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ash](https://community.plotly.com/u/ash)\
**Post date:** [February 3, 2023, 5:20am UTC](https://community.plotly.com/t/dont-understand-callback-context-documentation/72259/1 "2023-02-03T05:20:41Z")

</div>

> there is a global variable `dash.callback_context` , available only inside a callback  
> from [Advanced Callbacks | Dash for Python Documentation | Plotly](https://dash.plotly.com/advanced-callbacks)

how is it a global variable if it’s only available inside a callback?

Please advise if the heading is relevant

---

<div class="post-metadata">

**Author:** ![jcuypers](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jcuypers](https://community.plotly.com/u/jcuypers)\
**Post date:** [February 3, 2023, 5:30am UTC](https://community.plotly.com/t/dont-understand-callback-context-documentation/72259/2 "2023-02-03T05:30:46Z")

</div>

Hi,

Please refer to the following link (on top) which has some context and examples of usage

> [@mega Dash 2.4 Released - Improved Callback Context, Clientside Callback Promises, Typescript Components, Minor Ticks, and More!](https://community.plotly.com/t/dash-2-4-released-improved-callback-context-clientside-callback-promises-typescript-components-minor-ticks-and-more/64203):
>
> Update : [version 2.5.0 has been released](https://community.plotly.com/t/dash-2-5-released-easier-multi-page-apps-component-properties/64925) since this was posted. Good day Community Members, rocket We’re excited to announce that Dash 2.4.0 & 2.4.1 have been released. 2.4.0 contains the big feature items and 2.4.1 contains a quick bugfix. These are a backwards compatible releases and we recommend going straight to 2.4.1. pip install dash==2.4.1 Official Changelog arrow_forward [Dash v2.4.0](https://github.com/plotly/dash/releases/tag/v2.4.0)Highlights arrow_down_smallImproved Callback Contextwriting_hand [Improved callback\_co…](https://dash.plotly.com/advanced-callbacks#properties-for-callback_context)

---

<div class="post-metadata">

**Author:** ![jcuypers](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jcuypers](https://community.plotly.com/u/jcuypers)\
**Post date:** [February 3, 2023, 6:05am UTC](https://community.plotly.com/t/dont-understand-callback-context-documentation/72259/3 "2023-02-03T06:05:41Z")

</div>

Regarding it being a global variable, this might well be the case, but I assume that it will only be set  
and have a valid state when entering a callback

---

<div class="post-metadata">

**Author:** ![Tobs](https://avatars.discourse-cdn.com/v4/letter/t/6de8d8/32.png) [@Tobs](https://community.plotly.com/u/Tobs)\
**Post date:** [February 3, 2023, 9:08am UTC](https://community.plotly.com/t/dont-understand-callback-context-documentation/72259/4 "2023-02-03T09:08:08Z")

</div>

Codewise, if you look into Dash source code, it is enforced that dash.callback\_context can only be used inside a callback. Check the [`has_context` decorator](https://github.com/plotly/dash/blob/8ffb33163a861c7a4c7f1b645e6dec4cc6ca02e2/dash/_callback_context.py#L16).

The biggest magic behind the callback context happens in [`context_value`](https://github.com/plotly/dash/blob/8ffb33163a861c7a4c7f1b645e6dec4cc6ca02e2/dash/_callback_context.py#L12) variable. I didn’t dive in the source code behind that object, but this is how the proper context is derived.

What I can see is that calling `dash.callback_context` outside of a callback might have worked perfectly fine, if Dash would have allowed you to do that. However, Dash will raise an `exceptions.MissingCallbackContextException` error that explicitly prevents you from doing this.

I don’t know the magic to how the context is set, but if you want to understand better, my suggestion is to start looking at the [`contextvars` library](https://docs.python.org/3/library/contextvars.html).
