import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import requests
# Initialize Dash app
app = dash.Dash(__name__)
# Define app layout
app.layout = html.Div([
html.Div([
html.Label("Select a Month:"),
dcc.Dropdown(
id='month-dropdown',
options=[{'label': month, 'value': month} for month in range(1, 13)],
value=1, # Default value
),
html.Label("Select a Year:"),
dcc.Dropdown(
id='year-dropdown',
options=[{'label': year, 'value': year} for year in range(2020, 2026)],
value=2020, # Default value
),
dcc.Graph(id='monthly-collection-graph')
], style={'width': '50%', 'display': 'inline-block'}),
html.Div([
html.Label("Yearly Collection:"),
dcc.Graph(id='yearly-collection-graph')
], style={'width': '50%', 'display': 'inline-block'}),
])
# Define callback to update the monthly and yearly collection graphs based on live
data
@app.callback(
[Output('monthly-collection-graph', 'figure'),
Output('yearly-collection-graph', 'figure')],
[Input('month-dropdown', 'value'),
Input('year-dropdown', 'value')]
)
def update_graphs(selected_month, selected_year):
# Fetch live collection data for the selected month and year from an API -
replace with your actual API endpoint
response =
requests.get(f'https://api.example.com/live-collection-data/{selected_year}/
{selected_month}')
monthly_collections = response.json()['monthly_collections']
yearly_collections = response.json()['yearly_collections']
# Create trace for monthly collections
monthly_trace = go.Scatter(x=list(range(1, 13)), y=monthly_collections,
mode='lines+markers',
name=f'Monthly Collections - {selected_year}')
# Create trace for yearly collections
yearly_trace = go.Scatter(x=list(range(2020, 2026)), y=yearly_collections,
mode='lines+markers',
name='Yearly Collections')
# Create layout for monthly collections
monthly_layout = go.Layout(title=f'Monthly Collections - {selected_year}',
xaxis=dict(title='Month'),
yaxis=dict(title='Collections'))
# Create layout for yearly collections
yearly_layout = go.Layout(title='Yearly Collections',
xaxis=dict(title='Year'),
yaxis=dict(title='Collections'))
return {'data': [monthly_trace], 'layout': monthly_layout}, {'data':
[yearly_trace], 'layout': yearly_layout}
# Run the app
if __name__ == '__main__':
app.run_server(debug=True)