def build_bargraph(country_input, input_date_range,gtype):
    # Filtering the results based on input date range
    df_merged_filtered = dh.handle_updated_dates(df_merged, 'date', input_date_range, number_date_range_dict)

    df_per_country1 = df_merged_filtered[(df_merged_filtered['country'] == country_input)]
    df_per_country1 = df_per_country1.rename(
        {'new_cases': 'New cases ' + country_input, 'new_deaths': 'New deaths ' + country_input}, axis=1)
    df_germany1 = df_merged_filtered[(df_merged_filtered['country'] == "Germany")]
    df_germany1 = df_germany1.rename({'new_cases': 'New cases Germany', 'new_deaths': 'New deaths Germany'}, axis=1)
    df_merge1 = pd.merge(df_germany1, df_per_country1, on='date')
    # print(df_merge1)

    df_merge1 = pd.DataFrame(df_merge1)

    #print(df_merge1.info())
    if gtype == "B":
        fig = go.Figure(data=[
        go.Bar(name='New cases in Germany', x=df_merge1['date'], y=df_merge1['New cases Germany']),
        go.Bar(name='New cases in ' + country_input, x=df_merge1['date'], y=df_merge1['New cases ' + country_input])
    ])
        fig.update_layout(title_text='Comparing new cases in ' + country_input + ' against Germany',
                      xaxis_title='Date', yaxis_title='Cases')
        fig1 = go.Figure(data=[
        go.Bar(name='New deaths in Germany', x=df_merge1['date'], y=df_merge1['New deaths Germany']),
        go.Bar(name='New deaths in ' + country_input, x=df_merge1['date'], y=df_merge1['New deaths ' + country_input])
    ])
        fig1.update_layout(title_text='Comparing new deaths in ' + country_input + ' against Germany',
                       xaxis_title='Date', yaxis_title='Cases')

        return fig, fig1
    else:
        fig = px.line(df_merge1,x=df_merge1['date'],y=['New cases Germany','New cases ' + country_input],title='Comparing new cases of '+country_input+' against Germany')
        fig1 = px.line(df_merge1, x=df_merge1['date'], y=['New deaths Germany', 'New deaths ' + country_input],
                      title='Comparing  new deaths of ' + country_input + ' against Germany')
        return fig,fig1
def build_graph_mean(country_input, govt_rest, input_date_range):

    filtered_Dataset1 = dh.handle_updated_dates(DataSet1, 'date', input_date_range, number_date_range_dict)
    German_data = filtered_Dataset1[(filtered_Dataset1['CountryName'] == "Germany")]
    Country_data = filtered_Dataset1[(filtered_Dataset1['CountryName'] == country_input)]
    German_data = German_data.rename(
        {'C1_School closing': 'School Closing Germany', 'C2_Workplace closing': 'WorkPlace Closing Germany',
         'C6_Stay at home requirements': 'StayHome Restriction Germany',
         'C4_Restrictions on gatherings': 'Gather Restriction Germany',
         'C5_Close public transport': 'Transport Restriction Germany',
         'C8_International travel controls': 'International Travel Restriction Germany',
         'retail_and_recreation_percent_change_from_baseline': 'Retail Restriction Germany',
         'grocery_and_pharmacy_percent_change_from_baseline': 'Grocery Pharmacy Restriction Germany',
         'parks_percent_change_from_baseline': 'Park Restriction Germany'}, axis=1)
    Country_data = Country_data.rename({'C1_School closing': 'School Closing ' + country_input,
                                        'C2_Workplace closing': 'WorkPlace Closing ' + country_input,
                                        'C6_Stay at home requirements': 'StayHome Restriction ' + country_input,
                                        'C4_Restrictions on gatherings': 'Gather Restriction ' + country_input,
                                        'C5_Close public transport': 'Transport Restriction ' + country_input,
                                        'C8_International travel controls': 'International Travel Restriction ' + country_input,
                                        'retail_and_recreation_percent_change_from_baseline': 'Retail Restriction ' + country_input,
                                        'grocery_and_pharmacy_percent_change_from_baseline': 'Grocery Pharmacy Restriction ' + country_input,
                                        'parks_percent_change_from_baseline': 'Park Restriction ' + country_input},
                                       axis=1)
    df_merge_mean = pd.merge(German_data, Country_data, on='date')
    df_merge_mean['meanGermany'] = df_merge_mean['School Closing Germany'].div(9, axis=0) + df_merge_mean[
        'WorkPlace Closing Germany'].div(9, axis=0) + df_merge_mean['StayHome Restriction Germany'].div(9, axis=0) + \
                                   df_merge_mean[
                                       'Gather Restriction Germany'].div(9, axis=0) + df_merge_mean[
                                       'Transport Restriction Germany'].div(9, axis=0) + df_merge_mean[
                                       'International Travel Restriction Germany'].div(9, axis=0) + df_merge_mean[
                                       'Retail Restriction Germany'].div(9, axis=0) + df_merge_mean[
                                       'Grocery Pharmacy Restriction Germany'].div(9, axis=0) + df_merge_mean[
                                       'Park Restriction Germany'].div(9, axis=0)
    df_merge_mean['meanCountry'] = df_merge_mean['School Closing ' + country_input].div(9, axis=0) + df_merge_mean[
        'WorkPlace Closing ' + country_input].div(9, axis=0) + df_merge_mean['StayHome Restriction ' + country_input].div(
        9, axis=0) + df_merge_mean[
                                       'Gather Restriction ' + country_input].div(9, axis=0) + df_merge_mean[
                                       'Transport Restriction ' + country_input].div(9, axis=0) + df_merge_mean[
                                       'International Travel Restriction ' + country_input].div(9, axis=0) + \
                                   df_merge_mean[
                                       'Retail Restriction ' + country_input].div(9, axis=0) + df_merge_mean[
                                       'Grocery Pharmacy Restriction ' + country_input].div(9, axis=0) + df_merge_mean[
                                       'Park Restriction ' + country_input].div(9, axis=0)
    df_merge_mean['meanGermany'] = df_merge_mean['meanGermany'].fillna(0)
    df_merge_mean['meanCountry'] = df_merge_mean['meanCountry'].fillna(0)

    df_merge_mean = df_merge_mean.rename(
        {'meanGermany': 'Overall Restriction Germany', 'meanCountry': 'Overall Restriction ' + country_input}, axis=1)

    new_df_mean = pd.DataFrame(df_merge_mean)
    df_1_mean = pd.DataFrame(new_df_mean)

    fig = px.line(df_1_mean, x='date', y=['Overall Restriction Germany', 'Overall Restriction '+country_input])
    fig.update_layout(title_text='MEAN of all Policy Measures')
    return fig
def build_graph1(country_input, govt_rest, input_date_range):

    filtered_Dataset1 = dh.handle_updated_dates(DataSet1, 'date', input_date_range, number_date_range_dict)
    German_data = filtered_Dataset1[(filtered_Dataset1['CountryName'] == "Germany")]
    Country_data = filtered_Dataset1[(filtered_Dataset1['CountryName'] == country_input)]
    German_data = German_data.rename(
        {'C1_School closing': 'School Closing Germany', 'C2_Workplace closing': 'WorkPlace Closing Germany',
         'C6_Stay at home requirements': 'StayHome Restriction Germany',
         'C4_Restrictions on gatherings': 'Gather Restriction Germany',
         'C5_Close public transport': 'Transport Restriction Germany',
         'C8_International travel controls': 'International Travel Restriction Germany',
         'retail_and_recreation_percent_change_from_baseline': 'Retail Restriction Germany',
         'grocery_and_pharmacy_percent_change_from_baseline': 'Grocery Pharmacy Restriction Germany',
         'parks_percent_change_from_baseline': 'Park Restriction Germany'}, axis=1)
    Country_data = Country_data.rename({'C1_School closing': 'School Closing ' + country_input,
                                        'C2_Workplace closing': 'WorkPlace Closing ' + country_input,
                                        'C6_Stay at home requirements': 'StayHome Restriction ' + country_input,
                                        'C4_Restrictions on gatherings': 'Gather Restriction ' + country_input,
                                        'C5_Close public transport': 'Transport Restriction ' + country_input,
                                        'C8_International travel controls': 'International Travel Restriction ' + country_input,
                                        'retail_and_recreation_percent_change_from_baseline': 'Retail Restriction ' + country_input,
                                        'grocery_and_pharmacy_percent_change_from_baseline': 'Grocery Pharmacy Restriction ' + country_input,
                                        'parks_percent_change_from_baseline': 'Park Restriction ' + country_input},
                                       axis=1)
    df_merge = pd.merge(German_data, Country_data, on='date')
    # print(df_merge2['date'])
    new_df = pd.DataFrame(df_merge)
    # new_df2 = pd.DataFrame(df_merge2)
    df_1 = pd.DataFrame(new_df)
    # df_2 = pd.DataFrame(new_df2)
    if govt_rest == 'School closing':
        fig = drawLinegraph(df_1, 'School Closing ', country_input)
    elif govt_rest == 'Workplace closing':
        fig = drawLinegraph(df_1, 'WorkPlace Closing ', country_input)
    elif govt_rest == 'Stay at home requirements':
        fig = drawLinegraph(df_1, 'StayHome Restriction ', country_input)
    elif govt_rest == 'Restrictions on gatherings':
        fig = drawLinegraph(df_1, 'Gather Restriction ', country_input)
    elif govt_rest == 'Close public transport':
        fig = drawLinegraph(df_1, 'Transport Restriction ', country_input)
    elif govt_rest == 'International travel controls':
        fig = drawLinegraph(df_1, 'International Travel Restriction ', country_input)
    elif govt_rest == 'Restriction on Retail':
        fig = drawLinegraph(df_1, 'Retail Restriction ', country_input)
    elif govt_rest == "Restriction on pharmacy":
        fig = drawLinegraph(df_1, 'Grocery Pharmacy Restriction ', country_input)
    elif govt_rest == "Restriction on park":
        fig = drawLinegraph(df_1, 'Park Restriction ', country_input)
    return fig
def generate_datatable_countries(country_input, input_date_range, tab_id):

    label = country_input

    data_per_country_df = data_table_visulaization_df[(data_table_visulaization_df['COUNTRY'] == country_input)]

    data_per_country_df = dh.handle_updated_dates(data_per_country_df, 'DATE_IMPLEMENTED', input_date_range,
                                                  number_date_range_dict)
    tooltip_data = [
        {
            column: {'value': str(value), 'type': 'markdown'}
            for column, value in row.items()
        } for row in data_per_country_df.to_dict('rows')
    ]
    return (data_per_country_df.to_dict('records'), tooltip_data, label)
def generate_datatable_germany(input_date_range, tab_id):
    
    country_input = "Germany"
    label = country_input

    data_table_germany = data_table_visulaization_df[(data_table_visulaization_df['COUNTRY'] == country_input)]
    tooltip_data = []

    # Displaying only Germany data in Tab2 
    if tab_id == 'tab-2':
    
        data_table_germany = dh.handle_updated_dates(data_table_germany, 'DATE_IMPLEMENTED', input_date_range,
                                                    number_date_range_dict)
        tooltip_data = [
            {
                column: {'value': str(value), 'type': 'markdown'}
                for column, value in row.items()
            } for row in data_table_germany.to_dict('rows')
        ]
    return (data_table_germany.to_dict('records'), tooltip_data, label)
def build_graph(country_input, input_date_range):
    # Filtering the results based on input date range
    df_merged_filtered = dh.handle_updated_dates(df_merged, 'date', input_date_range, number_date_range_dict)

    df_per_country = df_merged_filtered[(df_merged_filtered['country'] == country_input)]
    df_per_country = df_per_country.rename({'total_cases': 'total_cases_' + country_input,
                                            'total_deaths': 'total_deaths_' + country_input,
                                            'total_recovery': 'total_recovery_' + country_input,
                                            'total_tests': 'total_tests_' + country_input}, axis=1)
    df_germany = df_merged_filtered[(df_merged_filtered['country'] == "Germany")]
    df_germany = df_germany.rename({'total_cases': 'total_cases_Germany',
                                    'total_deaths': 'total_deaths_Germany',
                                    'total_recovery': 'total_recovery_Germany',
                                    'total_tests': 'total_tests_Germany'}, axis=1)
    df_merge = pd.merge(df_germany, df_per_country, on='date')
    # print(df_merge.to_string())
    df_merge = pd.DataFrame(df_merge)

    fig = go.Figure()
    fig.add_trace(go.Scatter(x=df_merge['date'], y=df_merge["total_cases_Germany"], name="Total affected Germany"))
    fig.add_trace(go.Scatter(x=df_merge['date'], y=df_merge["total_deaths_Germany"], name="Total death Germany"))
    fig.add_trace(go.Scatter(x=df_merge['date'], y=df_merge["total_recovery_Germany"], name="Total recovered Germany"))
    fig.add_trace(go.Scatter(x=df_merge['date'], y=df_merge["total_deaths_" + country_input],
                             name="Total affected " + country_input))
    fig.add_trace(go.Scatter(x=df_merge['date'], y=df_merge["total_deaths_" + country_input],
                             name="Total death " + country_input))
    fig.add_trace(go.Scatter(x=df_merge['date'], y=df_merge["total_deaths_" + country_input],
                             name="Total recovered " + country_input))

    fig.update_layout(title_text='Comparing COVID cases of ' + country_input + ' against Germany',
                      title=dict(font=dict(size=20)), xaxis_title='Date', yaxis_title='Cases')

    fig_bar_graph = go.Figure(data=[
        go.Bar(name='Total tests in Germany', x=df_merge['date'], y=df_merge['total_tests_Germany'],
               marker_color='crimson'),
        go.Bar(name='Total tests in ' + country_input, x=df_merge['date'], y=df_merge['total_tests_' + country_input],
               marker_color='rgb(26, 118, 255)')
    ])
    fig_bar_graph.update_layout(title_text='Comparing COVID tests of ' + country_input + ' against Germany',
                                title=dict(font=dict(size=20)), xaxis_title='Date', yaxis_title='Cases')
    return fig, fig_bar_graph
        {'meanGermany': 'Overall Restriction Germany', 'meanCountry': 'Overall Restriction ' + country_input}, axis=1)

    new_df_mean = pd.DataFrame(df_merge_mean)
    df_1_mean = pd.DataFrame(new_df_mean)

    fig = px.line(df_1_mean, x='date', y=['Overall Restriction Germany', 'Overall Restriction '+country_input])
    fig.update_layout(title_text='MEAN of all Policy Measures')
    return fig


@app.callback(
    Output(component_id='the_graph', component_property='figure'),
    [Input('cases', 'value'),
     Input('slider_date', 'value')])
def build_map(case, input_date_range):
    df_merged_date_filtered = dh.handle_updated_dates(df_merged_raw, 'date', input_date_range, number_date_range_dict)

    if case == 'R':
        case_chosen = 'total_recovery'
    elif case == 'D':
        case_chosen = 'total_deaths'
    else:
        case_chosen = 'total_cases'

    fig_map = px.choropleth(data_frame=df_merged_date_filtered, geojson=europe_geo_json, locations='country',
                            scope="europe", color=case_chosen, hover_name='country', featureidkey='properties.name',
                            projection="equirectangular", color_continuous_scale="Blues",
                            title='Total COVID-19 Cases Across Europe')  # , template='plotly_dark')

    fig_map.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
    return fig_map