Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import pandas as pd | |
| import json | |
| from urllib.request import urlopen | |
| from bs4 import BeautifulSoup | |
| # import the dataset | |
| df = pd.read_json('recipes.json') | |
| df['liststring'] = [','.join(map(str, l)) for l in df['Ingredients']] | |
| st.write("#### Was koche ich heute?") | |
| ## Sidebar | |
| def user_input_features(): | |
| ingre1 = st.sidebar.text_input("Zutat 1") | |
| ingre2 = st.sidebar.text_input("Zutat 2", key = "zutat2") | |
| ingre3 = st.sidebar.text_input("Zutat 3", key = "zutat3") | |
| list_ingre = [ingre1, ingre2, ingre3] | |
| return list_ingre | |
| st.sidebar.write("Was hast du schon im Kühlschrank?") | |
| ingre = user_input_features() | |
| results_ingredients = st.sidebar.button("Suchen") | |
| st.sidebar.write("oder") | |
| results = st.sidebar.button("Überrasch mich!") | |
| # Sort the dataset | |
| #create a dataframe "new" with all the ingredients that are in the list ingre | |
| new = df[df['liststring'].apply(lambda x: ingre[0] in x)] | |
| if ingre[1]: | |
| new = new[new['liststring'].apply(lambda x: ingre[1] in x)] | |
| if ingre[2]: | |
| new = new[new['liststring'].apply(lambda x: ingre[2] in x)] | |
| #new = df[df['liststring'].apply(lambda x: ingre in x)] | |
| col1, col2 = st.columns(2) | |
| if not new.empty: | |
| if results_ingredients: | |
| bla = new.sample(1) | |
| #Opening header images | |
| url = str(bla["Url"].iloc[0]) | |
| htmldata = urlopen(url) | |
| soup = BeautifulSoup(htmldata, 'html.parser') | |
| images = soup.find_all('img') | |
| image = images[2]['src'] | |
| #Opening the Rest | |
| zutaten = bla["Ingredients"].iloc[0] | |
| instructions = str(bla["Instructions"].iloc[0]) | |
| name = str(bla["Name"].iloc[0]) | |
| with col1: | |
| st.write("# " + name) | |
| for i in zutaten: | |
| st.write("* "+i) | |
| with col2: | |
| st.image(image) | |
| st.text_area("Anleitung", instructions, height=300) | |
| else: | |
| st.write("### Sorry, nichts gefunden...") | |
| # Next | |
| if results: | |
| new_sample = df.sample(1) | |
| #Opening header images | |
| url = str(new_sample["Url"].iloc[0]) | |
| htmldata = urlopen(url) | |
| soup = BeautifulSoup(htmldata, 'html.parser') | |
| images = soup.find_all('img') | |
| image = images[2]['src'] | |
| # Rest | |
| zutaten = new_sample["Ingredients"].iloc[0] | |
| instructions = str(new_sample["Instructions"].iloc[0]) | |
| name = str(new_sample["Name"].iloc[0]) | |
| with col1: | |
| st.write("# " + name) | |
| for i in zutaten: | |
| st.write("* "+i) | |
| with col2: | |
| st.image(image) | |
| st.text_area("Anleitung", instructions, height=300) | |