MAJOR ANNOUNCEMENT!
Hold the presses! No need to work on this, I figured it out. I forgot to capitalize the keys in the data source. But that’s it. That was the only thing wrong. I will publish a solution soon!
I would not be the man I am today without you.
Thanks so much for everything.
I have been working on this all day. I seem very close, but I have not quite got it.
It accepts the function below, and each import produces the correct results. The callback in correctly constructed, I am almost certain. Based on error message, its trying to pass it to the table. The code produces the data, and the empty table appears without any error messages when I submit a recipe.
I know the “data” in this function, pasted below, is correctly structured, though it is not valid JSON. And that may be where the problem lies. Still, if I paste it into the script and call it “data,” it perfectly populates the table. So I am struggling to understand how it could be the problem and requires further parsing.
If I run the function as is, however, it processes the recipe and prints out the data.
But it will not populate the table.
I am at a loss and hoping you may have some final insights. It feels like I am missing something obvious, and you might spot it right away because it comes so close to working.
If this is too much, I apologize and I understand completely.
Thank you.
There is only one function and callback in the entire script now - the one shown below. Why? Because I was trying to use the output of one function and the Input of another in the next callback. And that just was not possible. Everything else is imported into this function to simplify matters and it seems to work fine until the data needs to populate the table.
1) This is the function in its current state. I could not import the list of dictionaries (recipe_ents_list) with json.load. I had to use json.loads and then then try to reconstruct it as a new list of dictionaries. If I use print(type), it tells me they are dictionaries inside a list. But it won’t accept the data unless I paste it directly into the script and call it data.
@app.callback(
Output('nutrients-review-datatable', 'data'),
Input('recipe-textarea-submit-button', 'n_clicks'),
State('recipe-textarea', 'value')
)
def update_output(n_clicks, value):
if n_clicks > 0:
print("Recipe submitted")
with open("temp/recipe_contents.txt", "w", encoding='utf-8') as recipe_contents:
recipe = value.split('\n')
for line in recipe:
recipe_contents.write(line)
recipe_contents.write('\n')
#The following are my imported Python scripts and one produces the expected results.
convert_unicode_fractions()
convert_regular_fractions()
prepare_recipe_text()
parse_recipe_ner()
prepare_ner_data_for_table()
# Here I open recipe_ents_list and try to return it as data, this seems to be where the problem lies. I experimented with
# your solution for quite a while. It either does not require further parsing or I am unable to apply it.
with open("./json/recipe_ents_list.json", "r") as final_recipe_file:
results = json.loads(final_recipe_file.read())
data_list = []
for each_dict in results:
data_list.append(each_dict)
data = data_list
print(data)
return data
2) Here are the last few lines of the final imported function, prepare_ner_data_for_table(), showing the making of recipe_ents_list - the list of dictionaries below that should become the data source. It works.
recipe_ents_list = list(unique_everseen(flask_dict_list))
for line in recipe_ents_list:
print("Recipe Ents List :", line)
with open("./json/recipe_ents_list.json", "w") as final_recipe_file:
json.dump(recipe_ents_list, final_recipe_file, indent=4)
3) This is the list of dictionaries called recipe_ents_list, as it currently stands. It is the data source that should populate the table, and this is the last printed output.
[{‘index’: 0, ‘amount’: ‘.5’, ‘mod’: ‘None’, ‘units’: ‘teaspoon’, ‘ing’: ‘dried oregano’},
{‘index’: 1, ‘amount’: ‘0.25’, ‘mod’: ‘None’, ‘units’: ‘tsp’, ‘ing’: ‘red chilli flakes’},
{‘index’: 2, ‘amount’: ‘0.25’, ‘mod’: ‘None’, ‘units’: ‘tsp’, ‘ing’: ‘ground cloves’},
{‘index’: 3, ‘amount’: ‘1’, ‘mod’: ‘None’, ‘units’: ‘tbsp’, ‘ing’: ‘sunflower oil’},
{‘index’: 4, ‘amount’: ‘1’, ‘mod’: ‘None’, ‘units’: ‘tsp’, ‘ing’: ‘mustard seeds’},
{‘index’: 5, ‘amount’: ‘1’, ‘mod’: ‘divided’, ‘units’: ‘tsp’, ‘ing’: ‘salt’},
{‘index’: 6, ‘amount’: ‘1.33’, ‘mod’: ‘None’, ‘units’: ‘tsp’, ‘ing’: ‘cumin’},
{‘index’: 7, ‘amount’: ‘1.5’, ‘mod’: ‘None’, ‘units’: ‘teaspoon’, ‘ing’: ‘dried thyme’},
{‘index’: 8, ‘amount’: ‘10’, ‘mod’: ‘None’, ‘units’: ‘teaspoon’, ‘ing’: ‘cardamom pods’},
{‘index’: 9, ‘amount’: ‘3’, ‘mod’: ‘None’, ‘units’: ‘cm’, ‘ing’: ‘ginger’},
{‘index’: 10, ‘amount’: ‘3’, ‘mod’: ‘medium’, ‘units’: ‘cm’, ‘ing’: ‘shallots’},
{‘index’: 11, ‘amount’: ‘300’, ‘mod’: ‘None’, ‘units’: ‘grams’, ‘ing’: ‘red lentils’},
{‘index’: 12, ‘amount’: ‘4’, ‘mod’: ‘minced’, ‘units’: ‘grams’, ‘ing’: ‘cloves of garlic’},
{‘index’: 13, ‘amount’: ‘400’, ‘mod’: ‘None’, ‘units’: ‘grams’, ‘ing’: ‘diced tomatoes’},
{‘index’: 14, ‘amount’: ‘80’, ‘mod’: ‘None’, ‘units’: ‘grams’, ‘ing’: ‘baby spinach’},
{‘index’: 15, ‘amount’: ‘1’, ‘mod’: ‘None’, ‘units’: ‘handful’, ‘ing’: ‘cilantro’},
{‘index’: 16, ‘amount’: ‘1’, ‘mod’: ‘Half’, ‘units’: ‘handful’, ‘ing’: ‘lemon’}]
Thank you again, I hope this is not a huge imposition.
I wish you a safe and happy holiday.
Robert