Skip to content

Commit

Permalink
Various optimizations (#2096)
Browse files Browse the repository at this point in the history
* Add roots to preprocessing route all at once

* Optimize Card header updates
  • Loading branch information
philippjfr committed Apr 8, 2021
1 parent 238199f commit a447e81
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 13 additions & 4 deletions panel/layout/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self, *objects, **params):
self._header_layout = Row(css_classes=['card-header-row'],
sizing_mode='stretch_width')
super().__init__(*objects, **params)
self._header = None
self.param.watch(self._update_header, ['title', 'header', 'title_css_classes'])
self._update_header()

Expand All @@ -76,12 +77,20 @@ def _process_param_change(self, params):
def _update_header(self, *events):
from ..pane import HTML, panel
if self.header is None:
item = HTML('%s' % (self.title or "​"),
css_classes=self.title_css_classes,
sizing_mode='stretch_width',
margin=(2, 5))
params = {
'object': '%s' % (self.title or "​"),
'css_classes': self.title_css_classes
}
if self._header is not None:
self._header.param.set_param(**params)
return
else:
self._header = item = HTML(
sizing_mode='stretch_width', margin=(2, 5), **params
)
else:
item = panel(self.header)
self._header = None
self._header_layout[:] = [item]

def _get_objects(self, model, old_objects, doc, root, comm=None):
Expand Down
4 changes: 3 additions & 1 deletion panel/template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def _init_doc(self, doc=None, comm=None, title=None, notebook=False, location=Tr
preprocess_root = col.get_root(doc, comm)
col._hooks.append(self._apply_hooks)
ref = preprocess_root.ref['id']
objs = []

for name, (obj, tags) in self._render_items.items():
if self._apply_hooks not in obj._hooks:
Expand All @@ -163,17 +164,18 @@ def _init_doc(self, doc=None, comm=None, title=None, notebook=False, location=Tr
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))
objs.append(obj)

# 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)
state._views[ref] = (col, preprocess_root, doc, comm)
col.objects = objs
col._preprocess(preprocess_root)
col._documents[doc] = preprocess_root
doc.on_session_destroyed(col._server_destroy)
Expand Down

0 comments on commit a447e81

Please sign in to comment.