python – Plotly Dash: How to add an Upload Button for CSV File that creates a data Frame and plots multiple graphs

Currently writing a script that reads in a csv file and makes numerous adjustments to the dataframe and plots multiple graphs. I’m trying to include an upload button to remove the need to read in the csv file. I’m struggling to add this upload button while also making the required changes needed to the dataframe and plotting the graphs. The code is not tidy but is operable. any help would be much apricated !

from dash import Dash
from dash import dcc
from dash import html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
import pandas as pd
import numpy as np
import socket
import datetime
from datetime import timedelta
import plotly.graph_objects as go

app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])


# Layout design
app.layout = html.Div(
    [
        dbc.Row(
            [
                dbc.Col(
                    [
                        html.Div('Cork TrakSYS Losses', style={'color': 'black', 'fontSize': 40})
                    ],
                    style={"width": 12, 'font-size': '20px'},
                ),
            ]
        ),
        dbc.Row(
            [
                html.Button(id='button1', n_clicks=0, children='Refresh'),
            ],
            style=dict(
                width=2,
                display='table-cell')
        ),
        dbc.Row(
            [
                    dcc.Graph(id='fig1',
                    style={'height': '100%', 'width': '33%'}),
                    dcc.Graph(id='fig2',
                    style={'height': '100%', 'width': '33%'}),
                    dcc.Graph(id='fig3',
                    style={'height': '100%', 'width': '33%'}),
            ]
        ),
        dbc.Row(
            [
                    dcc.Graph(id='fig4',
                    style={'height': '100%', 'width': '33%'}),
                    dcc.Graph(id='fig5',
                    style={'height': '100%', 'width': '33%'}),
                    dcc.Graph(id='fig6',
                    style={'height': '100%', 'width': '33%'}),
            ]
        ),
        dbc.Row(
            [
                    dcc.Graph(id='fig7',
                    style={'height': '100%', 'width': '33%'}),
                    dcc.Graph(id='fig8',
                    style={'height': '100%', 'width': '33%'}),
                    dcc.Graph(id='fig9',
                    style={'height': '100%', 'width': '33%'}),
            ]
        ),
        dbc.Row(
            [
                    dcc.Graph(id='fig10',
                    style={'height': '100%', 'width': '100%'}),
            ]
        ),
        dbc.Row(
            [
                    dcc.Graph(id='fig11',
                    style={'height': '100%', 'width': '100%'}),
            ]
        ),
        dbc.Row(
            [
                    dcc.Graph(id='fig12',
                    style={'height': '100%', 'width': '100%'}),
            ]
        ),

            ]
        ),

@app.callback(
    Output(component_id='fig1', component_property='figure'),
    Output(component_id='fig2', component_property='figure'),
    Output(component_id='fig3', component_property='figure'),
    Output(component_id='fig4', component_property='figure'),
    Output(component_id='fig5', component_property='figure'),
    Output(component_id='fig6', component_property='figure'),
    Output(component_id='fig7', component_property='figure'),
    Output(component_id='fig8', component_property='figure'),
    Output(component_id='fig9', component_property='figure'),
    Output(component_id='fig10', component_property='figure'),
    Output(component_id='fig11', component_property='figure'),
    Output(component_id='fig12', component_property='figure'),
    Input(component_id='button1', component_property='n_clicks'),
)

def update_fig1_5(n):
    if n > 0:
        start2021 = datetime(year=2021, month=1, day=1, hour=7)
        

        traksysloss_df = pd.read_csv('IMPORTED EXCEL FILE')
        traksysloss_df = ((
            traksysloss_df.loc[lambda df: df['Area'] != 'Key WORD']))

        traksysloss_df = traksysloss_df.fillna('None') # This should be done in the API
        traksysloss_df['Start Date'] = traksysloss_df['Start Date'].astype('datetime64[ns]') # This should be done in the API
        traksysloss_df['Event Category 02'] = traksysloss_df['Event Category 02'].str.strip() # This should be done in the API
        traksysloss_df['Event Category 01'] = traksysloss_df['Event Category 01'].str.strip() # This should be done in the API     
        traksysloss_df['Equipment'] = traksysloss_df['Equipment'].str.strip() # This should be done in the API
        traksysloss_df['Event Category 02'] = traksysloss_df['Event Category 02'].str.replace('None','Unassigned') # This should be done in the API
        traksysloss_df['Equipment'] = traksysloss_df['Equipment'].str.replace('None','Unassigned') # This should be done in the API
        
        start = datetime(2022,1,1,0)
        
        end  = traksysloss_df['Start Date'].max()
        end = pd.to_datetime(end)
        end = end.to_pydatetime()
        
        start_time = {'ytd': start,
                      'r6w': end - timedelta(weeks=6),
                      'r12m': end - timedelta(days=365)
                      }

        def top_losses(time):
            filt = (traksysloss_df['Start Date'] > start_time[time]) & (traksysloss_df['Start Date'] <= end)
            traksyssum_df = traksysloss_df.loc[filt]
            traksyssum_df = traksyssum_df[['Start Date', 'Event Category 02', 'Duration (hr)', 'Effective Mass Loss (kg)']]
            traksyssum_df = ((traksyssum_df.groupby(by=['Event Category 02'], dropna=False).sum()))         
            traksyssum_df['Sum of Total (MT)'] = (traksyssum_df['Effective Mass Loss (kg)'] / 1000).round(1)
            traksyssum_df['% Loss'] = ((traksyssum_df['Effective Mass Loss (kg)'] / traksyssum_df['Effective Mass Loss (kg)'].sum()) * 100).round(2)
            traksyssum_df = traksyssum_df[['Sum of Total (MT)', '% Loss']]
            traksyssum_df = traksyssum_df.sort_values(by='Sum of Total (MT)', ascending=False)
            traksyssum_df = traksyssum_df.head(7)
            traksyssum_df = traksyssum_df.reset_index()
            return traksyssum_df

        times = ['ytd', 'r6w', 'r12m']
        df_list = list()
        for i in times:
            df = pd.DataFrame()
            df = top_losses(i)
            df_list.append(df)

        ytdlosses_df = df_list[0]
        r6wlosses_df = df_list[1]
        r12mlosses_df = df_list[2]

        def spec_losses(time, eq):
            spec = {'mech': 'Mechanical',
                    'elec': 'Electrical & Instrumentation',
                    }
            filt = (traksysloss_df['Start Date'] > start_time[time]) & (traksysloss_df['Start Date'] <= end)
            specsum_df = traksysloss_df.loc[filt]
            specsum_df = specsum_df[['Start Date', 'Event Category 02', 'Equipment', 'Duration (hr)', 'Effective Mass Loss (kg)']]
            specsum_df = ((
                    specsum_df
                    .loc[lambda df: df['Event Category 02'] == spec[eq]]
            ))
            specsum_df = specsum_df.groupby(by=['Equipment'], dropna=False).sum()
            specsum_df = specsum_df.sort_values(by='Duration (hr)', ascending=False)
            specsum_df['Sum of Total (MT)'] = (specsum_df['Effective Mass Loss (kg)'] / 1000).round(1)
            specsum_df['% Loss'] = ((specsum_df['Effective Mass Loss (kg)'] / specsum_df['Effective Mass Loss (kg)'].sum()) * 100).round(2)
            specsum_df = specsum_df[['Sum of Total (MT)', '% Loss']]
            specsum_df = specsum_df.sort_values(by='Sum of Total (MT)', ascending=False)
            specsum_df = specsum_df.head(5)
            specsum_df = specsum_df.reset_index()
            return specsum_df

        spec = ['mech', 'elec']
        df_list = list()
        for i in times:
            df = pd.DataFrame()
            df = spec_losses(i, spec[0])
            df_list.append(df)
            df = spec_losses(i, spec[1])
            df_list.append(df)

        ytdmech_df = df_list[0]
        ytdelec_df = df_list[1]
        r6wmech_df = df_list[2]
        r6welec_df = df_list[3]
        r12mmech_df = df_list[4]
        r12melec_df = df_list[5]

        traksyscusum_df = traksysloss_df[['Start Date', 'Event Category 01', 'Duration (hr)', 'Effective Mass Loss (kg)']]
        # Filter for specific Event Category 01
        traksyscusum_df = ((
                traksyscusum_df
                .loc[traksyscusum_df['Event Category 01'] == 'Equipment Issue']
                .set_index('Start Date')
        ))
        traksyscusum_df = traksyscusum_df[['Event Category 01', 'Duration (hr)', 'Effective Mass Loss (kg)']]
        traksyscusum_df = ((
                traksyscusum_df
                .groupby(by=["Start Date"], dropna=False).sum()
                .cumsum(axis=0)
        ))
        traksyscusum_df = traksyscusum_df.reset_index()

        traksysmon_df = traksysloss_df[['Start Date', 'Event Category 01', 'Duration (hr)', 'Effective Mass Loss (kg)']]
        traksysmon_df = ((
                traksysmon_df
                .loc[traksysmon_df['Event Category 01'] == 'Equipment Issue']
                .set_index('Start Date')
                .reset_index()
        ))
        traksysmon_df['Start Date'] = traksysmon_df['Start Date'].astype(str)
        traksysmon_df['Start Date New'] = traksysmon_df['Start Date'].str[:10]
        traksysmon_df = ((
                traksysmon_df
                .groupby(by=["Start Date New"], dropna=False).sum()
                .reset_index()
        ))
        traksysmon_df['Month'] = traksysmon_df['Start Date New'].str[5:7]
        traksysmon_df = ((
                traksysmon_df
                .groupby(by=["Month"], dropna=False).sum()
                .reset_index()
        ))
        fig1 = go.Figure()
        fig1.add_trace(go.Bar(x=ytdlosses_df['Sum of Total (MT)'], 
                              y=ytdlosses_df['Event Category 02'], 
                              text = ytdlosses_df['Sum of Total (MT)'].astype(int).astype(str) + " MT (" + ytdlosses_df['% Loss'].round(1).astype(str) + "%)",
                              textposition='outside',
                              orientation="h"))
        fig1.update_layout(yaxis={'categoryorder':'total ascending'})
        fig1.update_layout(plot_bgcolor = "white",
                            title="<b>YTD Losses</b> <br> Event Category 2: Top 7",
                            title_x = 0.5)
        fig1.update_xaxes(showticklabels=False)
        fig1 = fig1.update_layout(
            xaxis_range=[0, ytdlosses_df['Sum of Total (MT)'].max()+200]
        )      
        fig2 = go.Figure()
        fig2.add_trace(go.Bar(x=r6wlosses_df['Sum of Total (MT)'], 
                              y=r6wlosses_df['Event Category 02'], 
                              text = r6wlosses_df['Sum of Total (MT)'].astype(int).astype(str) + " MT (" + r6wlosses_df['% Loss'].round(1).astype(str) + "%)",
                              textposition='outside',
                              orientation="h"))
        fig2.update_layout(yaxis={'categoryorder':'total ascending'})
        fig2.update_layout(plot_bgcolor = "white",
                            title="<b>Rolling 6 Week Losses</b> <br> Event Category 2: Top 7",
                            title_x = 0.5)
        fig2.update_xaxes(showticklabels=False)
        fig2 = fig2.update_layout(
            xaxis_range=[0, r6wlosses_df['Sum of Total (MT)'].max()+200]
        )    
        fig3 = go.Figure()
        fig3.add_trace(go.Bar(x=r12mlosses_df['Sum of Total (MT)'], 
                              y=r12mlosses_df['Event Category 02'], 
                              text = r12mlosses_df['Sum of Total (MT)'].astype(int).astype(str) + " MT (" + r12mlosses_df['% Loss'].round(1).astype(str) + "%)",
                              textposition='outside',
                              orientation="h"))
        fig3.update_layout(yaxis={'categoryorder':'total ascending'})
        fig3.update_layout(plot_bgcolor = "white",
                            title="<b>Rolling 12 Month Losses</b> <br> Event Category 2: Top 7",
                            title_x = 0.5)
        fig3.update_xaxes(showticklabels=False)
        fig3 = fig3.update_layout(
            xaxis_range=[0, r12mlosses_df['Sum of Total (MT)'].max()+900]
        )               
        fig4 = go.Figure()
        fig4.add_trace(go.Bar(x=ytdmech_df['Sum of Total (MT)'], 
                              y=ytdmech_df['Equipment'], 
                              text = ytdmech_df['Sum of Total (MT)'].astype(int).astype(str) + " MT (" + ytdmech_df['% Loss'].round(1).astype(str) + "%)",
                              textposition='outside',
                              orientation="h"))
        fig4.update_layout(yaxis={'categoryorder':'total ascending'})
        fig4.update_layout(plot_bgcolor = "white",
                            title="<b>YTD Losses</b> <br> Equipment - Mechanical: Top 5",
                            title_x = 0.5)
        fig4.update_xaxes(showticklabels=False)
        fig4 = fig4.update_layout(
            xaxis_range=[0, ytdmech_df['Sum of Total (MT)'].max()+100]
        )                
        
        fig5 = go.Figure()
        fig5.add_trace(go.Bar(x=r6wmech_df['Sum of Total (MT)'], 
                              y=r6wmech_df['Equipment'], 
                              text = r6wmech_df['Sum of Total (MT)'].astype(int).astype(str) + " MT (" + r6wmech_df['% Loss'].round(1).astype(str) + "%)",
                              textposition='outside',
                              orientation="h"))
        fig5.update_layout(yaxis={'categoryorder':'total ascending'})
        fig5.update_layout(plot_bgcolor = "white",
                            title="<b>Rolling 6 Week Losses</b> <br> Equipment - Mechanical: Top 5",
                            title_x = 0.5)
        fig5.update_xaxes(showticklabels=False)
        fig5 = fig5.update_layout(
            xaxis_range=[0, r6wmech_df['Sum of Total (MT)'].max()+100]
        )  

        fig6 = go.Figure()
        fig6.add_trace(go.Bar(x=r12mmech_df['Sum of Total (MT)'], 
                              y=r12mmech_df['Equipment'], 
                              text = r12mmech_df['Sum of Total (MT)'].astype(int).astype(str) + " MT (" + r12mmech_df['% Loss'].round(1).astype(str) + "%)",
                              textposition='outside',
                              orientation="h"))
        fig6.update_layout(yaxis={'categoryorder':'total ascending'})
        fig6.update_layout(plot_bgcolor = "white",
                            title="<b>Rolling 12 Month Losses</b> <br> Equipment - Mechanical: Top 5",
                            title_x = 0.5)
        fig6.update_xaxes(showticklabels=False)
        fig6 = fig6.update_layout(
            xaxis_range=[0, r12mmech_df['Sum of Total (MT)'].max()+200]
        )  

        fig7 = go.Figure()
        fig7.add_trace(go.Bar(x=ytdelec_df['Sum of Total (MT)'], 
                             y=ytdelec_df['Equipment'], 
                             text = ytdelec_df['Sum of Total (MT)'].astype(int).astype(str) + " MT (" + ytdelec_df['% Loss'].round(1).astype(str) + "%)",
                             textposition='outside',
                             orientation="h"))
        fig7.update_layout(yaxis={'categoryorder':'total ascending'})
        fig7.update_layout(plot_bgcolor = "white",
                           title="<b>YTD Losses</b> <br> Equipment - Electrical: Top 5",
                           title_x = 0.5)
        fig7.update_xaxes(showticklabels=False)
        fig7 = fig7.update_layout(
           xaxis_range=[0, ytdelec_df['Sum of Total (MT)'].max()+100]
        )  

        fig8 = go.Figure()
        fig8.add_trace(go.Bar(x=r6welec_df['Sum of Total (MT)'], 
                             y=r6welec_df['Equipment'], 
                             text = r6welec_df['Sum of Total (MT)'].astype(int).astype(str) + " MT (" + r6welec_df['% Loss'].round(1).astype(str) + "%)",
                             textposition='outside',
                             orientation="h"))
        fig8.update_layout(yaxis={'categoryorder':'total ascending'})
        fig8.update_layout(plot_bgcolor = "white",
                           title="<b>Rolling 6 Week Losses</b> <br> Equipment - Electrical: Top 5",
                           title_x = 0.5)
        fig8.update_xaxes(showticklabels=False)
        fig8 = fig8.update_layout(
           xaxis_range=[0, r6welec_df['Sum of Total (MT)'].max()+100]
        )  

        fig9 = go.Figure()
        fig9.add_trace(go.Bar(x=r12melec_df['Sum of Total (MT)'], 
                             y=r12melec_df['Equipment'], 
                             text = r12melec_df['Sum of Total (MT)'].astype(int).astype(str) + " MT (" + r12melec_df['% Loss'].round(1).astype(str) + "%)",
                             textposition='outside',
                             orientation="h"))
        fig9.update_layout(yaxis={'categoryorder':'total ascending'})
        fig9.update_layout(plot_bgcolor = "white",
                           title="<b>Rolling 12 Month Losses</b> <br> Equipment - Electrical: Top 5",
                           title_x = 0.5)
        fig9.update_xaxes(showticklabels=False)
        fig9 = fig9.update_layout(
           xaxis_range=[0, r12melec_df['Sum of Total (MT)'].max()+100]
        )  



        df_el_2022_rdec1 = pd.read_csv('ck_el_2022_rdec1.csv').fillna(0.0) # For Event Category 01
        df_el_2022_rdec2 = pd.read_csv('ck_el_2022_rdec2.csv').fillna(0.0) # For Event Category 02
        
        # Graphs to generate
        groups = ['Event Category 01', 'Event Category 02']

        td = end - start
        td = td.total_seconds()
        td = td / 86400 # Convert to # days
        # td = td.values[0]
        
        filt2022 = (traksysloss_df['Start Date'] > start) & (traksysloss_df['Start Date'] <= end)
        df_el_2022 = traksysloss_df.loc[filt2022]
        
        filt2021 = (traksysloss_df['Start Date'] > start2021) & (traksysloss_df['Start Date'] <= start)
        df_el_2021 = traksysloss_df.loc[filt2021]

        
        df_list = list()
        for i in groups:
            ref = {groups[0]: df_el_2022_rdec1,
                   groups[1]: df_el_2022_rdec2}
            # Grouping 2022 data by i
            df_2022 = df_el_2022
            df_2022 = (
                df_2022
                .groupby(i).sum()
        
                .reset_index()
                .merge(ref[i],how='left', left_on=i, right_on=i)
                .rename(columns={"Duration (hr)_x": "Duration (hr) YTD", 
                                   "Effective Mass Loss (kg)_x": "Effective Mass Loss (kg) YTD",
                                   "Duration (hr)_y": "Duration (hr) 2022 Reduction", 
                                   "Effective Mass Loss (kg)_y": "Effective Mass Loss (kg) 2022 Reduction"
                                  })
                .drop(columns=['Rate (kg/hr)'])
                .set_index(i)
            )
            df_2022['Duration (hr) 2022 Forecast'] = (df_2022['Duration (hr) YTD'] / td)*365.0
            df_2022['Effective Mass Loss (kg) 2022 Forecast'] = (df_2022['Effective Mass Loss (kg) YTD'] / td)*365.0
            df_2022 = (
                df_2022
                .round(1)

                .sort_values(by=['Effective Mass Loss (kg) YTD'], ascending=False)
            )
            # Grouping 2021 data by i
            df_2021 = df_el_2021
            df_2021 = (
            df_2021
            .groupby(i).sum()
            .reset_index()
            .rename(columns={"Duration (hr)": "Duration (hr) 2021 Actual", 
                               "Effective Mass Loss (kg)": "Effective Mass Loss (kg) 2021 Actual"
                              })
            .set_index(i)
            .round(1)
            .sort_values(by=['Effective Mass Loss (kg) 2021 Actual'], ascending=False)
            )
            df_2022 = (
            df_2022
            .merge(df_2021,how='left', left_on=i, right_on=i)    
            )
            df_2022['Effective Mass Loss (kg) 2022 Reduction'] = df_2022['Effective Mass Loss (kg) 2022 Reduction'].fillna(0.0)
            df_2022['Duration (hr) 2022 Reduction'] = np.where(df_2022['Duration (hr) 2022 Reduction'] == 'blank', 0.0, df_2022['Duration (hr) 2022 Reduction'])
            df_2022['Duration (hr) 2022 Target'] = df_2022['Duration (hr) 2021 Actual'] - df_2022['Duration (hr) 2022 Reduction']
            df_2022['Effective Mass Loss (kg) 2022 Target'] = df_2022['Effective Mass Loss (kg) 2021 Actual'] - df_2022['Effective Mass Loss (kg) 2022 Reduction']
            df_list.append(df_2022)
            
        df_ec1_top5_ytd = df_list[0]
        num_losses = 10
        
        fig10 = go.Figure()
        fig10.add_trace(go.Bar(
                name="YTD", 
                x=df_ec1_top5_ytd.index, 
                y=(df_ec1_top5_ytd['Effective Mass Loss (kg) YTD'].head(num_losses))/1000,
                text = (df_ec1_top5_ytd['Effective Mass Loss (kg) YTD']/1000).head(num_losses).astype(int).astype(str) + " MT",
                textposition='outside',
                marker = {'color': 'steelblue'}))
        fig10.add_trace(go.Bar(
                name="Forecast", 
                x=df_ec1_top5_ytd.index, 
                y=(df_ec1_top5_ytd['Effective Mass Loss (kg) 2022 Forecast'].head(num_losses))/1000,
                text = (df_ec1_top5_ytd['Effective Mass Loss (kg) 2022 Forecast']/1000).head(num_losses).astype(int).astype(str) + " MT",          
                textposition='outside',
                marker = {'color': 'steelblue'},
                marker_pattern_shape="/"))
        fig10.add_trace(go.Bar(
                name="Target", 
                x=df_ec1_top5_ytd.index, 
                y=(df_ec1_top5_ytd['Effective Mass Loss (kg) 2022 Target'].head(num_losses))/1000,
                text = (df_ec1_top5_ytd['Effective Mass Loss (kg) 2022 Target']/1000).head(num_losses).astype(int).astype(str) + " MT",
                textposition='outside'))
        fig10.update_yaxes(showticklabels=False)
        fig10.update_layout(plot_bgcolor = "white",
                           title="<b>YTD Losses</b> <br> Event Category 01: Top "+str(num_losses),
                           title_x = 0.5)
        fig10 = fig10.update_layout(
           yaxis_range=[0, (df_ec1_top5_ytd['Effective Mass Loss (kg) 2022 Forecast']/1000).max()+200]
        )  
        
        df_ec2_top5_ytd = df_list[1]  
        
        fig11 = go.Figure()
        fig11.add_trace(go.Bar(
                name="YTD", 
                x=df_ec2_top5_ytd.index, 
                y=(df_ec2_top5_ytd['Effective Mass Loss (kg) YTD'].head(num_losses))/1000,
                text = (df_ec2_top5_ytd['Effective Mass Loss (kg) YTD']/1000).head(num_losses).astype(int).astype(str) + " MT",
                textposition='outside',
                marker = {'color': 'steelblue'}))
        fig11.add_trace(go.Bar(
                name="Forecast", 
                x=df_ec2_top5_ytd.index, 
                y=(df_ec2_top5_ytd['Effective Mass Loss (kg) 2022 Forecast'].head(num_losses))/1000,
                text = (df_ec2_top5_ytd['Effective Mass Loss (kg) 2022 Forecast']/1000).head(num_losses).astype(int).astype(str) + " MT",
                textposition='outside',
                marker = {'color': 'steelblue'},
                marker_pattern_shape="/"))
        fig11.add_trace(go.Bar(
                name="Target", 
                x=df_ec2_top5_ytd.index, 
                y=(df_ec2_top5_ytd['Effective Mass Loss (kg) 2022 Target'].head(num_losses))/1000,
                text = (df_ec2_top5_ytd['Effective Mass Loss (kg) 2022 Target']/1000).head(num_losses).astype(int).astype(str) + " MT",
                textposition='outside'))
        fig11.update_yaxes(showticklabels=False)
        fig11.update_layout(plot_bgcolor = "white",
                           title="<b>YTD Losses</b> <br> Event Category 02: Top "+str(num_losses),
                           title_x = 0.5)
        fig11 = fig11.update_layout(
           yaxis_range=[0, (df_ec2_top5_ytd['Effective Mass Loss (kg) 2022 Forecast']/1000).max()+200]
        )  
        
        
        df_ec1_top5_targ = df_list[0]
        df_ec1_top5_targ = df_ec1_top5_targ.fillna(0.0).astype(int)
        
        df_ec1_top5_targ['Delta'] = df_ec1_top5_targ['Effective Mass Loss (kg) YTD'] - df_ec1_top5_targ['Effective Mass Loss (kg) 2022 Target']
        df_ec1_top5_targ = df_ec1_top5_targ.sort_values(by=['Delta'], ascending=False)
        
        
        fig12 = go.Figure()
        fig12.add_trace(go.Bar(
                name="YTD", 
                x=df_ec1_top5_targ.index, 
                y=(df_ec1_top5_targ['Effective Mass Loss (kg) YTD'].head(num_losses))/1000,
                text = (df_ec1_top5_targ['Effective Mass Loss (kg) YTD']/1000).head(num_losses).astype(int).astype(str) + " MT",
                textposition='outside',
                marker = {'color': 'steelblue'}))
        fig12.add_trace(go.Bar(
                name="Forecast", 
                x=df_ec1_top5_targ.index, 
                y=(df_ec1_top5_targ['Effective Mass Loss (kg) 2022 Forecast'].head(num_losses))/1000,
                text = (df_ec1_top5_targ['Effective Mass Loss (kg) 2022 Forecast']/1000).head(num_losses).astype(int).astype(str) + " MT",
                textposition='outside',
                marker = {'color': 'steelblue'},
                marker_pattern_shape="/"))
        fig12.add_trace(go.Bar(
                name="Target", 
                x=df_ec1_top5_targ.index, 
                y=(df_ec1_top5_targ['Effective Mass Loss (kg) 2022 Target'].head(num_losses))/1000,
                text = (df_ec1_top5_targ['Effective Mass Loss (kg) 2022 Target']/1000).head(num_losses).astype(int).astype(str) + " MT",
                textposition='outside'))
        fig12.update_yaxes(showticklabels=False)
        fig12.update_layout(plot_bgcolor = "white",
                           title="<b>YTD Losses</b> <br> Event Category 01: Top "+str(num_losses)+ ' Approaching Target',
                           title_x = 0.5)
        fig12 = fig12.update_layout(
           yaxis_range=[0, (df_ec1_top5_targ['Effective Mass Loss (kg) 2022 Forecast']/1000).max()+200]
        ) 
        
        return fig1, fig2, fig3, fig4, fig5, fig6, fig7, fig8, fig9, fig10, fig11, fig12,
    else:
        return ''


if __name__ == "__main__":
   host = socket.gethostbyname(socket.gethostname())
app.run_server(debug=[True or False], host=host, port = 8080)

Leave a Comment