def _init_doc(self, doc=None, comm=None, title=None, notebook=False, location=True): doc = doc or _curdoc() title = title or 'Panel Application' if location and self.location: loc = self._add_location(doc, location) doc.on_session_destroyed(loc._server_destroy) doc.title = title # Initialize fake root. This is needed to ensure preprocessors # which assume that all models are owned by a single root can # link objects across multiple roots in a template. col = Column() preprocess_root = col.get_root(doc, comm) col._hooks.append(self._apply_hooks) ref = preprocess_root.ref['id'] objs, models = [], [] for name, (obj, tags) in self._render_items.items(): if self._apply_hooks not in obj._hooks: obj._hooks.append(self._apply_hooks) # We skip preprocessing on the individual roots model = obj.get_root(doc, comm, preprocess=False) mref = model.ref['id'] doc.on_session_destroyed(obj._server_destroy) for sub in obj.select(Viewable): submodel = sub._models.get(mref) if submodel is None: continue sub._models[ref] = submodel if isinstance(sub, HoloViews) and mref in sub._plots: sub._plots[ref] = sub._plots.get(mref) obj._documents[doc] = model model.name = name model.tags = tags self._apply_root(name, model, tags) add_to_doc(model, doc, hold=bool(comm)) objs.append(obj) models.append(model) # Here we ensure that the preprocessor is run across all roots # and set up session cleanup hooks for the fake root. state._fake_roots.append(ref) # Ensure no update is run state._views[ref] = (col, preprocess_root, doc, comm) col.objects = objs preprocess_root.children[:] = models col._preprocess(preprocess_root) col._documents[doc] = preprocess_root doc.on_session_destroyed(col._server_destroy) if notebook: doc.template = self.nb_template else: doc.template = self.template doc._template_variables.update(self._render_variables) return doc
def server_doc(self, doc=None, title=None): """ Returns a serveable bokeh Document with the panel attached Arguments --------- doc : bokeh.Document (optional) The bokeh Document to attach the panel to as a root, defaults to bokeh.io.curdoc() title : str A string title to give the Document Returns ------- doc : bokeh.Document The bokeh document the panel was attached to """ doc = doc or _curdoc() if title is not None: doc.title = title model = self.get_root(doc) if hasattr(doc, 'on_session_destroyed'): doc.on_session_destroyed(self._server_destroy) self._documents[doc] = model add_to_doc(model, doc) return doc
def _init_doc(self, doc=None, comm=None, title=None, notebook=False): doc = doc or _curdoc() title = title or 'Panel Application' doc.title = title col = Column() preprocess_root = col.get_root(doc, comm) ref = preprocess_root.ref['id'] for name, (obj, tags) in self._render_items.items(): model = obj.get_root(doc, comm) doc.on_session_destroyed(obj._server_destroy) for sub in obj.select(Viewable): sub._models[ref] = sub._models.get(model.ref['id']) if isinstance(sub, HoloViews): sub._plots[ref] = sub._plots.get(model.ref['id']) col.objects.append(obj) obj._documents[doc] = model model.name = name model.tags = tags add_to_doc(model, doc, hold=bool(comm)) state._views[ref] = (col, preprocess_root, doc, comm) col._preprocess(preprocess_root) col._documents[doc] = preprocess_root doc.on_session_destroyed(col._server_destroy) if notebook: doc.template = self.nb_template else: doc.template = self.template doc._template_variables.update(self._render_variables) return doc
def server_doc(self, doc=None, title=None): """ Returns a servable bokeh Document with the panel attached Arguments --------- doc : bokeh.Document (optional) The Bokeh Document to attach the panel to as a root, defaults to bokeh.io.curdoc() title : str A string title to give the Document Returns ------- doc : bokeh.Document The Bokeh document the panel was attached to """ doc = doc or _curdoc() if title is not None: doc.title = title for name, obj in self._render_items.items(): model = obj.get_root(doc) model.name = name if hasattr(doc, 'on_session_destroyed'): doc.on_session_destroyed(obj._server_destroy) obj._documents[doc] = model add_to_doc(model, doc) doc.template = self.template return doc
def _init_doc(self, doc=None, comm=None, title=None, notebook=False): doc = doc or _curdoc() if title is not None: doc.title = title root = None preprocess_root = _BkRow() ref = preprocess_root.ref['id'] for name, (obj, tags) in self._render_items.items(): if root is None: root = model = obj.get_root(doc, comm) elif isinstance(obj, PaneBase): if obj._updates: model = obj._get_model(doc, root, root, comm=comm) else: model = obj.layout._get_model(doc, root, root, comm=comm) else: model = obj._get_model(doc, root, root, comm) obj._models[ref] = obj._models[root.ref['id']] preprocess_root.children.append(model) model.name = name model.tags = tags if hasattr(doc, 'on_session_destroyed'): doc.on_session_destroyed(obj._server_destroy) obj._documents[doc] = model add_to_doc(model, doc, hold=bool(comm)) for (obj, _) in self._render_items.values(): obj._preprocess(preprocess_root) if notebook: doc.template = self.nb_template else: doc.template = self.template return doc
def on_session_destroyed(self, callback): """ Callback that is triggered when a session is destroyed. """ doc = self._curdoc or _curdoc() if doc: doc.on_session_destroyed(callback) else: raise RuntimeError( "Could not add session destroyed callback since no " "document to attach it to could be found.")
def curdoc(self): if self._curdoc: return self._curdoc try: doc = _curdoc() except Exception: return None try: if doc.session_context: return doc except Exception: return None
def servable(self): """ Serves the object if in a `panel serve` context and returns the panel object to allow it to display itself in a notebook context. Returns ------- The Panel object itself """ if _curdoc().session_context: self.server_doc() return self
def _init_doc(self, doc=None, comm=None, title=None, notebook=False, location=True): doc = doc or _curdoc() title = title or 'Panel Application' doc.title = title col = Column() preprocess_root = col.get_root(doc, comm) ref = preprocess_root.ref['id'] for name, (obj, tags) in self._render_items.items(): model = obj.get_root(doc, comm) mref = model.ref['id'] doc.on_session_destroyed(obj._server_destroy) for sub in obj.select(Viewable): sub._models[ref] = sub._models.get(mref) if isinstance(sub, HoloViews) and mref in sub._plots: sub._plots[ref] = sub._plots.get(mref) col.objects.append(obj) obj._documents[doc] = model model.name = name model.tags = tags for o in obj.select(): self._apply_modifiers(o, mref) add_to_doc(model, doc, hold=bool(comm)) state._fake_roots.append(ref) state._views[ref] = (col, preprocess_root, doc, comm) if location: from ..io.location import Location if isinstance(location, Location): loc = location elif doc in state._locations: loc = state.location else: loc = Location() state._locations[doc] = loc loc_model = loc._get_model(doc, preprocess_root) loc_model.name = 'location' #doc.add_root(loc_model) col._preprocess(preprocess_root) col._documents[doc] = preprocess_root doc.on_session_destroyed(col._server_destroy) if notebook: doc.template = self.nb_template else: doc.template = self.template doc._template_variables.update(self._render_variables) return doc
def _init_doc(self, doc=None, comm=None, title=None, notebook=False, location=True): doc = doc or _curdoc() title = title or 'Panel Application' doc.title = title col = Column() preprocess_root = col.get_root(doc, comm) ref = preprocess_root.ref['id'] for name, (obj, tags) in self._render_items.items(): if self._apply_hooks not in obj._hooks: obj._hooks.append(self._apply_hooks) model = obj.get_root(doc, comm) mref = model.ref['id'] doc.on_session_destroyed(obj._server_destroy) for sub in obj.select(Viewable): submodel = sub._models.get(mref) if submodel is None: continue sub._models[ref] = submodel if isinstance(sub, HoloViews) and mref in sub._plots: sub._plots[ref] = sub._plots.get(mref) col.objects.append(obj) obj._documents[doc] = model model.name = name model.tags = tags self._apply_root(name, model, tags) add_to_doc(model, doc, hold=bool(comm)) state._fake_roots.append(ref) state._views[ref] = (col, preprocess_root, doc, comm) col._preprocess(preprocess_root) col._documents[doc] = preprocess_root doc.on_session_destroyed(col._server_destroy) if notebook: doc.template = self.nb_template else: doc.template = self.template doc._template_variables.update(self._render_variables) doc._template_variables['template_css_files'] = css_files = ( doc._template_variables.get('template_css_files', [])) for cssf in self._css_files: css_files.append(str(cssf)) return doc
def servable(self, title=None): """ Serves the object if in a `panel serve` context and returns the panel object to allow it to display itself in a notebook context. Arguments --------- title : str A string title to give the Document (if served as an app) Returns ------- The Panel object itself """ if _curdoc().session_context: self.server_doc(title=title) return self
def servable(self, title=None): """ Serves the object if in a `panel serve` context and returns the Panel object to allow it to display itself in a notebook context. Arguments --------- title : str A string title to give the Document (if served as an app) Returns ------- The Panel object itself """ if _curdoc().session_context: logger = logging.getLogger('bokeh') for handler in logger.handlers: if isinstance(handler, logging.StreamHandler): handler.setLevel(logging.WARN) self.server_doc(title=title) return self
def get_root(self, doc=None, comm=None): """ Returns the root model and applies pre-processing hooks Arguments --------- doc: bokeh.Document Bokeh document the bokeh model will be attached to. comm: pyviz_comms.Comm Optional pyviz_comms when working in notebook Returns ------- Returns the bokeh model corresponding to this panel object """ doc = doc or _curdoc() root = self.layout.get_root(doc, comm) ref = root.ref['id'] self._models[ref] = (root, None) state._views[ref] = (self, root, doc, comm) return root
def server_doc(self, doc=None, title=None, location=True): """ Returns a serveable bokeh Document with the panel attached Arguments --------- doc : bokeh.Document (optional) The bokeh Document to attach the panel to as a root, defaults to bokeh.io.curdoc() location : boolean or panel.io.location.Location Whether to create a Location component to observe and set the URL location. title : str A string title to give the Document Returns ------- doc : bokeh.Document The bokeh document the panel was attached to """ from .io.location import Location doc = doc or _curdoc() title = title or 'Panel Application' doc.title = title model = self.get_root(doc) if hasattr(doc, 'on_session_destroyed'): doc.on_session_destroyed(self._server_destroy) self._documents[doc] = model add_to_doc(model, doc) if location: if isinstance(location, Location): loc = location elif state._locations.get(doc) is not None: loc = state._locations[doc] else: loc = Location() state._locations[doc] = loc loc_model = loc._get_model(doc, model) doc.add_root(loc_model) return doc
def servable(self, title=None, location=True, area='main'): """ Serves the object if in a `panel serve` context and returns the Panel object to allow it to display itself in a notebook context. Arguments --------- title : str A string title to give the Document (if served as an app) location : boolean or panel.io.location.Location Whether to create a Location component to observe and set the URL location. area: str The area of a template to add the component too. Only has an effect if pn.config.template has been set. Returns ------- The Panel object itself """ if _curdoc().session_context: logger = logging.getLogger('bokeh') for handler in logger.handlers: if isinstance(handler, logging.StreamHandler): handler.setLevel(logging.WARN) if config.template: template = state.template if template.title == template.param.title.default and title: template.title = title if area == 'main': template.main.append(self) elif area == 'sidebar': template.sidebar.append(self) elif area == 'modal': template.modal.append(self) elif area == 'header': template.header.append(self) else: self.server_doc(title=title, location=location) return self
def _init_doc(self, doc=None, comm=None, title=None, notebook=False): doc = doc or _curdoc() if title is not None: doc.title = title root = None col = Column() preprocess_root = col.get_root(doc, comm) ref = preprocess_root.ref['id'] for name, (obj, tags) in self._render_items.items(): if root is None: root = model = obj.get_root(doc, comm) elif isinstance(obj, PaneBase): if obj._updates: model = obj._get_model(doc, root, root, comm=comm) else: model = obj.layout._get_model(doc, root, root, comm=comm) else: model = obj._get_model(doc, root, root, comm) for sub in obj.select(Viewable): sub._models[ref] = sub._models.get(root.ref['id']) obj._documents[doc] = root doc.on_session_destroyed(obj._server_destroy) col.append(obj) model.name = name model.tags = tags add_to_doc(model, doc, hold=bool(comm)) state._views[ref] = (col, preprocess_root, doc, comm) col._preprocess(preprocess_root) col._documents[doc] = preprocess_root doc.on_session_destroyed(col._server_destroy) if notebook: doc.template = self.nb_template else: doc.template = self.template doc._template_variables.update(self._render_variables) return doc
def stop(self): """ Stops running the periodic callback. """ if self.running: try: self._updating = True self.running = False finally: self._updating = False self._counter = 0 self._timeout = None if self._doc: self._doc.remove_periodic_callback(self._cb) elif self._cb: self._cb.stop() self._cb = None doc = self._doc or _curdoc() if doc: doc.session_destroyed_callbacks = { cb for cb in doc.session_destroyed_callbacks if cb is not self._cleanup } self._doc = None
def servable(self, title=None, location=True): """ Serves the object if in a `panel serve` context and returns the Panel object to allow it to display itself in a notebook context. Arguments --------- title : str A string title to give the Document (if served as an app) location : boolean or panel.io.location.Location Whether to create a Location component to observe and set the URL location. Returns ------- The Panel object itself """ if _curdoc().session_context: logger = logging.getLogger('bokeh') for handler in logger.handlers: if isinstance(handler, logging.StreamHandler): handler.setLevel(logging.WARN) self.server_doc(title=title, location=True) return self
def curdoc(self): if self._curdoc: return self._curdoc elif _curdoc().session_context: return _curdoc()