Spaces:
Sleeping
Sleeping
File size: 1,699 Bytes
a9d9e97 705057b e988e3b c4dea99 e988e3b 705057b e2531ec eb1ab19 e988e3b e2531ec c3096f2 e48f638 e39eb92 2cff234 e48f638 94996fc 7cee12a 83499a1 0fa4602 83499a1 c83f2b2 83499a1 e48f638 c54e75a e36cb14 83499a1 e48f638 83499a1 9a43d39 e48f638 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import pandas as pd
import streamlit as st
import json
st.set_page_config(layout="wide")
# load data
f = open('data.json')
recipes_json = json.load(f)
# prepare data
recipes = pd.DataFrame(recipes_json)
ingredients = pd.DataFrame(recipes_json).explode('ingredients')
# name search
name_search = st.text_input('Search recipes by name')
if name_search == "":
filter_name = recipes['name'].to_list()
else:
filter_1 = recipes['name'].str.contains(name_search)
filter_name = recipes.loc[filter_1, 'name'].to_list()
# ingredient filter
options = st.multiselect(
'Select ingredients to filter by:',ingredients['ingredients'].unique())
filter_type = st.radio(
"Select type of filtering:",
[
'Recipe contains ANY of the specified ingredients',
'Recipe contains ALL of the specified ingredients'
]
)
if len(options)==0:
filter_ingredient = recipes['name'].to_list()
else:
if filter_type == 'Recipe contains ANY of the specified ingredients':
filter_1 = ingredients['ingredients'].isin(options)
filter_ingredient = ingredients.loc[filter_1, 'name'].to_list()
elif filter_type == 'Recipe contains ALL of the specified ingredients':
filter_1 = ingredients['ingredients'].isin(options)
ingredients['has_ingredient'] = 0
ingredients.loc[filter_1, 'has_ingredient'] = 1
pivot = ingredients.groupby('name').agg(sum_has_ingredients=('has_ingredient', 'sum')).reset_index()
filter_ingredient = pivot.loc[pivot['sum_has_ingredients']==len(options), 'name'].to_list()
filter_all = list(set(filter_name) & set(filter_ingredient))
st.dataframe(recipes[recipes['name'].isin(filter_all)], hide_index=True) |