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') with st.expander("Search and filter recipes"): # 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( "Specify type of ingredient 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() # source filter source_options = st.multiselect( 'Filter by source:',recipes['source'].unique()) if len(source_options)==0: filter_source = recipes['name'].to_list() else: filter_1 = recipes['source'].isin(source_options) filter_source = recipes.loc[filter_1, 'name'].to_list() # type filter type_options = st.multiselect( 'Filter by type:',recipes['recipe_type'].unique()) if len(type_options)==0: filter_type = recipes['name'].to_list() else: filter_1 = recipes['recipe_type'].isin(type_options) filter_type = recipes.loc[filter_1, 'name'].to_list() with st.expander("Home bar"): # input home bar bar_dict = [{"ingredient":"rye", "have": False}, {"ingredient":"simple syrup", "have": False}, {"ingredient":"angostura bitters", "have": False}] bar_df = pd.DataFrame(bar_dict) st.data_editor(bar_df, hide_index=True) ingredients_joined = ingredients.join(bar_df.set_index('ingredients'), on='ingredients') ingredients_joined.replace({'have': {True: 1, False: 0}}, inplace=True) pivot = ingredients_joined.groupby('name').agg(sum_needs_ingredients=('have', 'count'), sum_has_ingredients=('have', 'sum')).reset_index() filter_1 = (pivot['sum_has_ingredients'] > 0) & (pivot['sum_needs_ingredients'] > 0) filter_2 = pivot['sum_needs_ingredients'] == pivot['sum_has_ingredients'] filter_all = filter_1 & filter_2 filter_bar = pivot.loc[filter_all, 'name'].to_list() st.header("Recipes") filter_all = list(set(filter_name) & set(filter_ingredient) & set(filter_source) & set(filter_type) & set(filter_bar)) st.dataframe(recipes[recipes['name'].isin(filter_all)], hide_index=True)