From 47b67816e272c9a719b45c0a319321da448b260b Mon Sep 17 00:00:00 2001 From: Andre Wohnsland <50302161+AndreWohnsland@users.noreply.github.com> Date: Wed, 16 Nov 2022 18:01:03 +0100 Subject: [PATCH] Added option to filter by serving count --- frontend/data.py | 11 +++++++---- frontend/plots.py | 3 ++- frontend/views.py | 13 ++++++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/frontend/data.py b/frontend/data.py index 5fcf5ca..af27b3e 100644 --- a/frontend/data.py +++ b/frontend/data.py @@ -1,6 +1,5 @@ import os import datetime -from dataclasses import dataclass import streamlit as st from deta import Deta import pandas as pd @@ -37,6 +36,7 @@ class ReceivedData(): def __myround(x, base=5): + """Rounds to the nearest number to given base""" return base * round(x / base) @@ -152,7 +152,7 @@ def time_aggregation(df: pd.DataFrame, hour_grouping: bool, machine_grouping: bo @st.cache(ttl=60) -def serving_aggreation(df: pd.DataFrame, machine_split: bool): +def serving_aggreation(df: pd.DataFrame, machine_split: bool, min_count: int): """Aggregates by serving sizes""" # rounds to the closest 25 serving_df = df.copy(deep=True) @@ -164,7 +164,10 @@ def serving_aggreation(df: pd.DataFrame, machine_split: bool): serving_df.groupby(grouping)[DataSchema.language] # type: ignore .agg(["count"]) .reset_index() - .sort_values(['count'], ascending=False) + .sort_values([DataSchema.volume], ascending=True) .rename(columns={"count": DataSchema.cocktail_count, }) ) - return serving_df + # for multiple grouping needs to calculate the sum per group and only inlcude the ones having more than min + serving_sive_count = serving_df.groupby(DataSchema.volume).sum() + volumes_to_keep = serving_sive_count[serving_sive_count[DataSchema.cocktail_count] >= min_count].index.to_list() + return serving_df[serving_df[DataSchema.volume].isin(volumes_to_keep)] diff --git a/frontend/plots.py b/frontend/plots.py index fca330a..9295548 100644 --- a/frontend/plots.py +++ b/frontend/plots.py @@ -147,6 +147,7 @@ def generate_serving_size_bars(df: pd.DataFrame, machine_split: bool): fig.update_xaxes( tickmode='array', tickvals=df[DataSchema.volume], - ticktext=[f"{x} ml" for x in df[DataSchema.volume].to_list()] + ticktext=[f"{x} ml" for x in df[DataSchema.volume].to_list()], + type="category", ) st.plotly_chart(fig, use_container_width=True, config={"displayModeBar": False}) diff --git a/frontend/views.py b/frontend/views.py index 594e425..756b56a 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -84,7 +84,7 @@ def display_introduction(df_stats: DataFrameStats): __what_is_this() st.markdown( f""" - # ๐Ÿ“ˆ Current CocktailBerry Stats + ## ๐Ÿ“ˆ Current CocktailBerry Stats - ๐Ÿธ **{df_stats.cocktails}** cocktails made - ๐Ÿงพ **{df_stats.recipes}** different recipes tasted - ๐ŸŽŠ **{df_stats.volume:.1f}** litre cocktails produced @@ -157,8 +157,15 @@ def __show_volume_stats(filterd_df: pd.DataFrame): def __show_serving_size(filterd_df: pd.DataFrame): """Show stats over the prepared volume choices""" st.header("๐Ÿฅƒ Serving Sizes") - machine_split = st.checkbox("Split by Machine", False, key="serving_machine") - serving_df = data.serving_aggreation(filterd_df, machine_split) + col1, col2 = st.columns(2) + machine_split = col1.checkbox("Split by Machine", False, key="serving_machine") + # only make it available if no machine split is activated + max_value_posssible = 10 + min_servings: int = col2.slider( + "Filter Minimal Serving Count", 0, + max_value_posssible + ) # type: ignore + serving_df = data.serving_aggreation(filterd_df, machine_split, min_servings) plots.generate_serving_size_bars(serving_df, machine_split)