Best practice for menu and lazy loading

Hi All… can you help with the best practices for setting up a menu in Panel where the dashboard is loaded ONLY when that particular menu item is selected/clicked? I have currently set up a tab based page but that loads all the dashboards in question and that’s a huge hit on front-end performance as well as the server load.

1 Like

Hi @chrisj

Have you tried using the dynamic=True argument of Tabs? That will only load the tab displayed.

Tabs Reference Guide

1 Like

More specifically try lazy ParamFunction approach outlined in the Tabs reference guide:

import time
import numpy as np

def plot():
    time.sleep(1) # some long running calculation
    np.random.seed(tabs.active)
    xs, ys = np.random.randn(2, 100)
    p = figure(width=300, height=300, name=f'Scatter Seed {tabs.active}')
    p.scatter(xs, ys)
    return p

p1 = pn.param.ParamFunction(plot, lazy=True, name='Seed 0')
p2 = pn.param.ParamFunction(plot, lazy=True, name='Seed 1')
p3 = pn.param.ParamFunction(plot, lazy=True, name='Seed 2')

tabs = pn.Tabs(p1, p2, p3, dynamic=True)

tabs
1 Like

Thanks for all the suggestions… i have to park this for now, will get back to it next week.

1 Like

I am trying to use lazy ParamFunction as you describe and getting the following:

2023-12-06 11:45:47,162 Error running application handler <bokeh.application.handlers.script.ScriptHandler object at 0x7f55cd644670>: ParamFunction pane does not support objects of type 'method'.
File 'base.py', line 212, in _type_error:
raise ValueError("%s pane does not support objects of type '%s'." % Traceback (most recent call last):
  File "/home/ygg/.cache/pypoetry/virtualenvs/alloha-qEy3MGg--py3.10/lib/python3.10/site-packages/bokeh/application/handlers/code_runner.py", line 229, in run
    exec(self._code, module.__dict__)
  File "/home/ygg/Workspace/TEC/alloha/app/app.py", line 7, in <module>
    from tqf.tegr1 import tegr1_app
  File "/home/ygg/Workspace/TEC/alloha/app/tqf/tegr1.py", line 69, in <module>
    tec_token_boost_tab = pn.param.ParamFunction(
  File "/home/ygg/.cache/pypoetry/virtualenvs/alloha-qEy3MGg--py3.10/lib/python3.10/site-packages/panel/param.py", line 781, in __init__
    super().__init__(object, **params)
  File "/home/ygg/.cache/pypoetry/virtualenvs/alloha-qEy3MGg--py3.10/lib/python3.10/site-packages/panel/pane/base.py", line 561, in __init__
    super().__init__(object, **params)
  File "/home/ygg/.cache/pypoetry/virtualenvs/alloha-qEy3MGg--py3.10/lib/python3.10/site-packages/panel/pane/base.py", line 164, in __init__
    self._type_error(self.object)
  File "/home/ygg/.cache/pypoetry/virtualenvs/alloha-qEy3MGg--py3.10/lib/python3.10/site-packages/panel/pane/base.py", line 212, in _type_error
    raise ValueError("%s pane does not support objects of type '%s'." %
ValueError: ParamFunction pane does not support objects of type 'method'.

I’m trying to pass a method of a parameterized class.

@LinuxIsCool Try using pn.param.ParamMethod instead.

1 Like