|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import matplotlib.dates as mdates |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +import pandas as pd |
| 6 | +import seaborn as sns |
| 7 | + |
| 8 | +from shiny import App, Inputs, Outputs, Session, reactive, render, ui |
| 9 | +from shiny.plotutils import brushed_points |
| 10 | + |
| 11 | +path = Path(__file__).parent / "boulder_temp.csv" |
| 12 | +weather_df = pd.read_csv(path) |
| 13 | +weather_df["date"] = pd.to_datetime(weather_df["date"]) |
| 14 | +weather_df["annotation"] = "" |
| 15 | + |
| 16 | +app_ui = ui.page_fluid( |
| 17 | + ui.panel_title("Plot annotation example"), |
| 18 | + ui.p( |
| 19 | + """ |
| 20 | + Select points to annotate them. |
| 21 | + The plot is rendered with seaborn and all interaction is handled by Shiny. |
| 22 | + """, |
| 23 | + {"style": "font-size: larger"}, |
| 24 | + ), |
| 25 | + ui.row( |
| 26 | + ui.column( |
| 27 | + 6, |
| 28 | + ui.output_plot("time_series", brush=ui.brush_opts(direction="x")), |
| 29 | + ui.output_ui("annotator"), |
| 30 | + ), |
| 31 | + ui.column( |
| 32 | + 4, |
| 33 | + ui.h3("Annotated points"), |
| 34 | + ui.output_data_frame("annotations"), |
| 35 | + ), |
| 36 | + ui.column(2, ui.download_button("download", "Download CSV")), |
| 37 | + ), |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +def server(input: Inputs, output: Outputs, session: Session): |
| 42 | + annotated_data = reactive.Value(weather_df) |
| 43 | + |
| 44 | + @reactive.Calc |
| 45 | + def selected_data(): |
| 46 | + out = brushed_points(annotated_data(), input.time_series_brush(), xvar="date") |
| 47 | + return out |
| 48 | + |
| 49 | + @reactive.Effect |
| 50 | + @reactive.event(input.annotate_button) |
| 51 | + def _(): |
| 52 | + selected = selected_data() |
| 53 | + selected["annotation_new"] = input.annotation() |
| 54 | + selected = selected.loc[:, ["date", "annotation_new"]] |
| 55 | + |
| 56 | + df = annotated_data().copy() |
| 57 | + |
| 58 | + df = df.merge(selected, on="date", how="left") |
| 59 | + df["annotation_new"] = df["annotation_new"].fillna("") |
| 60 | + updated_rows = df["annotation_new"] != "" |
| 61 | + df.loc[updated_rows, "annotation"] = df.loc[updated_rows, "annotation_new"] |
| 62 | + df = df.loc[:, ["date", "temp_c", "annotation"]] |
| 63 | + annotated_data.set(df) |
| 64 | + |
| 65 | + @output |
| 66 | + @render.plot |
| 67 | + def time_series(): |
| 68 | + fig, ax = plt.subplots() |
| 69 | + ax.xaxis.set_major_locator(mdates.MonthLocator()) |
| 70 | + ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m")) |
| 71 | + ax.set_title("Temperature readings, Boulder Colorado") |
| 72 | + out = sns.scatterplot( |
| 73 | + data=annotated_data(), x="date", y="temp_c", hue="annotation", ax=ax |
| 74 | + ) |
| 75 | + |
| 76 | + out.tick_params(axis="x", rotation=30) |
| 77 | + return out.get_figure() |
| 78 | + |
| 79 | + @output |
| 80 | + @render.ui |
| 81 | + def annotator(): |
| 82 | + if input.time_series_brush() is not None: |
| 83 | + selected = selected_data() |
| 84 | + |
| 85 | + min = str(selected["date"].min()) |
| 86 | + max = str(selected["date"].max()) |
| 87 | + |
| 88 | + min = min.replace(" 00:00:00+00:00", "") |
| 89 | + max = max.replace(" 00:00:00+00:00", "") |
| 90 | + |
| 91 | + out = ui.TagList( |
| 92 | + ui.row( |
| 93 | + {"style": "padding-top: 20px;"}, |
| 94 | + ui.column( |
| 95 | + 4, |
| 96 | + ui.p(f"{min} to", ui.br(), f"{max}"), |
| 97 | + ), |
| 98 | + ui.column( |
| 99 | + 4, |
| 100 | + ui.input_text("annotation", "", placeholder="Enter annotation"), |
| 101 | + ), |
| 102 | + ui.column(4, ui.input_action_button("annotate_button", "Submit")), |
| 103 | + ) |
| 104 | + ) |
| 105 | + return out |
| 106 | + |
| 107 | + @output |
| 108 | + @render.data_frame |
| 109 | + def annotations(): |
| 110 | + df = annotated_data().copy() |
| 111 | + df["date"] = df["date"].dt.strftime("%Y-%m-%d") |
| 112 | + df = df.loc[df["annotation"] != ""] |
| 113 | + return df |
| 114 | + |
| 115 | + @session.download(filename="data.csv") |
| 116 | + def download(): |
| 117 | + yield annotated_data().to_csv() |
| 118 | + |
| 119 | + |
| 120 | +app = App(app_ui, server) |
0 commit comments