Skip to content

Commit

Permalink
Demonstrate lazy tabs (#2479)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jul 2, 2021
1 parent 56f72d5 commit 9485449
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
33 changes: 33 additions & 0 deletions examples/reference/layouts/Tabs.ipynb
Expand Up @@ -140,6 +140,39 @@
"tabs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want the `Tabs` to be completely lazy when rendering some output you can leverage a [ParamFunction or ParamMethod](../../user_guide/Param.ipynb) to ensure that the output is not computed until you navigate to the tab:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import numpy as np\n",
"\n",
"def plot():\n",
" time.sleep(1) # some long running calculation\n",
" np.random.seed(tabs.active)\n",
" xs, ys = np.random.randn(2, 100)\n",
" p = figure(width=300, height=300, name=f'Scatter Seed {tabs.active}')\n",
" p.scatter(xs, ys)\n",
" return p\n",
"\n",
"p1 = pn.param.ParamFunction(plot, lazy=True, name='Seed 0')\n",
"p2 = pn.param.ParamFunction(plot, lazy=True, name='Seed 1')\n",
"p3 = pn.param.ParamFunction(plot, lazy=True, name='Seed 2')\n",
"\n",
"tabs = pn.Tabs(p1, p2, p3, dynamic=True)\n",
"\n",
"tabs"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
10 changes: 7 additions & 3 deletions panel/param.py
Expand Up @@ -825,8 +825,8 @@ class ParamFunction(ParamMethod):
priority = 0.6

def _link_object_params(self):
deps = self.object._dinfo
dep_params = list(deps['dependencies']) + list(deps.get('kw', {}).values())
deps = getattr(self.object, '_dinfo', {})
dep_params = list(deps.get('dependencies', [])) + list(deps.get('kw', {}).values())
grouped = defaultdict(list)
for dep in dep_params:
grouped[id(dep.owner)].append(dep)
Expand All @@ -846,7 +846,11 @@ def _link_object_params(self):

@classmethod
def applies(cls, obj):
return isinstance(obj, types.FunctionType) and hasattr(obj, '_dinfo')
if isinstance(obj, types.FunctionType):
if hasattr(obj, '_dinfo'):
return True
return None
return False


class JSONInit(param.Parameterized):
Expand Down

0 comments on commit 9485449

Please sign in to comment.