i have a very simple layout:
app.layout = dbc.Container([dbc.Tabs([dbc.Tab(label='Tab1', tab_id='tab1'),
dbc.Tab(label='Tab2', tab_id='tab2')],
id='tabs',
active_tab='tab1'),
html.Div(id='tab-content')])
then based on some database settings, the flask-login current user has a list of dicts that looks like
current_user.charts = [{'chart_id': 'chart_1', 'asset': 'someasset', 'lookback': '2Y'},
{'chart_id': 'chart_2', 'asset': 'someasset2', 'lookback': '5Y'}]
so then in my callbacks.py file i have the code
def register_callbacks(dashapp):
@dashapp.callback(
Output('tab-content', 'children'),
[Input('tabs', 'active_tab')])
def render_tab(active_tab):
if active_tab == 'tab1':
return render_tab1(current_user.charts)
else:
return html.Div([html.Br(), 'some junk tab2 text'])
def render_tab1(chart_list):
charts = []
for chart_settings in chart_list:
chart_id = chart_settings['chart_id']
chart_data_df = get_data(chart_settings['asset'],
chart_settings['lookback'])
#chart_data_df is a pandas dataframe index=DateTimeIndex, with one column close
#which is a timeseries of prices for the given asset.
fig = plotyexpress.line(chart_data_df,
labels={'variable': 'Data Legend'})
fig.update_xaxes(tickangle = 45, title_text = "Date")
fig.update_yaxes(range=[0, max(chart_data_df.max(axis=1)) * 1.25],
title_text = "Close")
#create the rangle slider
fig.update_layout(
xaxis=dict(
rangeselector=dict(
buttons=list([
{'count': 1, 'label': '1m', 'step': 'month', 'stepmode':'backward'},
{'count': 6, 'label': '6m', 'step': 'month', 'stepmode':'backward'},
{'count': 1, 'label': 'YTD', 'step': 'year', 'stepmode':'todate'},
{'count': 1, 'label': '1y', 'step': 'year', 'stepmode':'backward'},
{'step': 'all'}
])
),
rangeslider={'visible': False},
type="date"
)
)
charts.append(dcc.Graph(id=chart_id, figure=fig))
return html.Div(charts, id='tab1_charts')
this does an excellent job of creating my charts on the tab as the page is rendered, and it allows me to customize the charts based on updating the database, versus hardcoding them.
however my question is that when you use the rangeselector it doesn’t change the Y-axis scale of the graphs. all the documentation on the callbacks
for example, the callback has to define the id of the figure in the callback decorator, however I don’t know this when the app is registering, since its unknown until the page is rendered. thus I don’t know how to properly draft a callback function to adjust the y-axis min/max
any idea?