Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate lazy tabs #2479

Merged
merged 1 commit into from Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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