def select_contest(contest): if contest is None: contest = "Mayor - City of St. Louis" contest_name = mec_query.get_standard_contest_name(contest) print(contest_name) candidate_df = pd.read_csv("dsadata/static/candidates_2021-03-02.csv") contest_candidates_df = candidate_df[candidate_df["Office Sought"] == contest] select_options = [{"label": "All candidates", "value": "all"}] for index, row in contest_candidates_df.iterrows(): select_options.append({ "label": row["Candidate Name"].title(), "value": row["MECID"] }) return [ select_options, "all", "dsadata/static/geobuf/" + mec_query.get_standard_contest_name(contest_name) + "-stl-city-and-county-precincts.pbf", "dsadata/static/geobuf/" + mec_query.get_standard_contest_name(contest_name) + "-neighborhoods-and-municipalities.pbf", "dsadata/static/geobuf/" + mec_query.get_standard_contest_name(contest_name) + "-stl-region-zip.pbf", ]
def candidate_selected(selected_mec_id, include_pacs, contest): if contest is None: contest = "Mayor - City of St. Louis" contest_name = mec_query.get_standard_contest_name(contest) if selected_mec_id != "all": color_prop = "mec_donations_" + selected_mec_id if "include_pacs" in include_pacs: color_prop = color_prop + "_with_pacs" hideout = bootstrap_stuff.build_choropleth_hideout(color_prop) return ( True, [], # [bootstrap_stuff.get_candidate_info_card(candidate_row)], hideout, hideout, hideout, [plotting.build_contest_info_graph(contest) ], # Change this to candidate info at some point ) color_prop = "total_monetary_donations_" + contest_name if "include_pacs" in include_pacs: color_prop = color_prop + "_with_pacs" hideout = bootstrap_stuff.build_choropleth_hideout(color_prop) return ( False, [], hideout, hideout, hideout, [plotting.build_contest_info_graph(contest)], )
def select_contest(contest): if contest is None: contest = "Mayor - City of St. Louis" contest_name = mec_query.get_standard_contest_name(contest) candidate_df = pd.read_sql("candidate", db.engine) contest_candidates_df = candidate_df[candidate_df["Office Sought"] == contest] contest_candidates_df = contest_candidates_df.sort_values( "Candidate Name") select_options = [{"label": "All candidates", "value": "all"}] for index, row in contest_candidates_df.iterrows(): select_options.append({ "label": row["Candidate Name"].title(), "value": row["MECID"] }) return [ select_options, "all", url_for("static", filename="geobuf/") + contest_name + "-stl-city-and-county-precincts.pbf", url_for("static", filename="geobuf/") + contest_name + "-neighborhoods-and-municipalities.pbf", url_for("static", filename="geobuf/") + contest_name + "-stl-region-zip.pbf", ]
def zip_click(feature, n_clicks, contest): contest_name = mec_query.get_standard_contest_name(contest) class_name = "displayNone" header_text = "Error" card_contents = bootstrap_stuff.get_floatbox_card_contents("zip") if feature: header_text = f"ZIP Code {feature['properties']['ZCTA5CE10']}" body_contents = [ html.Strong("Total monetary donations: "), html.Span( locale.currency( feature["properties"]["total_monetary_donations_" + contest_name], grouping=True, )), ] class_name = "floatbox" card_contents = bootstrap_stuff.get_floatbox_card_contents( "zip", header_text, body_contents) if n_clicks: class_name = "displayNone" return [card_contents, class_name]
def precinct_click(feature, n_clicks, contest): contest_name = mec_query.get_standard_contest_name(contest) class_name = "displayNone" header_text = "Error" card_contents = bootstrap_stuff.get_floatbox_card_contents("precinct") if feature: # print(feature["properties"]) if ("WARD10" in feature["properties"] and feature["properties"]["WARD10"]): # STL City precinct header_text = f"STL City: Ward {feature['properties']['WARD10']}, Precinct {feature['properties']['PREC10']}" elif feature["properties"]["PRECINCTID"]: # STL County precinct header_text = ( f"STL County: Precinct {feature['properties']['PRECINCTID']}" ) body_contents = [ html.Strong("Total monetary donations: "), html.Span( locale.currency( feature["properties"]["total_monetary_donations_" + contest_name], grouping=True, )), ] class_name = "floatbox" card_contents = bootstrap_stuff.get_floatbox_card_contents( "precinct", header_text, body_contents) if n_clicks: class_name = "displayNone" return [card_contents, class_name]
def neighborhood_click(feature, n_clicks, contest): contest_name = mec_query.get_standard_contest_name(contest) class_name = "displayNone" header_text = "Error" card_contents = bootstrap_stuff.get_floatbox_card_contents( "neighborhood") if feature: print(feature) if ("NHD_NAME" in feature["properties"] and feature["properties"]["NHD_NAME"]): header_text = feature["properties"]["NHD_NAME"] else: header_text = feature["properties"]["MUNICIPALI"].title() body_contents = [ html.Strong("Total monetary donations: "), html.Span( locale.currency( feature["properties"]["total_monetary_donations_" + contest_name], grouping=True, )), ] class_name = "floatbox" card_contents = bootstrap_stuff.get_floatbox_card_contents( "neighborhood", header_text, body_contents) if n_clicks: class_name = "displayNone" return [card_contents, class_name]
def get_floatbox_card_contents( id_suffix, header_text="", contest="Mayor - City of St. Louis", feature_properties={}, ): header_style = {"fontSize": "1.5em", "fontWeight": "bold"} header_span = html.Span(header_text, style=header_style) close_card_button = dbc.Button( " X ", outline=True, color="danger", id="card-box-close-" + id_suffix, style={"float": "right"}, ) if bool(feature_properties): contest_name = mec_query.get_standard_contest_name(contest) if (feature_properties["total_monetary_donations_" + contest_name + "_with_pacs"] > 0): pie_plot = plotting.create_candidate_funds_pie( contest, feature_properties) else: pie_plot = [] card_contents = dbc.CardBody([ html.Div([ html.Div([ html.Strong("Total donations in race for " + contest_name + ": "), html.Span( locale.currency( feature_properties["total_monetary_donations_" + contest_name + "_with_pacs"], grouping=True, )), ]), ]), pie_plot, html.Div( html. Em("(Contributions where donor info was missing or invalid are not included on the map)" ), style={ "fontSize": ".9em", "lineHeight": "1em" }, ), ]) else: card_contents = [] return [dbc.CardHeader([header_span, close_card_button]), card_contents]
def candidate_selected(selected_mec_id, contest): if contest is None: contest = "Mayor - City of St. Louis" contest_name = mec_query.get_standard_contest_name(contest) if selected_mec_id != "all": color_prop = "mec_donations_" + selected_mec_id hideout = bootstrap_stuff.build_choropleth_hideout(color_prop) return ( True, [], # [bootstrap_stuff.get_candidate_info_card(candidate_row)], hideout, hideout, hideout, ) hideout = bootstrap_stuff.build_choropleth_hideout( "total_monetary_donations_" + contest_name) return (False, [], hideout, hideout, hideout)
def create_candidate_funds_pie(contest, geography_properties): contest_name = mec_query.get_standard_contest_name(contest) contest_candidates_df = candidate_df[candidate_df["Office Sought"] == contest] contest_candidates_df.loc[:, "Fundraising"] = contest_candidates_df.apply( lambda x: parse_geography_properties_for_fundraising( geography_properties, x["MECID"]), axis=1, ) color_discrete_map = get_candidate_colors(contest_candidates_df) fig = px.pie( contest_candidates_df, values="Fundraising", color="Candidate Name", names="Candidate Name", hover_name="Candidate Name", color_discrete_map=get_candidate_colors(contest_candidates_df), hole=0.3, width=250, height=250, ) fig.update_traces( textinfo="none", hovertemplate="<b>%{label}</b><br>Funds raised here: $%{value}", automargin=True, ) fig.update_layout(showlegend=False, margin=dict(l=10, r=10, t=10, b=10)) pie_graph = dcc.Graph( id="geography-pie-graph", figure=fig, config={ "displayModeBar": False, # 'staticPlot': True }, ) return html.Div([pie_graph], style={"width": "250px", "margin": "auto"})