I’m trying to create a stacked bar graph that updates as I update the inputs of a table.
The graph looks exactly the way I want it to without any interactivity with a code like this:
m=0
while m<len(Ref) :
ROWS = [i for i, x in enumerate(Shop) if x == Ref.iloc[m,0]]
j = len(ROWS)-1
while(j>0):
Ref.loc[m,:] = Ref.loc[m,:] + Ref.loc[ROWS[j]]
Ref = Ref.drop([ROWS[j]])
j = j -1
m = m+1
for i in Ref['Shop'] :
P = list(Ref['Shop'])
p = P.index(i)
Ref.iloc[p,0] = Shop[p]
Ref.iloc[p,1] =Price[p]
data = []
d=0
while d < len(Ref) :
TITLE = go.Bar(
x=Frames,
y=Ref.iloc[d,2:26],
name=Ref.iloc[d,0],
)
data.append(TITLE)
d = d+1
##App Design##
app.layout = html.Div([
html.H4('Stores'),
dt.DataTable(
rows=DF_GAPMINDER.to_dict('records'),
# rows=Data2.to_dict('records'),
# optional - sets the order of columns
columns=(DF_GAPMINDER.columns),
# columns=(Data2.columns),
filters=True,
resizable=True,
sortColumn=True,
editable=True,
row_selectable=True,
filterable=True,
sortable=True,
selected_row_indices=[],
id='datatable-gapminder'
),
html.Div(id='selected-indexes'),
dcc.Graph(
figure = go.Figure(data = data,
layout=go.Layout(
barmode='stack',
title='Storage',
showlegend=True,
legend=go.Legend(
x=0,
y=1.0
),
margin=go.Margin(l=40, r=0, t=40, b=30)
)
),
style={'height': 300},
id='graph-gapminder'
)
])
But, when I try to create an interactive graph, by putting the information in @app.callback and returning the layout I want, the data is returned but not the layout.
def datacompile(Ref) :
m=0
while m<len(Ref) :
ROWS = [i for i, x in enumerate(Store) if x == Ref.iloc[m,0]]
j = len(ROWS)-1
while(j>0):
Ref.loc[m,:] = Ref.loc[m,:] + Ref.loc[ROWS[j]]
Ref = Ref.drop([ROWS[j]])
j = j -1
m = m+1
for i in Ref['Shop'] :
P = list(Ref['Shop'])
p = P.index(i)
Ref.iloc[p,0] = Shop[p]
Ref.iloc[p,1] =Price[p]
data = []
d=0
while d < len(Ref) :
TITLE = go.Bar(
x=list(Ref)[2:26],
y=Ref.iloc[d,2:26],
name=Shop[d],
)
data.append(TITLE)
d = d+1
return {
'data' : np.array(data),
}
app.layout = html.Div([
html.H4('Stores'),
dt.DataTable(
rows=DF_GAPMINDER.to_dict('records'),
# rows=Data2.to_dict('records'),
# optional - sets the order of columns
columns=(DF_GAPMINDER.columns),
# columns=(Data2.columns),
filters=True,
resizable=True,
sortColumn=True,
editable=True,
row_selectable=True,
filterable=True,
sortable=True,
selected_row_indices=[],
id='datatable-gapminder'
),
html.Div(id='selected-indexes'),
dcc.Graph(
style={'height': 300},
id='graph-gapminder'
)
])
@app.callback(
Output('graph-gapminder', 'figure'),
[Input('datatable-gapminder', 'rows')])
def update_figure(rows):
dff =pd.DataFrame(rows)
return{ 'data': datacompile(dff),
'layout': {
'barmode' : 'stack'},
}
The thing is, i can only return the data. If i attempt to return a figure and add the layout to the function, no graph appears. I also tried to return the layout within the function, add it to the app.layout, such that I’m only updating the data, but none of this works.
The second code presents an interactive graph, with no arranged layout (in this case, it is just stacking, but once i get it to work, i’ll add everything else).
Help!