import streamlit as st import json import os st.title("Saved Recipes") # get all saved files directory_path = '/data/' recipes = [] for root, dirs, files in os.walk(directory_path): for file in files: if file.endswith('.json'): full_path = os.path.join(root, file) # os.remove(full_path) f = open(full_path) recipe_json = json.load(f) recipe_json['file'] = file recipes.append(recipe_json) #st.json(saved_files) cols = st.columns([4, 1]) with cols[0]: user_search = st.text_input("Search Recipes", value="") with cols[1]: user_sort = st.selectbox("Sort", ('Recent', 'Oldest', 'A-Z', 'Z-A', 'Random')) st.write("") # just some space recipes_filtered = [x for x in recipes if user_search.lower() in x['name'].lower()] if user_sort == 'Recent': recipes_filtered.sort(key=lambda x: x['timestamp'], reverse=True) elif user_sort == 'Oldest': recipes_filtered.sort(key=lambda x: x['timestamp']) elif user_sort == 'A-Z': recipes_filtered.sort(key=lambda x: x['name']) elif user_sort == 'Z-A': recipes_filtered.sort(key=lambda x: x['name'], reverse=True) elif user_sort == 'Random': recipes_filtered.sort(key=lambda x: x['file']) for recipe in recipes_filtered: with st.expander(recipe['name']): st.markdown(recipe['md']) if st.session_state.admin == True: st.write('') st.write(recipe['file']) if st.button("Delete", key=recipe['file']): if os.path.exists(f"/data/{recipe['file']}"): os.remove(f"/data/{recipe['file']}") st.rerun() # ignore # f = open('/data/test_output.json') # json_test = json.load(f) # st.json(json_test) # file_path = '/data/test_output.json' # if os.path.exists(file_path): # # Delete the file # os.remove(file_path) # st.write(f"The file {file_path} has been deleted.") # else: # st.write(f"The file {file_path} does not exist.")