A context manager that batches up trace and layout assignment operations into a singe plotly_update message that is executed when the context exits. Examples -------- For example, suppose we have a figure widget, `fig`, with a single trace.
(self)
| 3010 | # ---------------- |
| 3011 | @contextmanager |
| 3012 | def batch_update(self): |
| 3013 | """ |
| 3014 | A context manager that batches up trace and layout assignment |
| 3015 | operations into a singe plotly_update message that is executed when |
| 3016 | the context exits. |
| 3017 | |
| 3018 | Examples |
| 3019 | -------- |
| 3020 | For example, suppose we have a figure widget, `fig`, with a single |
| 3021 | trace. |
| 3022 | |
| 3023 | >>> import plotly.graph_objs as go |
| 3024 | >>> fig = go.FigureWidget(data=[{'y': [3, 4, 2]}]) |
| 3025 | |
| 3026 | If we want to update the xaxis range, the yaxis range, and the |
| 3027 | marker color, we could do so using a series of three property |
| 3028 | assignments as follows: |
| 3029 | |
| 3030 | >>> fig.layout.xaxis.range = [0, 5] |
| 3031 | >>> fig.layout.yaxis.range = [0, 10] |
| 3032 | >>> fig.data[0].marker.color = 'green' |
| 3033 | |
| 3034 | This will work, however it will result in three messages being |
| 3035 | sent to the front end (two relayout messages for the axis range |
| 3036 | updates followed by one restyle message for the marker color |
| 3037 | update). This can cause the plot to appear to stutter as the |
| 3038 | three updates are applied incrementally. |
| 3039 | |
| 3040 | We can avoid this problem by performing these three assignments in a |
| 3041 | `batch_update` context as follows: |
| 3042 | |
| 3043 | >>> with fig.batch_update(): |
| 3044 | ... fig.layout.xaxis.range = [0, 5] |
| 3045 | ... fig.layout.yaxis.range = [0, 10] |
| 3046 | ... fig.data[0].marker.color = 'green' |
| 3047 | |
| 3048 | Now, these three property updates will be sent to the frontend in a |
| 3049 | single update message, and they will be applied by the front end |
| 3050 | simultaneously. |
| 3051 | """ |
| 3052 | if self._in_batch_mode is True: |
| 3053 | yield |
| 3054 | else: |
| 3055 | try: |
| 3056 | self._in_batch_mode = True |
| 3057 | yield |
| 3058 | finally: |
| 3059 | # ### Disable batch mode ### |
| 3060 | self._in_batch_mode = False |
| 3061 | |
| 3062 | # ### Build plotly_update params ### |
| 3063 | ( |
| 3064 | restyle_data, |
| 3065 | relayout_data, |
| 3066 | trace_indexes, |
| 3067 | ) = self._build_update_params_from_batch() |
| 3068 | |
| 3069 | # ### Call plotly_update ### |