<filename>Part3-Article-NLP/index.py
import os

import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

from app import app

# from apps import app_home, app_overview, app_stringency, app_multi_sentiment_analysis, app_topic_modeling, app_question_answering, app_event_detection
from apps import app_home, app_stringency, app_multi_sentiment_analysis, app_topic_modeling, app_event_detection

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP], suppress_callback_exceptions=True)

# the style arguments for the sidebar. We use position:fixed and a fixed width
SIDEBAR_STYLE = {
    "position": "fixed",
    "top": 0,
    "left": 0,
    "bottom": 0,
    "width": "16rem",
    "padding": "2rem 1rem",
    "background-color": "#f8f9fa",
}

# the styles for the main content position it to the right of the sidebar and
# add some padding.
CONTENT_STYLE = {
    "margin-left": "18rem",
    "margin-right": "2rem",
    "padding": "2rem 1rem",
}

sidebar = html.Div(
    [
        html.H2("Summary", className="display-4"),
        html.Hr(),
        html.P(
            "Use the sidebar to explore my work on the project.", className="lead"
        ),
        dbc.Nav(
            [
                dbc.NavLink("Home", href="/", active="exact", external_link=True),
                # dbc.NavLink("Overview", href="/overview", active="exact", external_link=True),
                dbc.NavLink("Stringency", href="/stringency", active="exact", external_link=True),
                dbc.NavLink("Multi-sentiment Analysis", href="/multi_sentiment_analysis", active="exact", external_link=True),
                dbc.NavLink("Topic Modeling", href="/topic_modeling", active="exact", external_link=True),
                # dbc.NavLink("Question Answering", href="/question_answering", active="exact", external_link=True),
                dbc.NavLink("Event Detection", href="/event_detection", active="exact", external_link=True),
            ],
            vertical=True,
            pills=True,
        ),
    ],
    style=SIDEBAR_STYLE,
)

content = html.Div(id="page-content", style=CONTENT_STYLE)

app.layout = html.Div([
    dcc.Location(id="url", refresh=False),
    sidebar,
    content
])



## -------------- CALLBACKS --------------



@app.callback(Output("page-content", "children"), Input('url', 'pathname'))
def render_page_content(pathname):
    if pathname == "/":
        return app_home.layout
    # elif pathname == "/overview":
    #     return app_overview.layout
    elif pathname == "/multi_sentiment_analysis":
        return app_multi_sentiment_analysis.layout
    elif pathname == "/stringency":
        return app_stringency.layout
    elif pathname == "/topic_modeling":
        return app_topic_modeling.layout
    # elif pathname == "/question_asnwering":
    #     return app_question_answering.layout
    elif pathname == "/event_detection":
        return app_event_detection.layout
    # If the user tries to reach a different page, return a 404 message
    return dbc.Jumbotron(
        [
            html.H1("404: Not found", className="text-danger"),
            html.Hr(),
            html.P(f"The pathname {pathname} was not recognised..."),
        ]
    )





# ------- Stringency -------
from datetime import date
import pandas as pd
import plotly.express as px

from apps.app_stringency import get_json



@app.callback(
    [
        Output('output-container-date-picker-range-stringency', 'children'),
        Output('plotly-figure-stringency', 'figure'),
        Output('plotly-choropleth-animation', 'figure'),
        Output('dropdown-feature-stringency', 'options'),
        Output('multi-dropdown-country-code-stringency', 'options')
    ],
    [
        Input('date-picker-range-stringency', 'start_date'),
        Input('date-picker-range-stringency', 'end_date'),
        Input('dropdown-feature-stringency', 'value'),
        Input('multi-dropdown-country-code-stringency', 'value')
    ])
def update_output(start_date, end_date, feature, list_country_code):
    if list_country_code is None:
        list_country_code = []

    if start_date is not None:
        start_date_object = date.fromisoformat(start_date)
        start_date_string = start_date_object.strftime('%B %d, %Y')
    if end_date is not None:
        end_date_object = date.fromisoformat(end_date)
        end_date_string = end_date_object.strftime('%B %d, %Y')
    
    if start_date is not None or end_date is not None:
        string_prefix = f'Start Date: {start_date_string} | End Date: {end_date_string}'
        print(string_prefix)

        json_data = get_json(start_date_object, end_date_object)

        data_dates = [elt for elt in list(json_data['data'].values())]

        data = []
        for dic in data_dates:
            data.extend(list(dic.values()))
        
        df = pd.DataFrame(data)
        df['date_value'] = pd.to_datetime(df['date_value'])

        print(f"list_country_code: {list_country_code}")

        data = df[df['country_code'].isin(list_country_code)]
        print(data.columns)

        # Getting the Plotly figure:
        fig_1 = px.line(
            data,
            x="date_value", 
            y=feature, 
            # color="country_code",
            hover_data=data.columns
        )

        fig_1.update_layout(title_text = 'Evolution of the given feature with respect to time in the selected countries')

        fig_2 = None

        # fig_2 = px.choropleth(df, locations="country_code",
        #             color="stringency",
        #             animation_frame="year-week",
        #             title = "Evolution of stringency in the world",
        #             color_continuous_scale=px.colors.sequential.PuRd)
        

        list_options_dropdown_features = [elt for elt in df.columns.to_list() if elt not in ["date_value", "country_code"]]
        options_dropdown_features = [{'label': elt, 'value': elt} for elt in list_options_dropdown_features]

        list_options_dropdown_country_code = sorted(df['country_code'].unique().tolist())
        options_dropdown_country_code = [{'label': elt, 'value': elt} for elt in list_options_dropdown_country_code]

        return string_prefix, fig_1, fig_2, options_dropdown_features, options_dropdown_country_code

    else:
        return 'Select a date to see it displayed here', None, None, None, None









# ------- Event Detection -------
from event_detection.parse_WHO import parse_WHO
dic_events = parse_WHO()

@app.callback(
    Output('textarea-event-detection', 'value'),
    Input('dropdown-event-detection-date', 'value')
)
def event_detection_update(date):
    return dic_events[date]













if __name__ == "__main__":
    app.run_server(debug=True)
<filename>Part3-Article-NLP/index.py
import os

import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

from app import app

# from apps import app_home, app_overview, app_stringency, app_multi_sentiment_analysis, app_topic_modeling, app_question_answering, app_event_detection
from apps import app_home, app_stringency, app_multi_sentiment_analysis, app_topic_modeling, app_event_detection

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP], suppress_callback_exceptions=True)

# the style arguments for the sidebar. We use position:fixed and a fixed width
SIDEBAR_STYLE = {
    "position": "fixed",
    "top": 0,
    "left": 0,
    "bottom": 0,
    "width": "16rem",
    "padding": "2rem 1rem",
    "background-color": "#f8f9fa",
}

# the styles for the main content position it to the right of the sidebar and
# add some padding.
CONTENT_STYLE = {
    "margin-left": "18rem",
    "margin-right": "2rem",
    "padding": "2rem 1rem",
}

sidebar = html.Div(
    [
        html.H2("Summary", className="display-4"),
        html.Hr(),
        html.P(
            "Use the sidebar to explore my work on the project.", className="lead"
        ),
        dbc.Nav(
            [
                dbc.NavLink("Home", href="/", active="exact", external_link=True),
                # dbc.NavLink("Overview", href="/overview", active="exact", external_link=True),
                dbc.NavLink("Stringency", href="/stringency", active="exact", external_link=True),
                dbc.NavLink("Multi-sentiment Analysis", href="/multi_sentiment_analysis", active="exact", external_link=True),
                dbc.NavLink("Topic Modeling", href="/topic_modeling", active="exact", external_link=True),
                # dbc.NavLink("Question Answering", href="/question_answering", active="exact", external_link=True),
                dbc.NavLink("Event Detection", href="/event_detection", active="exact", external_link=True),
            ],
            vertical=True,
            pills=True,
        ),
    ],
    style=SIDEBAR_STYLE,
)

content = html.Div(id="page-content", style=CONTENT_STYLE)

app.layout = html.Div([
    dcc.Location(id="url", refresh=False),
    sidebar,
    content
])



## -------------- CALLBACKS --------------



@app.callback(Output("page-content", "children"), Input('url', 'pathname'))
def render_page_content(pathname):
    if pathname == "/":
        return app_home.layout
    # elif pathname == "/overview":
    #     return app_overview.layout
    elif pathname == "/multi_sentiment_analysis":
        return app_multi_sentiment_analysis.layout
    elif pathname == "/stringency":
        return app_stringency.layout
    elif pathname == "/topic_modeling":
        return app_topic_modeling.layout
    # elif pathname == "/question_asnwering":
    #     return app_question_answering.layout
    elif pathname == "/event_detection":
        return app_event_detection.layout
    # If the user tries to reach a different page, return a 404 message
    return dbc.Jumbotron(
        [
            html.H1("404: Not found", className="text-danger"),
            html.Hr(),
            html.P(f"The pathname {pathname} was not recognised..."),
        ]
    )





# ------- Stringency -------
from datetime import date
import pandas as pd
import plotly.express as px

from apps.app_stringency import get_json



@app.callback(
    [
        Output('output-container-date-picker-range-stringency', 'children'),
        Output('plotly-figure-stringency', 'figure'),
        Output('plotly-choropleth-animation', 'figure'),
        Output('dropdown-feature-stringency', 'options'),
        Output('multi-dropdown-country-code-stringency', 'options')
    ],
    [
        Input('date-picker-range-stringency', 'start_date'),
        Input('date-picker-range-stringency', 'end_date'),
        Input('dropdown-feature-stringency', 'value'),
        Input('multi-dropdown-country-code-stringency', 'value')
    ])
def update_output(start_date, end_date, feature, list_country_code):
    if list_country_code is None:
        list_country_code = []

    if start_date is not None:
        start_date_object = date.fromisoformat(start_date)
        start_date_string = start_date_object.strftime('%B %d, %Y')
    if end_date is not None:
        end_date_object = date.fromisoformat(end_date)
        end_date_string = end_date_object.strftime('%B %d, %Y')
    
    if start_date is not None or end_date is not None:
        string_prefix = f'Start Date: {start_date_string} | End Date: {end_date_string}'
        print(string_prefix)

        json_data = get_json(start_date_object, end_date_object)

        data_dates = [elt for elt in list(json_data['data'].values())]

        data = []
        for dic in data_dates:
            data.extend(list(dic.values()))
        
        df = pd.DataFrame(data)
        df['date_value'] = pd.to_datetime(df['date_value'])

        print(f"list_country_code: {list_country_code}")

        data = df[df['country_code'].isin(list_country_code)]
        print(data.columns)

        # Getting the Plotly figure:
        fig_1 = px.line(
            data,
            x="date_value", 
            y=feature, 
            # color="country_code",
            hover_data=data.columns
        )

        fig_1.update_layout(title_text = 'Evolution of the given feature with respect to time in the selected countries')

        fig_2 = None

        # fig_2 = px.choropleth(df, locations="country_code",
        #             color="stringency",
        #             animation_frame="year-week",
        #             title = "Evolution of stringency in the world",
        #             color_continuous_scale=px.colors.sequential.PuRd)
        

        list_options_dropdown_features = [elt for elt in df.columns.to_list() if elt not in ["date_value", "country_code"]]
        options_dropdown_features = [{'label': elt, 'value': elt} for elt in list_options_dropdown_features]

        list_options_dropdown_country_code = sorted(df['country_code'].unique().tolist())
        options_dropdown_country_code = [{'label': elt, 'value': elt} for elt in list_options_dropdown_country_code]

        return string_prefix, fig_1, fig_2, options_dropdown_features, options_dropdown_country_code

    else:
        return 'Select a date to see it displayed here', None, None, None, None









# ------- Event Detection -------
from event_detection.parse_WHO import parse_WHO
dic_events = parse_WHO()

@app.callback(
    Output('textarea-event-detection', 'value'),
    Input('dropdown-event-detection-date', 'value')
)
def event_detection_update(date):
    return dic_events[date]













if __name__ == "__main__":
    app.run_server(debug=True)
# from apps import app_home, app_overview, app_stringency, app_multi_sentiment_analysis, app_topic_modeling, app_question_answering, app_event_detection # the style arguments for the sidebar. We use position:fixed and a fixed width # the styles for the main content position it to the right of the sidebar and # add some padding. # dbc.NavLink("Overview", href="/overview", active="exact", external_link=True), # dbc.NavLink("Question Answering", href="/question_answering", active="exact", external_link=True), ## -------------- CALLBACKS -------------- # elif pathname == "/overview": #     return app_overview.layout # elif pathname == "/question_asnwering": #     return app_question_answering.layout # If the user tries to reach a different page, return a 404 message # ------- Stringency ------- # Getting the Plotly figure: # color="country_code", # fig_2 = px.choropleth(df, locations="country_code", #             color="stringency", #             animation_frame="year-week", #             title = "Evolution of stringency in the world", #             color_continuous_scale=px.colors.sequential.PuRd) # ------- Event Detection -------