Download app.py from mfebrizio/cra-window-rules: direct link, hf CLI and curl.
- Browser
- Download file 6.03 kB
-
https://huggingface.co/mfebrizio/cra-window-rules/resolve/main/app.py
- Command line
-
hf download hf://mfebrizio/cra-window-rules/app.py
-
curl -L -o app.py https://huggingface.co/mfebrizio/cra-window-rules/resolve/main/app.py
6.03 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, | |
| 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" | |
| def count_rules(): | |
| return f"{filtered_df()['document_number'].count()}" | |
| with ui.value_box(showcase=icon_svg("book")): | |
| "Other Significant rules" | |
| 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" | |
| 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"): | |
| 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(): | |
| def plot_by_month(): | |
| grouped = grouped_df_month() | |
| return plot_month( | |
| grouped | |
| ) | |
| 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(): | |
| def plot_by_agency(): | |
| grouped = grouped_df_agency() | |
| return plot_agency( | |
| grouped.head(10), | |
| ) | |
| 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"): | |
| 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 ----- # | |
| 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 | |
| def grouped_df_month(): | |
| filt_df = filtered_df() | |
| grouped = groupby_ym(filt_df, significant=GET_SIGNIFICANT) | |
| return grouped | |
| def grouped_df_agency(): | |
| filt_df = filtered_df() | |
| grouped = groupby_agency(filt_df, metadata=METADATA, significant=GET_SIGNIFICANT) | |
| return grouped | |