Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes v2 dev website dashboard not loading #1256

Merged
merged 19 commits into from
Jul 15, 2022
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 40 additions & 46 deletions server/dash/dashboards/neighborhood_recent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output
import pandas as pd
import plotly.express as px
from flask import request
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved

from app import app, batch_get_data
from config import API_HOST
from dash.dependencies import Input, Output
from design import CONFIG_OPTIONS, DISCRETE_COLORS, DISCRETE_COLORS_MAP, LABELS, apply_figure_style
from flask import request
from design import CONFIG_OPTIONS, DISCRETE_COLORS, LABELS, apply_figure_style, DISCRETE_COLORS_MAP


pretty_columns = {
'srnumber': "SR Number",
Expand All @@ -22,8 +24,10 @@
'address': "Address"
}

start_date = datetime.date.today() - datetime.timedelta(days=7)
end_date = datetime.date.today() - datetime.timedelta(days=1)
START_DATE_DELTA = 7
END_DATE_DELTA = 1
start_date = datetime.date.today() - datetime.timedelta(days=START_DATE_DELTA)
end_date = datetime.date.today() - datetime.timedelta(days=END_DATE_DELTA)

# TITLE
title = "NEIGHBORHOOD WEEKLY REPORT"
Expand All @@ -44,10 +48,16 @@
selected_council = 'Arleta'

table_df = df.query(f"councilName == '{selected_council}'")[['srnumber', 'createdDate', 'closedDate', 'typeName', 'agencyName', 'sourceName', 'address']] # noqa
figure_df = df.query(f"councilName == '{selected_council}' and createdDate >= '{start_date}'").groupby(['createdDate', 'typeName'])['srnumber'].count().reset_index() # noqa

# figure_df = df.query(f"councilName == '{selected_council}' and createdDate >= '{start_date}'").groupby(['createdDate', 'typeName'], as_index=False)['srnumber'].count() # noqa

# Populate the neighborhood dropdown
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
fig = px.line()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to have more descriptive variable names, e.g., requests_over_time_line_fig.

apply_figure_style(fig)

fig2 = px.pie()
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
apply_figure_style(fig2)


def populate_options():
council_df_path = '/councils'
council_df = pd.read_json(API_HOST + council_df_path)
Expand All @@ -60,39 +70,6 @@ def populate_options():
return values


fig = px.line(
figure_df,
x="createdDate",
y="srnumber",
color="typeName",
color_discrete_sequence=DISCRETE_COLORS,
color_discrete_map=DISCRETE_COLORS_MAP,
labels=LABELS,
)

fig.update_xaxes(
tickformat="%a\n%m/%d",
)

fig.update_traces(
mode='markers+lines'
) # add markers to lines

apply_figure_style(fig)

pie_fig = px.pie(
figure_df,
names="typeName",
values="srnumber",
color="typeName",
color_discrete_sequence=DISCRETE_COLORS,
color_discrete_map=DISCRETE_COLORS_MAP,
labels=LABELS,
hole=.3,
)
apply_figure_style(pie_fig)


# Layout
layout = html.Div([
html.H1(title),
Expand Down Expand Up @@ -124,7 +101,7 @@ def populate_options():
className="half-graph"
),
html.Div(
dcc.Graph(id='pie_graph', figure=pie_fig, config=CONFIG_OPTIONS),
dcc.Graph(id='pie_graph', figure=fig2, config=CONFIG_OPTIONS),
className="half-graph"
)
]),
Expand Down Expand Up @@ -162,7 +139,12 @@ def populate_options():
)
def update_table(selected_council):
table_df = df.query(f"councilName == '{selected_council}'")[['srnumber', 'createdDate', 'closedDate', 'typeName', 'agencyName', 'sourceName', 'address']] # noqa
return table_df.to_dict('records')
if table_df.shape[0] == 0:
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
table_df = pd.DataFrame(columns=["Request Type"])
for i in range(len(DISCRETE_COLORS_MAP.keys())):
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
table_df.loc[i] = [list(DISCRETE_COLORS_MAP.keys())[i]]
else:
return table_df.to_dict('records')


@app.callback(
Expand All @@ -176,7 +158,10 @@ def update_table(selected_council):
def update_text(selected_council):
create_count = df.query(f"councilName == '{selected_council}' and createdDate >= '{start_date}'")['srnumber'].count() # noqa
close_count = df.query(f"councilName == '{selected_council}' and closedDate >= '{start_date}'")['srnumber'].count() # noqa
return create_count, close_count, create_count - close_count
if create_count == 0 and close_count == 0:
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
return 0, 0, 0
else:
return create_count, close_count, create_count - close_count


@app.callback(
Expand All @@ -186,7 +171,12 @@ def update_text(selected_council):
def update_figure(selected_council):
figure_df = df.query(f"councilName == '{selected_council}' and createdDate >= '{start_date}'").groupby(['createdDate', 'typeName'])['srnumber'].count().reset_index() # noqa
figure_df.typeName = figure_df.typeName.map(lambda x: '<br>'.join(textwrap.wrap(x, width=16))) # noqa
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved

if figure_df.shape[0] == 0:
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
figure_df = pd.DataFrame(columns=['createdDate', "srnumber", "typeName"])
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
for j in range(len(DISCRETE_COLORS_MAP.keys())):
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
for i in range(len(DISCRETE_COLORS_MAP.keys())):
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
figure_df.loc[figure_df.shape[0]] = [start_date +
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
datetime.timedelta(days=j), 0, list(DISCRETE_COLORS_MAP.keys())[i]]
fig = px.line(
figure_df,
x="createdDate",
Expand All @@ -209,12 +199,16 @@ def update_figure(selected_council):


@app.callback(
Output("pie_graph", "pie_fig"),
Output("pie_graph", "figure"),
Input("council_list", "value")
)
def update_council_figure(selected_council):
pie_df = df.query(f"councilName == '{selected_council}' and createdDate >= '{start_date}'").groupby(['typeName']).agg('count').reset_index() # noqa

if pie_df.shape[0] == 0:
pie_df = pd.DataFrame(columns=["srnumber", "typeName"])
pie_df.loc[0] = [12345678, "No Request at this time"]
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
for i in range(len(DISCRETE_COLORS_MAP.keys())):
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
pie_df.loc[i] = [12345678, list(DISCRETE_COLORS_MAP.keys())[i]]
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
pie_fig = px.pie(
pie_df,
names="typeName",
Expand Down