This is my code:
def plot_data(df, metallo, port = 1276):
# Initialize the Dash app
app = Dash(__name__)
# Define the layout of the app
app.layout = html.Div([
dcc.Graph(id="graph"),
html.P("Starting point of the interval:"),
dcc.Slider(id="start-slider", min=1, max=len(df['Commercial Undertakings_net'])-1, value=(len(df['Commercial Undertakings_net'])-52), step=1),
html.P("Maximum lag:"),
dcc.Slider(id="lag-slider", min=1, max=len(df['Commercial Undertakings_net']), value=(len(df['Commercial Undertakings_net'])), step=1),
])
@app.callback(
Output("graph", "figure"),
[Input("start-slider", "value"),
Input("lag-slider", "value")]
)
def update_bar_chart(start, lag):
# Slice the data based on the selected interval
comm_data = df['Commercial Undertakings_net'][start-1:start+lag-1]
print(comm_data)
noncomm_data = df['InvestmentFunds_net'][start-1:start+lag-1]
index_values = df['Data'][start-1:start+lag-1]
# Create bar traces for comm_net and noncomm_net
# comm_trace = go.Bar(x=index_values, y=comm_data, name='Commercials')
noncomm_trace = go.Bar(x=index_values, y=noncomm_data, name='Investment Funds',marker=dict(color='red'))
# Create layout with secondary y-axis, legend, and centered, bold title
layout = go.Layout(
title=f"<b>Commitment of Traders LME - {metallo}</b>",
titlefont=dict(size=16, color='black', family='Arial',),
yaxis=dict(title='Comm & Inv.Fund Net'),
showlegend=True,
title_x=0.5
)
# Create a figure with both traces and layout
fig = go.Figure(data=[noncomm_trace], layout=layout)
return fig
app.run_server(port=port)
which returns me this:
Rather than everytime click on the dashboard to download the image as png, isn’t possible to write a line of code that does it automatically?
I tried adding this line of code right before “return fig” :
# Save the figure as PNG
fig.write_image(f"COTR{metallo}.png")
but is not working because is not displaying me anymore the plot, like this:
Anyway, even if the plot is completely white, nothing has been downloaded. Any suggestion?