adrianpierce commited on
Commit
e48f638
·
1 Parent(s): e2531ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -4
app.py CHANGED
@@ -10,6 +10,10 @@ recipes_json = json.load(f)
10
  recipes = pd.DataFrame(recipes_json)
11
  ingredients = pd.DataFrame(recipes_json).explode('ingredients')
12
 
 
 
 
 
13
  # ingredient filter
14
  options = st.multiselect(
15
  'Select ingredients to filter by:',ingredients['ingredients'].unique())
@@ -22,16 +26,17 @@ filter_type = st.radio(
22
  )
23
  if len(options)==0:
24
  filter_1 = [True]*len(ingredients)
25
- filtered_names = ingredients.loc[filter_1, 'name'].to_list()
26
  else:
27
  if filter_type == 'Recipe contains ANY of the specified ingredients':
28
  filter_1 = ingredients['ingredients'].isin(options)
29
- filtered_names = ingredients.loc[filter_1, 'name'].to_list()
30
  elif filter_type == 'Recipe contains ALL of the specified ingredients':
31
  filter_1 = ingredients['ingredients'].isin(options)
32
  ingredients['has_ingredient'] = 0
33
  ingredients.loc[filter_1, 'has_ingredient'] = 1
34
  pivot = ingredients.groupby('name').agg(sum_has_ingredients=('has_ingredient', 'sum')).reset_index()
35
- filtered_names = pivot.loc[pivot['sum_has_ingredients']==len(options), 'name'].to_list()
36
 
37
- st.dataframe(recipes[recipes['name'].isin(filtered_names)], hide_index=True)
 
 
10
  recipes = pd.DataFrame(recipes_json)
11
  ingredients = pd.DataFrame(recipes_json).explode('ingredients')
12
 
13
+ # name search
14
+ name_search = st.text_input('Search recipes by name')
15
+ filter_name = recipes['name'].str.contains(name_search)
16
+
17
  # ingredient filter
18
  options = st.multiselect(
19
  'Select ingredients to filter by:',ingredients['ingredients'].unique())
 
26
  )
27
  if len(options)==0:
28
  filter_1 = [True]*len(ingredients)
29
+ filter_ingredient = ingredients.loc[filter_1, 'name'].to_list()
30
  else:
31
  if filter_type == 'Recipe contains ANY of the specified ingredients':
32
  filter_1 = ingredients['ingredients'].isin(options)
33
+ filter_ingredient = ingredients.loc[filter_1, 'name'].to_list()
34
  elif filter_type == 'Recipe contains ALL of the specified ingredients':
35
  filter_1 = ingredients['ingredients'].isin(options)
36
  ingredients['has_ingredient'] = 0
37
  ingredients.loc[filter_1, 'has_ingredient'] = 1
38
  pivot = ingredients.groupby('name').agg(sum_has_ingredients=('has_ingredient', 'sum')).reset_index()
39
+ filter_ingredient = pivot.loc[pivot['sum_has_ingredients']==len(options), 'name'].to_list()
40
 
41
+ filter_all = filter_name & filter_ingredient
42
+ st.dataframe(recipes[recipes['name'].isin(filter_all)], hide_index=True)