I pasted all the code below. I hope its not too much.
It is just all relevant to the question below. Its highlighted to save you time.
The first callback works fine. It takes the number of clicks from the submit button as the Input. For the Ouput, it returns the value of the initial processing and populates the corresponding editable datatable. There’s no issues here - the resultant datatable provides the user with editable data so that the user can make final edits and hit submit (again).
There is also no problem with the submit button. It takes the data edited by the user for final processing, and triggers the corresponding function which performs its job successfully. The problem seems to lie in the second Ouput statement, which is supposed to present the output of the final analysis. Specifically, it is supposed to render the results of the final analysis and present it in the div “html.Div(id=‘final-nutritional-profile-output’, ‘children’)”
I think I am confused by the word children, and how and when to use it. How can I direct the results of the final results of the last function to that area? By any chance, does DASH provide a separate reporting div where final report data can be displayed in an ordered way? What’s the best way to display text in the browser?
This is the final technical hurdle to completing the project. I would greatly appreciate any sincere feedback.
Here is the code. I highlighted the relevant areas.
Thank you very much for reviewing my question.
Robert
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div([
*# Text area where the user pastes the raw materials or a cooking recipe in this case.*
dcc.Textarea(
id='recipe-textarea',
value='',
style={'width': '100%', 'height': 200},
placeholder="Please note: All information in brackets or parentheses will be deleted."
),
html.Button('Submit Recipe', id='recipe-textarea-submit-button', n_clicks=0),
html.Div(id="msg"),
#After initial processing, the nutritional elements (ingredients, amounts and so forth...) appear in this datatable for user review and editing.
dash_table.DataTable(
id='nutrients-review-datatable',
columns=[{'id': i, 'name':i} for i in ['Index', 'ID', 'Grams', 'Total Grams', 'Amounts', 'Modifiers', 'Units', 'Ings', 'Multiplier']],
style_cell={'textAlign': 'left', 'padding': '10px', 'fontSize':14, 'font-family':'sans-serif'},
style_header={'backgroundColor': '6eafde', 'fontWeight': 'bold', 'fontSize':14, 'border': '1px solid black','align': 'left'},
style_data_conditional=[{'if':{'row_index': 'odd'}, 'backgroundColor': 'rgb(248, 248, 248)'}],
data=None,
editable=True,
row_deletable=True,
hidden_columns=['Index','ID','Grams', 'Total Grams', 'Multiplier' ],
sort_action='native',
page_action='native',
page_size=10,
style_table={'height':'400px', 'overflowY':'auto'}
),''
html.Button('Submit Edits', id='nutrients-review-datatable-submit-button', n_clicks=0),
html.Div(id='final-nutritional-profile-output', 'children')
])
# This callback and function accepts and processes the recipe,
# producing an editable table of amounts, units of measurement,
# ingredients and modifier essential to a nutritional profile.
@app.callback(
Output('nutrients-review-datatable', 'value'),
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')
convert_unicode_fractions()
convert_regular_fractions()
prepare_recipe_text()
parse_recipe_ner()
validate_recipe_ents()
prepare_data_for_table()
with open("./json/recipe_ents_list.json", "r") as final_recipe_file:
data = json.loads(final_recipe_file.read())
return data
# This function and callback accept the user's edits to the nutritional
# elements in the table and produce a nutritional profile. In the future, check boxes
# allow the user to tailor the report, tracking 50+ nutrients.
# The Output line below is the problem. It is supposed to return the final results of the nutritional analysis and place it in # the div "(id='final-nutritional-profile-output'. " But this does not produce the correct results. I receive the error message: # "SyntaxError: positional argument follows keyword argument" Can someone point me in the right direct?
@app.callback(
**Output('final-nutritional-profile-output', 'children'),**
Input('nutrients-review-datatable-submit-button', 'n_clicks'),
State('nutrients-review-datatable', 'data'),
prevent_initial_call=True
)
def create_nutritional_profile(n_clicks, data):
if n_clicks > 0:
finalize_ing_dicts(data)
create_match_idslist()
create_nutrient_dicts()
merge_nutrient_to_ing_dicts()
return (final_conversions())
if __name__ == '__main__':
app.run_server(debug=True)
Bottom line, I want to print the final data - finial report results - to the screen.
How can I do that?