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)