# Buggy code
def refresh(self, **kwargs):
    """
    Refreshes the plot by rerendering it and then pushing
    the updated data if the plot has an associated Comm.
    """
    if self.renderer.mode == "server":
        from bokeh.io import curdoc

        thread = threading.current_thread()
        thread_id = thread.ident if thread else None
        if curdoc() is not self.document or (
            state._thread_id is not None and thread_id != state._thread_id
        ):
            # If we do not have the Document lock, schedule refresh as callback
            self._triggering += [
                s
                for p in self.traverse(lambda x: x, [Plot])
                for s in getattr(p, "streams", [])
                if s._triggering
            ]
            if self.document.session_context:
                self.document.add_next_tick_callback(self.refresh)
                return

    # Ensure that server based tick callbacks maintain stream triggering state
    for s in self._triggering:
        s._triggering = True
    try:
        traverse_setter(self, "_force", True)
        key = self.current_key if self.current_key else self.keys[0]
        dim_streams = [
            stream
            for stream in self.streams
            if any(c in self.dimensions for c in stream.contents)
        ]
        stream_params = stream_parameters(dim_streams)
        key = tuple(
            None if d in stream_params else k for d, k in zip(self.dimensions, key)
        )
        stream_key = util.wrap_tuple_streams(key, self.dimensions, self.streams)

        self._trigger_refresh(stream_key)
        if self.top_level:
            self.push()
    except Exception as e:
        raise e
    finally:
        # Reset triggering state
        for s in self._triggering:
            s._triggering = False
        self._triggering = []


# Traceback (AttributeError)
bokeh.application.application - ERROR - Error running application handler <bokeh.application.handlers.script.ScriptHandler object at 0x7f58c595dcd0>: 'NoneType' object has no attribute 'session_context'
File "plot.py", line 218, in refresh:
if self.document.session_context: Traceback (most recent call last):
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/bokeh/application/handlers/code_runner.py", line 197, in run
exec(self._code, module.__dict__)
File "/home/user/Documents/panel_viz/bin/minimal_example.py", line 25, in <module>
doc = renderer.server_doc(datashaded_plot)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/holoviews/plotting/renderer.py", line 466, in server_doc
return obj.layout.server_doc(doc)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/panel/viewable.py", line 600, in server_doc
model = self.get_root(doc)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/panel/viewable.py", line 536, in get_root
root = self._get_model(doc, comm=comm)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/panel/layout.py", line 136, in _get_model
objects = self._get_objects(model, [], doc, root, comm)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/panel/layout.py", line 126, in _get_objects
child = pane._get_model(doc, root, model, comm)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/panel/pane/holoviews.py", line 227, in _get_model
plot = self._render(doc, comm, root)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/panel/pane/holoviews.py", line 286, in _render
return renderer.get_plot(self.object, **kwargs)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/holoviews/plotting/bokeh/renderer.py", line 73, in get_plot
plot = super(BokehRenderer, self_or_cls).get_plot(obj, doc, renderer, **kwargs)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/holoviews/plotting/renderer.py", line 223, in get_plot
plot.update(init_key)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/holoviews/plotting/plot.py", line 878, in update
return self.initialize_plot()
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/holoviews/plotting/bokeh/element.py", line 1334, in initialize_plot
Stream.trigger(trigger)
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/holoviews/streams.py", line 169, in trigger
subscriber(**dict(union))
File "/home/user/anaconda3/envs/bokeh-dev/lib/python3.7/site-packages/holoviews/plotting/plot.py", line 218, in refresh
if self.document.session_context:
AttributeError: 'NoneType' object has no attribute 'session_context'

# Fixed code
def refresh(self, **kwargs):
    """
    Refreshes the plot by rerendering it and then pushing
    the updated data if the plot has an associated Comm.
    """
    if self.renderer.mode == "server":
        from bokeh.io import curdoc

        thread = threading.current_thread()
        thread_id = thread.ident if thread else None
        if curdoc() is not self.document or (
            state._thread_id is not None and thread_id != state._thread_id
        ):
            # If we do not have the Document lock, schedule refresh as callback
            self._triggering += [
                s
                for p in self.traverse(lambda x: x, [Plot])
                for s in getattr(p, "streams", [])
                if s._triggering
            ]
            if self.document and self.document.session_context:
                self.document.add_next_tick_callback(self.refresh)
                return

    # Ensure that server based tick callbacks maintain stream triggering state
    for s in self._triggering:
        s._triggering = True
    try:
        traverse_setter(self, "_force", True)
        key = self.current_key if self.current_key else self.keys[0]
        dim_streams = [
            stream
            for stream in self.streams
            if any(c in self.dimensions for c in stream.contents)
        ]
        stream_params = stream_parameters(dim_streams)
        key = tuple(
            None if d in stream_params else k for d, k in zip(self.dimensions, key)
        )
        stream_key = util.wrap_tuple_streams(key, self.dimensions, self.streams)

        self._trigger_refresh(stream_key)
        if self.top_level:
            self.push()
    except Exception as e:
        raise e
    finally:
        # Reset triggering state
        for s in self._triggering:
            s._triggering = False
        self._triggering = []