# Bokeh Breaks Randomly During Widget Change

**URL:** <https://discourse.holoviz.org/t/bokeh-breaks-randomly-during-widget-change/6024>\
**Category:** Panel\
**Tags:** bokeh, panel, holoviews\
**Created:** [September 14, 2023, 1:53am UTC](https://discourse.holoviz.org/t/bokeh-breaks-randomly-during-widget-change/6024 "2023-09-14T01:53:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![robml](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/robml/32/5187_2.png) [@robml](https://discourse.holoviz.org/u/robml)\
**Post date:** [September 14, 2023, 1:53am UTC](https://discourse.holoviz.org/t/bokeh-breaks-randomly-during-widget-change/6024/1 "2023-09-14T01:53:15Z")

</div>

I have been working with a multipage app (refer to [this](https://panel.holoviz.org/how_to/server/multiple.html) article in the documentation if you don’t understand what I mean). Each page has its own separate widgets which are effectively filters. When loading each of these pages and using these filters, it is almost as if the Bokeh gods decide if a filter will go through or if your server will break down.

Sometimes it is something quite reasonable (me saying this should give you an idea of how my experience with using Bokeh in non-traditional workflows has been) and I get an error like `RuntimeError: _pending_writes should be non-None when we have a document lock, and we should have the lock when the document changes`, to which all one has to do is just select the widget again and voila it works without any problem.

Sometimes, however, I get an error from Bokeh’s end, for something that has worked before that instead spits out `AttributeError: 'Document' object has no attribute '_theme'` and completely breaks the server, requiring a full shutdown and restart. For this example, by the way, all I did was `hv.render([ANY Holoviews plot])` and as it goes down the execution chain to eventually Bokeh it breaks on Bokeh’s end.

This is completely unacceptable for production apps which sure might break but not on every run, and certainly not after maybe a few minutes of use. I am not sure how to address these kinds of issues and was hoping someone more experienced in the Holoviz community can lend a hand.

EDIT:  
Something I forgot to add is that some of my Bokeh plots bound to a widget will eventually shrink to maybe a few pixels in height and stay that way no matter what widget value is chosen. Here is a small example, where the default value is using a “Day” interval:

 ![bokeh error pic1](https://discourse.holoviz.org/uploads/default/original/2X/a/a8014bc1a758c5370ef5cda00d4410ebc90fcd26.png)

I then select the “Month” one, so far so good:

 ![bokeh error pic2](https://discourse.holoviz.org/uploads/default/original/2X/d/d63d50be18918ca2d158c16b26cfa9e6405e9965.png)

Choosing exactly 1 more yields no problem, it can be any of the options. However, by the 4th time, I always get this:

 ![bokeh error pic3](https://discourse.holoviz.org/uploads/default/original/2X/d/d77d977d2ed6661f1abbc0c4eb6a3b1fe8d8a627.png)

I should note here that the title is actually a `pn.pane.HTML` for unrelated reasons, otherwise when it used to be part of the Bokeh plot it would disappear as well.

Now, for the life of my I cannot guess why this happens. To illustrate the above example and how odd it is, I have tried to store the chart by `pn.bind`-ing the widget to a `plot` function that would resample the dataset based on the value and return a `pn.pane.Bokeh` chart to be rendered. I would return the whole thing inside a `pn.Column` component. Later I figured it would be faster to have everything pre-rendered, so instead I set the values of the widget to the rendered `plot()` functions with the appropriate inputs, and instead changed my `pn.bind` from something like this `pn.bind(plot,widget)` to this `pn.bind(lambda x: x, widget)` since what widget would actually input was `widget.value` which was appropriately my plot.

Having tried these both and seeing it doesn’t necessarily have to do with the widget, but rather switching between Bokeh plots helps me realize this is actually a problem with Bokeh and not Panel. I have taken the time to raise this in Bokeh’s discourse but thought the Panel community would benefit from knowing such bugs exist. I unfortunately don’t know how to fix this, so if anyone has any advice it would greatly be appreciated.

---

<div class="post-metadata">

**Author:** ![cdeil](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/cdeil/32/4452_2.png) [@cdeil](https://discourse.holoviz.org/u/cdeil)\
**Post date:** [September 14, 2023, 7:17pm UTC](https://discourse.holoviz.org/t/bokeh-breaks-randomly-during-widget-change/6024/2 "2023-09-14T19:17:20Z")

</div>

Could you please link to the Bokeh forum post you mention?

---

<div class="post-metadata">

**Author:** ![robml](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/robml/32/5187_2.png) [@robml](https://discourse.holoviz.org/u/robml)\
**Post date:** [September 15, 2023, 1:51am UTC](https://discourse.holoviz.org/t/bokeh-breaks-randomly-during-widget-change/6024/3 "2023-09-15T01:51:02Z")

</div>

Sure thing, [here](https://discourse.bokeh.org/t/bokeh-breaks-during-widget-changes/10854) it is, although I will warn you there’s a lot repeated from this post.

---

<div class="post-metadata">

**Author:** ![Wiggan](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/wiggan/32/4016_2.png) [@Wiggan](https://discourse.holoviz.org/u/Wiggan)\
**Post date:** [September 15, 2023, 6:21am UTC](https://discourse.holoviz.org/t/bokeh-breaks-randomly-during-widget-change/6024/4 "2023-09-15T06:21:15Z")

</div>

This looks like the same error message I get randomly in my Panel Pipeline app. It started happening only on Ubuntu for me. Never seen it in windows.

---

<div class="post-metadata">

**Author:** ![ahuang11](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/ahuang11/32/4947_2.png) [@ahuang11](https://discourse.holoviz.org/u/ahuang11)\
**Post date:** [September 15, 2023, 4:44pm UTC](https://discourse.holoviz.org/t/bokeh-breaks-randomly-during-widget-change/6024/5 "2023-09-15T16:44:04Z")

</div>

> Sometimes, however, I get an error from Bokeh’s end, for something that has worked before that instead spits out `AttributeError: 'Document' object has no attribute '_theme'` and completely breaks the server, requiring a full shutdown and restart. For this example, by the way, all I did was `hv.render([ANY Holoviews plot])` and as it goes down the execution chain to eventually Bokeh it breaks on Bokeh’s end.

Can you provide a minimal example?

Also, to clarify, are you using Bokeh to serve or panel?

Other than that, maybe try setting a min height?

---

<div class="post-metadata">

**Author:** ![robml](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/robml/32/5187_2.png) [@robml](https://discourse.holoviz.org/u/robml)\
**Post date:** [September 16, 2023, 1:06am UTC](https://discourse.holoviz.org/t/bokeh-breaks-randomly-during-widget-change/6024/6 "2023-09-16T01:06:23Z")

</div>

Sure I will write the plotting code below. I am using `panel serve` for deployment, and I tried setting `min_height` prior to calling `hv.render`, however the issue remained and the server still crashed. However I noticed a hack-ish method that works, which I will detail below:

Assume our data to plot is a timeseries and in a pandas DataFrame `df`, then the plotting code would be:

```auto
def plot():
    chart = df.hvplot.line(by="Year",
                            color=colors,
                            xformatter=bm.formatters.DatetimeTickFormatter(months="%b %Y",days="%b %d"),
                            xlabel="",
                            ylabel="",
                            tools=[hover,"box_zoom","wheel_zoom","tap"],
                            legend="top",
                            ).opts(
                                margin=(10,10),
                                border=0,
                                active_tools=["pan"],
                                padding=0.01,
                            )
    return chart

chart = plot()
chart = hv.render(chart) #breaks here

```

Now THIS would break, however if I were to put this in a `try...except` block and repeat the exact same function call, it works most of the time (except when a user goes back and forth between pages too much):

```auto
def plot():
    ...
chart = plot()
try:
    chart = hv.render(plot) # throws Exception
except:
    chart = hv.render(plot) # passes without further Exception

```

After scouring the Bokeh forums, Github issues, and some of the Github code, it seems there MIGHT be a bug that causes this. I am not sure how to fix this, nor would I say I am that inclined to at this point but hope this post helps someone out in the future.

P.S. I also want to say that most of my plotting frustrations in Panel have only sometimes been with the Panel codebase itself, and mostly with Bokeh. For comparison, whenever I used Altair/Vega I never ran into these kinds of issues, despite some more constraints I would say the user experience can be significantly smoother at times.

---

<div class="post-metadata">

**Author:** ![cdeil](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/cdeil/32/4452_2.png) [@cdeil](https://discourse.holoviz.org/u/cdeil)\
**Post date:** [September 30, 2023, 9:51am UTC](https://discourse.holoviz.org/t/bokeh-breaks-randomly-during-widget-change/6024/7 "2023-09-30T09:51:35Z")

</div>

I filed a bug in the Panel issue tracker: [Bokeh figure axes collapse on dynamic creation · Issue #5561 · holoviz/panel · GitHub](https://github.com/holoviz/panel/issues/5561)
