How to have non-shared session states

How do I write code that allows different session to have different states?
Currently, all my sessions are sychronized / share their states without explicitly coding it that way.

A minimum example:

import panel as pn

dashboard = pn.Column('# Dashboard', 
                  pn.Tabs(pn.Spacer(), pn.Spacer(), pn.Spacer())

server = pn.serve(dashboard, start=True, show=True)

If I now open multiple sessions, even across clients (so different IPs), once I click a tab in one session, all other sessions switch the tab as well.

How do I get to a situation where sessions behave independently?

Two approaches, either use .servable() and launch the dashboard with panel serve or wrap the dashboard components in a function like this:

1 Like

The .servable() approach would cause some more trouble / restructuring with my real code.
But wrapping it in a function works just fine.
Thank you!

1 Like

Adding a subtle point to this topic that I learned the hard way. Regardless of servable(), if using parameterized classes as parameters, you should initialize them in the init and not in the parameter declaration if you want them to be session-independent.

import param
import panel as pn

class SomeParameter(param.Parameterized):
    data = param.DataFrame()

class SomeClass1(param.Parameterized):
    # Initializing SomeParameter at class level will make param1 shared across all sessions
    param1 = param.Parameter(SomeParameter())

class SomeClass2(param.Parameterized):
    param1 = param.Parameter()

    def __init__(self, **params):
        # This will make param1 session independent
        param1 = SomeParameter()