Here is a simple example that saves data as a list, and at startup loads the data and creates widgets. In the example the file is saved when the app is closed, and read with the app is opened.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from pathlib import Path
import json
kv = """
<MyButton>:
size_hint_y: None
height: 48
on_release: print(f'Button {self.text} pressed')
RootBoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: None
height: 48
TextInput:
id: ti
hint_text: 'Button Name'
multiline: False
on_text_validate:
if self.text: root.create_a_button(self.text)
Button:
text: 'Create a Button'
on_release:
if ti.text: root.create_a_button(ti.text)
ti.text = ''
ti.focus = True
BoxLayout:
id:button_box
orientation: 'vertical'
"""
class MyButton(Button):
pass
class RootBoxLayout(BoxLayout):
def create_a_button(self, text):
self.ids.button_box.add_widget(MyButton(text=text))
class SaveButtonsApp(App):
def build(self):
return Builder.load_string(kv)
def on_stop(self):
# get the names of the buttons
names = [w.text for w in self.root.ids.button_box.children]
with open('saved_button.txt', 'w') as f:
json.dump(names, f)
def on_start(self):
p = Path('saved_button.txt')
if p.exists():
with open(p) as f:
button_names = json.load(f)
for name in button_names:
self.root.create_a_button(name)
SaveButtonsApp().run()
From: John Zuriel
Sent: Tuesday, February 9, 2021 7:25 AM
To: Kivy users support
Subject: [kivy-users] Re: How to load data to list using JSON
how can I possibly populate them with different data on each expansion panel?
On Tuesday, February 9, 2021 at 10:21:17 PM UTC+8 John Zuriel wrote:
Someone recommend me to use JSON now, I have an expansion panel with different name with the use of dictionary but inside it there's a OneLineListItem widget with a fixed text value so each one of expansion panel has same value, now with the use of json, how can I possibly populate them with different date on each expansion panel?
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/887744e8-2af4-4da8-a093-3467a202db01n%40googlegroups.com.
What is the underlying data structure you are using to store your data?
It seems like you might want to use a set of nested dictionaries where the keys relate to the data as displayed.
From: John Zuriel
Sent: Tuesday, February 9, 2021 8:15 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
I have a grasp now on how to save and load data, but for example I want to load a JSON file with different recipes to my onelinelistitem how can I possibly load it? I want it to be like this
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/05b27764-a7e1-4a1a-bee0-33c2812e87d1n%40googlegroups.com.
What is the underlying data structure you are using to store your data?
What do you mean by that? code I'm using to store my data? I still don't have a JSON file to load, since I'm still studying it, I only understand how to use a dictionary, but on my menuscreen, you're the one who change my code to store the data using JSON
It seems like you might want to use a set of nested dictionaries where the keys relate to the data as displayed.
The simple JSON example I shared used a list. It looks like you’re your app, you would want to save your data in dictionaries, and then restore your data from dictionaries.
The keys and content would relate to how the data is displayed.
From: John Zuriel
Sent: Tuesday, February 9, 2021 8:54 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
Here's the edited version of my project.
On Tuesday, February 9, 2021 at 11:46:02 PM UTC+8 John Zuriel wrote:
What is the underlying data structure you are using to store your data?
What do you mean by that? code I'm using to store my data? I still don't have a JSON file to load, since I'm still studying it, I only understand how to use a dictionary, but on my menuscreen, you're the one who change my code to store the data using JSON
It seems like you might want to use a set of nested dictionaries where the keys relate to the data as displayed.
kinda, since i'm using a dictionary to load the name of expansion panel then load another dictionary for the menu on onelinelistitem
On Tuesday, February 9, 2021 at 11:30:46 PM UTC+8 ElliotG wrote:
What is the underlying data structure you are using to store your data?
It seems like you might want to use a set of nested dictionaries where the keys relate to the data as displayed.
From: John Zuriel
Sent: Tuesday, February 9, 2021 8:15 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
I have a grasp now on how to save and load data, but for example I want to load a JSON file with different recipes to my onelinelistitem how can I possibly load it? I want it to be like this
because every list on expansion panel has the same value because of the fix text value "honey garlic".
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6e039329-fbc3-486a-8734-74329de3bf1cn%40googlegroups.com.
So perhaps something like:
{'Categories': ['Fish', 'Chicken', 'Beef']}
{'Recipes': [{'title':'Steamed Bass', 'directions': 'steam the fish',
'shopping list':['Fish', 'parchment paper', 'tomatoes']},
{'title':'CheeseBurger', 'directions': 'cook, flip, melt, eat',
'shopping list':['ground beef', 'cheese', 'buns']},
]}
You may want to include the categories in the recipes or the recipes in the categories…
The idea is to use dictionaries to identify the types of data so you can save it and restore it.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/fc217b57-5e0d-493a-8e76-e7b511fed8bcn%40googlegroups.com.
This not about JSON, this is only about Python dictionaries and lists.
Yes, you can iterate over the dicts and lists and populate your screens.
Much like the example that used a list, you can use JSON to store to dictionaries to disk, and load them.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/2d74f79f-72b8-401f-b037-63f2314848a4n%40googlegroups.com.
This not about JSON, this is only about Python dictionaries and lists.
Ohhh okay, coz since yesterday I keep on studying how to use json on python
Much like the example that used a list, you can use JSON to store to dictionaries to disk, and load them
Yeah, I still have your code to store data on my main code.
Here is an example of how to iterate through the example data. Run it a play with it. Add the categories as you like.
After you understand this – you would learn to create the data structure by pulling the data off of your widgets or creating widgets by reading the data.
card_file = {'Recipes': [{'title':'Steamed Bass', 'directions': 'steam the fish',
'shopping list':['Fish', 'parchment paper', 'tomatoes']},
{'title':'Cheese Burger', 'directions': 'cook, flip, melt, eat',
'shopping list':['ground beef', 'cheese', 'buns']},
]}
for recipe in card_file['Recipes']:
print(f"The {recipe['title']} uses ingredients: {recipe['shopping list']}")
print(f"The directions are: {recipe['directions']}\n")
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/aa7082dd-a24f-4745-8ebd-c6a1ee408c24n%40googlegroups.com.


This error, keyerror ‘nlst’, means you do not have an id “nlst” on the menu screen.
From: John Zuriel
Sent: Wednesday, February 10, 2021 4:17 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
@ElliotG no luck on how can I put the "title" from "Recipes" on to my OneLineListItem
I tried this code but it doesn't work
Is it possible to load a text file with the dictionary you use?
like the one you did on my menuscreen, when adding and storing them after closing or running the app
This is what I want to do, but after hours experimenting your code i only manage to print them, but fail display them on widget.
On Wednesday, February 10, 2021 at 3:15:21 AM UTC+8 John Zuriel wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d90a13b6-792f-486a-b9fb-0d6b5ed3a820n%40googlegroups.com.
The id “nlst” is indicates the instance of the Content class. I do not see where the Content class is instanced.
From: John Zuriel
Sent: Wednesday, February 10, 2021 9:49 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
Is it possible to load a text file with the dictionary you use?
like the one you did on my menuscreen, when adding and storing them after closing or running the app
This is what I want to do, but after hours experimenting your code i only manage to print them, but fail display them on widget.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/cffdec43-384d-4db0-a363-9ddb846ba8b0n%40googlegroups.com.
Create and share a minimal runnable example. I can show the issue.
Here's the code, can you check it out? can't seem to find what's the problem in it
py:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/eaaf31c9-e401-4790-a91f-5a4630366761n%40googlegroups.com.
I made an edit in the on_start routine. There is a problem with the MDExpansionPanel. As written Content() only supports one item. I suspect you want an unlimited set of items there. I’m not familiar enough with kivyMD to know if that is an option or not – but you can do those experiments.
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineListItem
from kivy.uix.screenmanager import Screen
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelTwoLine
from kivy.core.window import Window
Window.size = (300, 500)
class GroceryApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.list_items = []
self.category_list = ['Chicken', 'Pork', 'Beef', 'Fish', 'Vegetables']
self.dialog = None
self.card_file = {
'Recipes': [{'title': 'Chicken Adobo', 'shopping list': ['Fish', 'parchment paper', 'tomatoes']},
{'title': 'Chicken Noodle Soup', 'shopping list': ['ground beef', 'cheese', 'buns']},
]}
def build(self):
self.theme_cls.primary_palette = "Pink"
self.theme_cls.theme_style = "Dark"
return Builder.load_file('main.kv')
def on_start(self):
for category in self
.category_list:
if category == 'Chicken':
for recipe in self.card_file['Recipes']:
print(recipe)
panel = MDExpansionPanel(icon="recipe.png", content=Content(text=recipe['title']),
panel_cls=MDExpansionPanelTwoLine(text=category, secondary_text="Tap to view recipes"))
self.root.ids.sm.get_screen('menu').ids.rlist.add_widget(panel)
else:
panel = MDExpansionPanel(icon="recipe.png", content=Content(text=recipe['title']),
panel_cls=MDExpansionPanelTwoLine(text=category,
secondary_text="Tap to view recipes"))
self.root.ids.sm.get_screen('menu').ids.rlist.add_widget(panel)
def showinfo(self, widget):
self.dialog = MDDialog(size_hint=(0.8, 0.8), text="Ingredients:", auto_dismiss=True)
self.dialog.open()
for recipe in self.card_file['Recipes']:
print(f"{recipe['shopping list']}")
class MenuScreen(Screen):
pass
class Content(MDBoxLayout, OneLineListItem):
pass
GroceryApp().run()
From: John Zuriel
Sent: Wednesday, February 10, 2021 11:47 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
Ok so here's a runnable example, and I change the dictionary to create a bit to example for "Chicken" category
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/06cdaba8-ced5-48ea-b567-b6cab5469858n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4cfc8388-4928-4ca1-b712-d837f85dd444n%40googlegroups.com.
On Feb 10, 2021, at 9:31 PM, John Zuriel <zuriel...@gmail.com> wrote:
I don't know what's happening but after putting the code and running it, it just close like normal like when you close the window, it doesn't give any traceback or any error
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/78c29a45-74ab-4acb-870d-5ce7e21e8cafn%40googlegroups.com.
Yes you can start with a text file of recipes and use that to load into the app.
Some things to consider: You could define a simple format that you use for each recipe that includes the category, title, ingredients… and write a program to format all of the recipes in the desired data structure. Alternatively you could create an editor app that lets you load and edit a JSON file that has all of this data.
I would recommend combining the Categories with the recipes in a single data structure as in the example below, I included some code to show how you can parse this:
card_file = [{'Category': 'Chicken',
'Recipes': [{'title': 'Chicken Adobo', 'shopping list': ['Fish', 'parchment paper', 'tomatoes']},
{'title': 'Chicken Noodle Soup', 'shopping list': ['ground beef', 'cheese', 'buns']},
]
},
{'Category': 'Pork',
'Recipes': [{'title': 'Pork Adobo', 'shopping list': ['Bacon', 'spices', 'tomatoes']},
{'title': 'Pork Burger', 'shopping list': ['ground pork', 'cheese', 'buns']},
]
}]
print('The Card file is a list of catagories each with a list of recipes:')
for cat in card_file:
print(f'Card_file entry: {cat}')
print('\nAccess the Categories')
for cat in card_file:
print(cat['Category'])
print('\nRecipes per Catagory:')
for cat in card_file:
print(f'\nCategory: {cat["Category"]}')
for r in cat['Recipes']:
print(r)
print('\n Full details')
print('\nRecipes per Category:')
for cat in card_file:
print(f'\nCategory: {cat["Category"]}')
for r in cat['Recipes']:
print(f'Recipe: {r["title"]}')
for i, item in enumerate(r['shopping list']):
print(f'{i + 1}.\t {item}')
print()
From: John Zuriel
Sent: Wednesday, February 10, 2021 9:46 PM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
It works now i've accidently put unnecessary code, and is it possible to create a text file with the recipe list dictionary on it then load it into the app? since I will be like putting 10-15 recipes according to the category
now after putting this code to on_start since I'm trying to put the title to OneLineListItem widget.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d5e14034-8700-4878-8752-aeb3800cd963n%40googlegroups.com.
The example:
def on_start(self): # Event handler that triggers when the application has started running
# Loads the json file created after closing the app to
p = Path('recipelist.txt')
if p.exists():
with open(p) as f:
items = json.load(f)
for item in items:
self.add_item(item)
Assumes the stored data is a list. It will not work if the data is stored differently.
The nice thing about JSON is that is well defined and there are tools that parse it right into python data structures. While the combinations of lists and dictionaries are a little complicated, it is quite a common technique. Web APIs often return data formatted this way.
As I mention before, it might be worthwhile to write a simple editor that loads in a recipe file in JSON, and lets you edit/create data.
You could of course create your own format instead, and parse it as you like.
Another option might be use a commas separated value file (CSV) see: https://docs.python.org/3/library/csv.html
Any spreadsheet can create CSV files, and you could create a simple template to build your recipes. The disadvantage of this approach is the data would be flat – rather than hierarchical, but you could work around that as well.
From: John Zuriel
Sent: Thursday, February 11, 2021 8:27 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
Yes you can start with a text file of recipes and use that to load into the app.
I tried to make a recipe text file and put the dictionary you made
I can't seem to make it run with these code of yours, this is from my menuscreen that you edited too last time. it loads the files that saves when exiting the app, I tried this too but I don't know how to make it work, i keep getting errors and errors
def on_start(self): # Event handler that triggers when the application has started running
# Loads the json file created after closing the app to
p = Path('recipelist.txt')
if p.exists():
with open(p) as f:
items = json.load(f)
for item in items:
self.add_item(item)
Some things to consider: You could define a simple format that you use for each recipe that includes the category, title, ingredients… and write a program to format all of the recipes in the desired data structure. Alternatively you could create an editor app that lets you load and edit a JSON file that has all of this data.
Yes i've thought about that too, since the category is in another dictionary
I would recommend combining the Categories with the recipes in a single data structure as in the example below, I included some code to show how you can parse this:
Ok I will, I also added a pork recipes dictionary, its kinda same the one you given above
here is it:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/886cb373-f93a-41c4-a258-bc43d80e7e94n%40googlegroups.com.


Card_file is a python data structure, not a JSON data structre.
Create a program that writes card file to disk use the JSON module. Then you will have a text file in JSON format.
In your program you can load that file with the JSON module, and it will be the same python data structure.
From: John Zuriel
Sent: Thursday, February 11, 2021 9:04 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
So I created a recipe_list.txt with the dictionary you give, now I don't know what's next since the code you give that loads a file with dictionary in it is pretty different to what i'm gonna do right now
here's the error after moving the card_file dictionary to text
since the card_file is not in the py.file anymore it gives error, so what to do to make this work again like before.
On Thursday, February 11, 2021 at 11:27:09 PM UTC+8 John Zuriel wrote:
Yes you can start with a text file of recipes and use that to load into the app.
I tried to make a recipe text file and put the dictionary you made
I can't seem to make it run with these code of yours, this is from my menuscreen that you edited too last time. it loads the files that saves when exiting the app, I tried this too but I don't know how to make it work, i keep getting errors and errors
def on_start(self): # Event handler that triggers when the application has started running
# Loads the json file created after closing the app to
p = Path('recipelist.txt')
if p.exists():
with open(p) as f:
items = json.load(f)
for item in items:
self.add_item(item)
Some things to consider: You could define a simple format that you use for each recipe that includes the category, title, ingredients… and write a program to format all of the recipes in the desired data structure. Alternatively you could create an editor app that lets you load and edit a JSON file that has all of this data.
Yes i've thought about that too, since the category is in another dictionary
I would recommend combining the Categories with the recipes in a single data structure as in the example below, I included some code to show how you can parse this:
Ok I will, I also added a pork recipes dictionary, its kinda same the one you given above
here is it:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/30af62c7-8d02-4892-aaa7-21acb2b5d0c3n%40googlegroups.com.
Assumes the stored data is a list. It will not work if the data is stored differently.
ohhhh I thought it will work, since it work on my menuscreen.
The nice thing about JSON is that is well defined and there are tools that parse it right into python data structures. While the combinations of lists and dictionaries are a little complicated, it is quite a common technique. Web APIs often return data formatted this way.
As I mention before, it might be worthwhile to write a simple editor that loads in a recipe file in JSON, and lets you edit/create data.
You could of course create your own format instead, and parse it as you like.
Thank you for another information, I'm still kinda new to dictionaries but i'm getting the grasp on it, so for now it's best to put the card_file on py file right?
Of course it is fine to keep things in python for now.
From: John Zuriel
Sent: Thursday, February 11, 2021 9:25 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
Assumes the stored data is a list. It will not work if the data is stored differently.
ohhhh I thought it will work, since it work on my menuscreen.
The nice thing about JSON is that is well defined and there are tools that parse it right into python data structures. While the combinations of lists and dictionaries are a little complicated, it is quite a common technique. Web APIs often return data formatted this way.
As I mention before, it might be worthwhile to write a simple editor that loads in a recipe file in JSON, and lets you edit/create data.
You could of course create your own format instead, and parse it as you like.
Thank you for another information, I'm still kinda new to dictionaries but i'm getting the grasp on it, so for now it's best to put the card_file on py file right?
On Friday, February 12, 2021 at 12:02:57 AM UTC+8 John Zuriel wrote:
So I created a recipe_list.txt with the dictionary you give, now I don't know what's next since the code you give that loads a file with dictionary in it is pretty different to what i'm gonna do right now
here's the error after moving the card_file dictionary to text
since the card_file is not in the py.file anymore it gives error, so what to do to make this work again like before.
On Thursday, February 11, 2021 at 11:27:09 PM UTC+8 John Zuriel wrote:
Yes you can start with a text file of recipes and use that to load into the app.
I tried to make a recipe text file and put the dictionary you made
I can't seem to make it run with these code of yours, this is from my menuscreen that you edited too last time. it loads the files that saves when exiting the app, I tried this too but I don't know how to make it work, i keep getting errors and errors
def on_start(self): # Event handler that triggers when the application has started running
# Loads the json file created after closing the app to
p = Path('recipelist.txt')
if p.exists():
with open(p) as f:
items = json.load(f)
for item in items:
self.add_item(item)
Some things to consider: You could define a simple format that you use for each recipe that includes the category, title, ingredients… and write a program to format all of the recipes in the desired data structure. Alternatively you could create an editor app that lets you load and edit a JSON file that has all of this data.
Yes i've thought about that too, since the category is in another dictionary
I would recommend combining the Categories with the recipes in a single data structure as in the example below, I included some code to show how you can parse this:
Ok I will, I also added a pork recipes dictionary, its kinda same the one you given above
here is it:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/8e5281cc-ac27-4c9a-80e0-db659b8e02ean%40googlegroups.com.
Look at how the code is parsed the in standalone example:
card_file = [{'Category': 'Chicken',
'Recipes': [{'title': 'Chicken Adobo', 'shopping list': ['Fish', 'parchment paper', 'tomatoes']},
{'title': 'Chicken Noodle Soup', 'shopping list': ['ground beef', 'cheese', 'buns']},
]
},
{'Category': 'Pork',
'Recipes': [{'title': 'Pork Adobo', 'shopping list': ['Bacon', 'spices', 'tomatoes']},
{'title': 'Pork Burger', 'shopping list': ['ground pork', 'cheese', 'buns']},
]
}]
print('\nFull details')
print('\nRecipes per Category:')
for cat in card_file:
print(f'\nCategory: {cat["Category"]}')
for r in cat['Recipes']:
print(f'Recipe: {r["title"]}')
for i, item in enumerate(r['shopping list']):
print(f'{i + 1}.\t {item}')
print()
From: John Zuriel
Sent: Thursday, February 11, 2021 10:05 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
I have manage to parse and display the category with the updated card file dictionary, now I'm having a problem on the recipes inside it.
"Unexpected type(s):(str)Possible type(s):(int)(slice)" this is the warning on line 60, i tried deleting it, the warning is gone, but nothing is showing on the list
On Friday, February 12, 2021 at 12:24:23 AM UTC+8 John Zuriel wrote:
Assumes the stored data is a list. It will not work if the data is stored differently.
ohhhh I thought it will work, since it work on my menuscreen.
The nice thing about JSON is that is well defined and there are tools that parse it right into python data structures. While the combinations of lists and dictionaries are a little complicated, it is quite a common technique. Web APIs often return data formatted this way.
As I mention before, it might be worthwhile to write a simple editor that loads in a recipe file in JSON, and lets you edit/create data.
You could of course create your own format instead, and parse it as you like.
Thank you for another information, I'm still kinda new to dictionaries but i'm getting the grasp on it, so for now it's best to put the card_file on py file right?
On Friday, February 12, 2021 at 12:02:57 AM UTC+8 John Zuriel wrote:
So I created a recipe_list.txt with the dictionary you give, now I don't know what's next since the code you give that loads a file with dictionary in it is pretty different to what i'm gonna do right now
here's the error after moving the card_file dictionary to text
since the card_file is not in the py.file anymore it gives error, so what to do to make this work again like before.
On Thursday, February 11, 2021 at 11:27:09 PM UTC+8 John Zuriel wrote:
Yes you can start with a text file of recipes and use that to load into the app.
I tried to make a recipe text file and put the dictionary you made
I can't seem to make it run with these code of yours, this is from my menuscreen that you edited too last time. it loads the files that saves when exiting the app, I tried this too but I don't know how to make it work, i keep getting errors and errors
def on_start(self): # Event handler that triggers when the application has started running
# Loads the json file created after closing the app to
p = Path('recipelist.txt')
if p.exists():
with open(p) as f:
items = json.load(f)
for item in items:
self.add_item(item)
Some things to consider: You could define a simple format that you use for each recipe that includes the category, title, ingredients… and write a program to format all of the recipes in the desired data structure. Alternatively you could create an editor app that lets you load and edit a JSON file that has all of this data.
Yes i've thought about that too, since the category is in another dictionary
I would recommend combining the Categories with the recipes in a single data structure as in the example below, I included some code to show how you can parse this:
Ok I will, I also added a pork recipes dictionary, its kinda same the one you given above
here is it:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3a67c600-1dbf-452d-a914-80630a608681n%40googlegroups.com.
for i, item in enumerate(r['shopping list']):
print(f'{i + 1}.\t {item}')
On Feb 11, 2021, at 10:55 AM, John Zuriel <zuriel...@gmail.com> wrote:
It works now, the only problem now is when clicking a recipe it shows all the ingredients instead of only the one that's been clicked
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/1e64a33d-3f9a-471b-b683-d531c62588d0n%40googlegroups.com.
<Capture.PNG>
I’m not sure I understand your question…
Assuming you want to show a list of ingredients on a Dialog, you would need to add custom content to the Dialog, Create a ScrollView with an MDList and add a list item for each ingredient. This is similar to how the list of recipes is added as widgets to the list under the categories.
From: John Zuriel
Sent: Thursday, February 11, 2021 11:44 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
Got that already thanks, how bout when clicking a recipe it shows all the ingredients instead of only the one that's been clicked
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/0346dbc8-7a0e-44dc-9971-47277b71c96dn%40googlegroups.com.
On Feb 11, 2021, at 7:04 PM, John Zuriel <zuriel...@gmail.com> wrote:
I’m not sure I understand your question…
this is what I'm saying, when you click the recipe, all of the shopping list is printing instead of only their own shopping list
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d7064cdc-eb89-408b-9167-1b80c446d796n%40googlegroups.com.
<Capture.PNG>
You want something more like this:
card_file = [{'Category': 'Chicken',
'Recipes': [{'title': 'Chicken Adobo', 'shopping list': ['Fish', 'parchment paper', 'tomatoes']},
{'title': 'Chicken Noodle Soup', 'shopping list': ['ground beef', 'cheese', 'buns']},
]
},
{'Category': 'Pork',
'Recipes': [{'title': 'Pork Adobo', 'shopping list': ['Bacon', 'spices', 'tomatoes']},
{'title': 'Pork Burger', 'shopping list': ['ground pork', 'cheese', 'buns']},
]
}]
def print_shopping_list(cat, recipe):
for c in card_file:
if c['Category'] == cat:
for r in c['Recipes']:
if r['title'] == recipe:
print(r['shopping list'])
print_shopping_list('Pork', 'Pork Burger')
At the risk of being more confusing. This data structure was put together to save and restore the state of widgets. You could create a different data structure for more efficient look ups. This is not likely to ever be an issue in the scope of this application.
From: John Zuriel
Sent: Thursday, February 11, 2021 7:42 PM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
Here it is:
On Friday, February 12, 2021 at 2:16:55 AM UTC+8 ElliotG wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4c49a021-04b4-4a5c-8abb-aae1c0920d98n%40googlegroups.com.
When you click on the item in the list the text (self.text) is the recipe title.
You can access the widget tree to get the category that the RecipeLine is under.
Alternatively create a new attribute and save the category in the RecipeLine when it is created. That way you can use it to easily look up the ingredients.
<RecipeLine>:
adaptive_height: True
OneLineListItem:
text: root.
text
on_release: app.showinfo(root.parent.parent.panel_cls.text, self.text)
MDIconButton:
icon:'plus'
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4de7a655-3eda-40e5-9064-d395ed44f89cn%40googlegroups.com.
I suspect our emails crossed…
def showinfo(self, category, recipe):
close_button = MDFlatButton(text="Done", on_release=self.close_dialog)
# instance the content…
ing = IngContent(items=ingredient_list(category, recipe)) # ingredient_list returns a list of the ingredients..
self.dialog = MDDialog(size_hint=(0.8, 0.8), text="Ingredients:", content=ing, auto_dismiss=True,
buttons=[close_button])
self.dialog.open()
Create IngContent with a Listproperty(), items. And display the items as you like.(ScrollView and MDList would work).
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3fd5f714-d0e4-4307-9364-8022eb0b3353n%40googlegroups.com.
In your kv code:
<RecipeLine>:
adaptive_height: True
OneLineListItem:
text: root.text
# on_release: app.showinfo(None)
on_release: app.showinfo(root.parent.parent.panel_cls.text, self.text)
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/b5eb271a-1ba1-4ce7-8de0-9e1867e30a62n%40googlegroups.com.
On Feb 12, 2021, at 8:16 AM, John Zuriel <zuriel...@gmail.com> wrote:
ohhhh, it gives me unresolved reference "ingredient_list" error now
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/e43d7297-8987-4663-a972-311b1c18b18an%40googlegroups.com.
Here is a version that puts the ingredients in a dialog box. This includes creating the button in on_start from the updated dictionary.
From: John Zuriel
Sent: Friday, February 12, 2021 12:27 PM
To: Kivy users support
Subject: Re: [kivy-users] Re: How to load data to list using JSON
so I misspelled there, now I must create a method like this right? but still it gives an Unresolved reference 'ingredient_list'
py:
def ingredient_list(category, recipe):
pass
def showinfo(self, category, recipe):
close_button = MDFlatButton(text="Done", on_release=self.close_dialog)
add_button = MDFlatButton(text="Add to list", on_release=self.close_dialog)
for c in self.card_file:
if c['Category'] == category:
for r in c['Recipes']:
if r['title'] == recipe:
print(r['shopping list'])
ing = IngContent(items=ingredient_list(category, recipe)) # and in here an Unexpected argument error
self.dialog = MDDialog(size_hint=(0.8, 0.8), text="Ingredients:", content=ing, auto_dismiss=True,
buttons=[close_button, add_button])
self.dialog.open()'
kv:
<IngContent>:
ScrollView:
MDList:
id: ilist
On Friday, February 12, 2021 at 11:44:51 PM UTC+8 John Zuriel wrote:
I think I'm doing it wrong, if I add self on ingredients_list it gives me "Unresolved attribute reference 'ingredient_list' for class 'GroceryApp'" and unexpected argument is showing too
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/2fb33aa8-c815-43a7-9f82-25fe479486afn%40googlegroups.com.