adrianpierce commited on
Commit
8ff835f
·
1 Parent(s): aa717e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -52
app.py CHANGED
@@ -12,59 +12,62 @@ recipes_json = json.load(f)
12
  recipes = pd.DataFrame(recipes_json)
13
  ingredients = pd.DataFrame(recipes_json).explode('ingredients')
14
 
15
- # name search
16
- name_search = st.text_input('Search recipes by name')
17
- if name_search == "":
18
- filter_name = recipes['name'].to_list()
19
- else:
20
- filter_1 = recipes['name'].str.contains(name_search)
21
- filter_name = recipes.loc[filter_1, 'name'].to_list()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # ingredient filter
24
- options = st.multiselect(
25
- 'Select ingredients to filter by:',ingredients['ingredients'].unique())
26
- filter_type = st.radio(
27
- "Specify type of ingredient filtering:",
28
- [
29
- 'Recipe contains ANY of the specified ingredients',
30
- 'Recipe contains ALL of the specified ingredients'
31
- ]
32
- )
33
- if len(options)==0:
34
- filter_ingredient = recipes['name'].to_list()
35
- else:
36
- if filter_type == 'Recipe contains ANY of the specified ingredients':
37
- filter_1 = ingredients['ingredients'].isin(options)
38
- filter_ingredient = ingredients.loc[filter_1, 'name'].to_list()
39
- elif filter_type == 'Recipe contains ALL of the specified ingredients':
40
- filter_1 = ingredients['ingredients'].isin(options)
41
- ingredients['has_ingredient'] = 0
42
- ingredients.loc[filter_1, 'has_ingredient'] = 1
43
- pivot = ingredients.groupby('name').agg(sum_has_ingredients=('has_ingredient', 'sum')).reset_index()
44
- filter_ingredient = pivot.loc[pivot['sum_has_ingredients']==len(options), 'name'].to_list()
45
-
46
- # source filter
47
- source_options = st.multiselect(
48
- 'Filter by source:',recipes['source'].unique())
49
- if len(source_options)==0:
50
- filter_source = recipes['name'].to_list()
51
- else:
52
- filter_1 = recipes['source'].isin(source_options)
53
- filter_source = recipes.loc[filter_1, 'name'].to_list()
54
-
55
- # type filter
56
- type_options = st.multiselect(
57
- 'Filter by type:',recipes['recipe_type'].unique())
58
- if len(type_options)==0:
59
- filter_type = recipes['name'].to_list()
60
- else:
61
- filter_1 = recipes['recipe_type'].isin(type_options)
62
- filter_type = recipes.loc[filter_1, 'name'].to_list()
63
-
64
- # input home bar
65
- bar_dict = [{"ingredient":"bourbon", "have": False}, {"ingredient":"gin", "have": False}, {"ingredient":"vodka", "have": False}]
66
- bar_df = pd.DataFrame(bar_dict)
67
- st.data_editor(bar_df)
68
 
 
69
  filter_all = list(set(filter_name) & set(filter_ingredient) & set(filter_source) & set(filter_type))
70
  st.dataframe(recipes[recipes['name'].isin(filter_all)], hide_index=True)
 
12
  recipes = pd.DataFrame(recipes_json)
13
  ingredients = pd.DataFrame(recipes_json).explode('ingredients')
14
 
15
+ with st.expander("Search and filter recipes"):
16
+ # name search
17
+ name_search = st.text_input('Search recipes by name')
18
+ if name_search == "":
19
+ filter_name = recipes['name'].to_list()
20
+ else:
21
+ filter_1 = recipes['name'].str.contains(name_search)
22
+ filter_name = recipes.loc[filter_1, 'name'].to_list()
23
+
24
+ # ingredient filter
25
+ options = st.multiselect(
26
+ 'Select ingredients to filter by:',ingredients['ingredients'].unique())
27
+ filter_type = st.radio(
28
+ "Specify type of ingredient filtering:",
29
+ [
30
+ 'Recipe contains ANY of the specified ingredients',
31
+ 'Recipe contains ALL of the specified ingredients'
32
+ ]
33
+ )
34
+ if len(options)==0:
35
+ filter_ingredient = recipes['name'].to_list()
36
+ else:
37
+ if filter_type == 'Recipe contains ANY of the specified ingredients':
38
+ filter_1 = ingredients['ingredients'].isin(options)
39
+ filter_ingredient = ingredients.loc[filter_1, 'name'].to_list()
40
+ elif filter_type == 'Recipe contains ALL of the specified ingredients':
41
+ filter_1 = ingredients['ingredients'].isin(options)
42
+ ingredients['has_ingredient'] = 0
43
+ ingredients.loc[filter_1, 'has_ingredient'] = 1
44
+ pivot = ingredients.groupby('name').agg(sum_has_ingredients=('has_ingredient', 'sum')).reset_index()
45
+ filter_ingredient = pivot.loc[pivot['sum_has_ingredients']==len(options), 'name'].to_list()
46
+
47
+ # source filter
48
+ source_options = st.multiselect(
49
+ 'Filter by source:',recipes['source'].unique())
50
+ if len(source_options)==0:
51
+ filter_source = recipes['name'].to_list()
52
+ else:
53
+ filter_1 = recipes['source'].isin(source_options)
54
+ filter_source = recipes.loc[filter_1, 'name'].to_list()
55
+
56
+ # type filter
57
+ type_options = st.multiselect(
58
+ 'Filter by type:',recipes['recipe_type'].unique())
59
+ if len(type_options)==0:
60
+ filter_type = recipes['name'].to_list()
61
+ else:
62
+ filter_1 = recipes['recipe_type'].isin(type_options)
63
+ filter_type = recipes.loc[filter_1, 'name'].to_list()
64
 
65
+ with st.expander("Home bar"):
66
+ # input home bar
67
+ bar_dict = [{"ingredient":"bourbon", "have": False}, {"ingredient":"gin", "have": False}, {"ingredient":"vodka", "have": False}]
68
+ bar_df = pd.DataFrame(bar_dict)
69
+ st.data_editor(bar_df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ st.header("Recipes:")
72
  filter_all = list(set(filter_name) & set(filter_ingredient) & set(filter_source) & set(filter_type))
73
  st.dataframe(recipes[recipes['name'].isin(filter_all)], hide_index=True)