Suppose i have the following layout
app.layout = html.Div(
[
html.H3(“Status”),
dcc.Tabs(
id=“tabs-selector”,
value = ‘tab1’,
children=[dcc.Tab(label=“tab1”, value=“tab1”), dcc.Tab(label=“tab2”, value=“tab2”)],
),
dcc.Loading(id=“tabs-content”,type = ‘circle’),
]
)
@app.callback(
Output(component_id=“tabs-content”, component_property=“children”), [Input(“tabs-selector”, “value”)]
)
is it possible to assign initial output value for dcc.Tabs based on condition?
for exmple if the user that is logged in is in group A, when they open the page they will see tab1 if they are from group B they will see tab2, without pressing the tabs.
Hi there Jadei…
I’m not a pro but I would create an if where it returns the tab you want.
And then pass the value as your variable.
Changing the example you gave:
GroupA=[....]
if userX in GroupA:
selectedtab='tab1',
else:
selectedtab='tab2'
app.layout = html.Div([ html.H3(“Status”),
dcc.Tabs(id=“tabs-selector”,
value = selectedtab,
children=[dcc.Tab(label=“tab1”, value=“tab1”),
dcc.Tab(label=“tab2”, value=“tab2”)],
),
dcc.Loading(id=“tabs-content”,type = ‘circle’)])
@app.callback(
Output( “tabs-content”, “children”),
[Input(“tabs-selector”, “value”)])
I hope it helps.
Another tip, if you write ``` ` in a row (without the blank space) before your code, it will look like code and will be much easier to read and understand.
Thank you for the response,
I apologize but i neglected to mention, in my case i am actually wondering if this can be done within the callback function.
May I ask why do you want to do it with a callback? A callback always needs an input, so that I know, it’s not easy to put it working when they open the page…
If the variable is in the page layout that would be solved as they load the page.
Maybe a more experienced member may help you out doing it with a callback…
I am embedding the app in django and i want to use the request.META to enter the data in the initial value. This can be achieved by by passing context to the callback function through views. but i can only get that data though callback
I’m not really a pro…hope someone else may help you!
Well in theory if you’re passing smtg to a callback, you are triggering it.
I don’t really know how you pass the info into the callback but you should have an output like this:
@app.callback(
Output( “tabs-selector”, “value”),
[Input( ) ]) #whatever you externaly pass the app.callback
def Select_tab (UserX):
GroupA=[...]
if userX in GroupA:
selectedtab='tab1',
else:
selectedtab='tab2'
return selectedtab