Spaces:
Sleeping
Sleeping
File size: 2,642 Bytes
ce889fc 4ef3b48 6745377 ce889fc 5b9d4d6 b930377 5b9d4d6 df59e84 cd742a5 726778d cd742a5 5b9d4d6 cd742a5 8c5f156 22cfaaa b930377 5b9d4d6 f9cdc70 5b9d4d6 e87fc3d 8c5f156 e87fc3d 5b9d4d6 e87fc3d 8c5f156 f9cdc70 9e3f6c4 5b9d4d6 9e3f6c4 |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import requests
import json
import random
import pandas as pd
import streamlit as st
# Define session state variables
if 'df' not in st.session_state:
st.session_state.df = pd.DataFrame()
st.session_state.df_edited = st.session_state.df.copy()
st.session_state.df2 = pd.DataFrame()
# Define functions
def get_recipes():
url = "https://api.notion.com/v1/databases/180f4b492f4d421c88028c54cfe077a5/query"
payload = json.dumps({
"filter": {
"property": "Weekly Rotation",
"checkbox": {
"equals": True
}
}
})
headers = {
'Authorization': st.secrets["notion_authorization"],
'Content-Type': 'application/json',
'Notion-Version': '2022-02-22'
}
response = requests.request("POST", url, headers=headers, data=payload)
recipes = response.json()
recipe_list = []
for recipe in random.sample(recipes['results'], n_recipes):
recipe_dict = {}
recipe_dict['Name'] = recipe["properties"]["Name"]["title"][0]["plain_text"].strip()
recipe_dict['Url'] = recipe["url"]
recipe_list.append(recipe_dict)
st.session_state.df = pd.DataFrame.from_dict(recipe_list)
st.session_state.df.insert(1,'Save',[False]*len(st.session_state.df))
def save_recipes():
filter_1 = st.session_state.df_edited['Save'] == True
filtered = st.session_state.df_edited[filter_1]
st.session_state.df2 = pd.concat([st.session_state.df2, filtered])
def clear_saved():
st.session_state.df2 = pd.DataFrame()
# Define app
st.title('Get Recipes')
n_recipes = st.slider('Select number of recipes to get', 0, 20, 10)
st.button(label='Get', on_click=get_recipes)
if len(st.session_state.df) > 0:
st.session_state.df_edited = st.data_editor(
st.session_state.df,
column_config={
"Name": st.column_config.Column(width="medium"),
"Save": st.column_config.Column(width="small"),
"Url": st.column_config.LinkColumn(width="large")
},
disabled=["Name", "Url"],
hide_index=True,
)
st.button(label='Save Recipes', on_click=save_recipes)
st.title('Saved Recipes')
if len(st.session_state.df2) == 0:
st.write('Nothing saved, yet!')
else:
st.dataframe(
st.session_state.df2,
column_config={
"Name": st.column_config.Column(width="medium"),
"Save": st.column_config.Column(width="small"),
"Url": st.column_config.LinkColumn(width="large")
},
hide_index=True)
st.button(label='Clear Saved', on_click=clear_saved) |