Mark Febrizio
Update app.py
e6cbeaa
raw
history blame
7.41 kB
import asyncio
from datetime import datetime, date, time
from faicons import icon_svg
from modules.get_rules_in_window import (
DF,
LAST_UPDATED,
START_DATE,
GET_SIGNIFICANT,
METADATA,
AGENCIES,
groupby_agency,
groupby_ym,
plot_agency,
plot_month,
)
from shiny import reactive
from shiny.express import input, render, ui
FOOTER = f"""
-----
Developed by the [GW Regulatory Studies Center](https://go.gwu.edu/regstudies). See our page on the [Congressional Review Act](https://regulatorystudies.columbian.gwu.edu/congressional-review-act) for more information.
"""
ui.page_opts(
title="Rules in the Congressional Review Act (CRA) Window", #fillable=True,
)
with ui.sidebar(title="Settings"):
ui.input_date("start_date", "Start of window", value=START_DATE, min=START_DATE, max=date.today())
ui.input_switch("switch", "Show significant rules in plots", False)
ui.input_select("menu_agency", "Select agencies", choices=["all"] + AGENCIES, selected="all")
#ui.input_checkbox_group(
# "significant",
# "EO 12866 Significance",
# ["Section 3(f)(1)", "Other"],
#)
with ui.layout_column_wrap():
with ui.value_box(showcase=icon_svg("book")):
"All final rules"
@render.text
def count_rules():
return f"{filtered_df()['document_number'].count()}"
ui.input_action_button("filter_all", "View", ) #class_="btn-success")
with ui.value_box(showcase=icon_svg("book")):
"Section 3(f)(1) Significant rules"
@render.text
def count_3f1_significant():
output = "Not available"
if GET_SIGNIFICANT:
output = f"{filtered_df()['3f1_significant'].sum()}"
return output
ui.input_action_button("filter_3f1", "View", ) #class_="btn-success")
with ui.value_box(showcase=icon_svg("book")):
"Other Significant rules"
@render.text
def count_other_significant():
output = "Not available"
if GET_SIGNIFICANT:
output = f"{filtered_df()['other_significant'].sum()}"
return output
ui.input_action_button("filter_other", "View", )
with ui.navset_card_underline(title=""):
with ui.nav_panel("Rules in detail"):
@render.data_frame
def table_rule_detail():
df = filtered_sig()
#print(df.columns)
#df.loc[:, "date"] = df.apply(lambda x: f"{x['publication_year']}-{x['publication_month']}-{x['publication_day']}", axis=1)
df.loc[:, "date"] = df.loc[:, "publication_date"].apply(lambda x: f"{x.date()}")
char, limit = " ", 10
df.loc[:, "title"] = df["title"].apply(lambda x: x if len(x.split(char)) < (limit + 1) else f"{char.join(x.split(char)[:limit])}...")
df.loc[:, "agencies"] = df["parent_slug"].apply(lambda x: "; ".join(x))
cols = [
"date",
"title",
"agencies",
"3f1_significant",
"other_significant",
]
return render.DataGrid(df.loc[:, [c for c in cols if c in df.columns]], width="100%") #filters=True)
with ui.nav_panel("By month"):
with ui.layout_columns():
@render.plot
def plot_by_month():
grouped = grouped_df_month()
return plot_month(
grouped
)
@render.data_frame
def table_by_month():
grouped = grouped_df_month()
cols = [
"publication_year",
"publication_month",
"rules",
"3f1_significant",
"other_significant",
]
return render.DataTable(grouped.loc[:, [c for c in cols if c in grouped.columns]])
with ui.nav_panel("By agency"):
with ui.layout_columns():
@render.plot
def plot_by_agency():
grouped = grouped_df_agency()
if input.switch():
pass
# placeholder for stacked bar chart
else:
plot = plot_agency(
grouped.head(10),
)
return plot
@render.data_frame
def table_by_agency():
grouped = grouped_df_agency()
cols = [
"agency",
"acronym",
"rules",
"3f1_significant",
"other_significant",
]
return render.DataTable(grouped.loc[:, [c for c in cols if c in grouped.columns]])
with ui.accordion(open=False):
with ui.accordion_panel("Download Data"):
@render.download(
label="Download data as CSV",
filename=f"rules_in_cra_window_accessed_{date.today()}.csv",
)
async def download():
await asyncio.sleep(0.25)
yield filtered_df().to_csv(index=False)
with ui.accordion(open=False):
with ui.accordion_panel("Notes"):
ui.markdown(
f"""
Rule data retrieved from the [Federal Register API](https://www.federalregister.gov/developers/documentation/api/v1).
Executive Order 12866 significance data last updated **{LAST_UPDATED}**.
"""
)
ui.markdown(
FOOTER
)
#ui.tags.footer()
# ----- REACTIVE CALCULATIONS ----- #
@reactive.calc
def filtered_df():
filt_df = DF
# filter dates
try:
filt_df = filt_df.loc[filt_df["publication_date"] >= input.start_date()]
except TypeError:
filt_df = filt_df.loc[filt_df["publication_date"] >= datetime.combine(input.start_date(), time(0, 0))]
# filter agencies
if input.menu_agency() != "all":
bool_agency = [True if input.menu_agency() in agency else False for agency in filt_df["parent_slug"]]
filt_df = filt_df.loc[bool_agency]
return filt_df
@reactive.calc
def filtered_sig():
filt_df = filtered_df()
# filter significance
if filter_value.get() == "all":
pass
elif filter_value.get() == "3f1":
filt_df = filt_df.loc[filt_df["3f1_significant"] == 1]
elif filter_value.get() == "other":
filt_df = filt_df.loc[filt_df["other_significant"] == 1]
return filt_df
@reactive.calc
def grouped_df_month():
filt_df = filtered_sig()
grouped = groupby_ym(filt_df, significant=GET_SIGNIFICANT)
return grouped
@reactive.calc
def grouped_df_agency():
filt_df = filtered_sig()
grouped = groupby_agency(filt_df, metadata=METADATA, significant=GET_SIGNIFICANT)
return grouped
# ----- REACTIVE VALUES ----- #
filter_value = reactive.value("all")
@reactive.effect
@reactive.event(input.filter_all)
def _():
filter_value.set("all")
filtered_df()
@reactive.effect
@reactive.event(input.filter_3f1)
def _():
filter_value.set("3f1")
filtered_df()
@reactive.effect
@reactive.event(input.filter_other)
def _():
filter_value.set("other")
filtered_df()