File size: 6,025 Bytes
fcd3e5f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 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,
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_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()}"
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
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
with ui.navset_card_underline(title=""):
with ui.nav_panel("Rules in detail"):
@render.data_frame
def table_rule_detail():
df = filtered_df()
#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.apply(lambda x: f"{x['publication_date'].date()}", axis=1)
char = " "
df.loc[:, "title"] = df["title"].apply(lambda x: f"{char.join(x.split(char)[:9])}...")
df.loc[:, "agencies"] = df["parent_slug"].apply(lambda x: "; ".join(x))
cols = [
"date",
"title",
"agencies",
"3f1_significant",
"other_significant",
]
return render.DataTable(df.loc[:, [c for c in cols if c in df.columns]])
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()
return plot_agency(
grouped.head(10),
)
@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
#filt_df = df[df["species"].isin(input.species())]
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))]
return filt_df
@reactive.calc
def grouped_df_month():
filt_df = filtered_df()
grouped = groupby_ym(filt_df, significant=GET_SIGNIFICANT)
return grouped
@reactive.calc
def grouped_df_agency():
filt_df = filtered_df()
grouped = groupby_agency(filt_df, metadata=METADATA, significant=GET_SIGNIFICANT)
return grouped
|