Spaces:
Running
Running
File size: 1,971 Bytes
5266631 ca13361 80d7df8 4c20be7 f3f69dd 80d7df8 f3f69dd cb46f62 80d7df8 56dbafa e95c920 cb46f62 80d7df8 fea45d9 80d7df8 54af6c8 e95c920 9bb70c7 cb46f62 9bb70c7 739e47b 9bb70c7 739e47b e95c920 cb46f62 56dbafa 627c2c0 6a798f1 20e5e7d 6a798f1 20e5e7d 56dbafa f3f69dd fea45d9 80d7df8 fea45d9 |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
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)
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'])
elif user_sort == 'Oldest':
recipes_filtered.sort(key=lambda x: x['timestamp'], reverse=True)
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.") |