Skip to content

Commit

Permalink
Added option to filter by serving count
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreWohnsland committed Nov 16, 2022
1 parent 3be9e2c commit 47b6781
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
11 changes: 7 additions & 4 deletions frontend/data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import datetime
from dataclasses import dataclass
import streamlit as st
from deta import Deta
import pandas as pd
Expand Down Expand Up @@ -37,6 +36,7 @@ class ReceivedData():


def __myround(x, base=5):
"""Rounds to the nearest number to given base"""
return base * round(x / base)


Expand Down Expand Up @@ -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)
Expand All @@ -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)]
3 changes: 2 additions & 1 deletion frontend/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
13 changes: 10 additions & 3 deletions frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 47b6781

Please sign in to comment.