Spaces:
Sleeping
Sleeping
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) |