Hi @ AnnMarieW! Thank you for your help! It took me some trials to adapt it but I have something now.
I added a new buttons to launch the app which will contains the graphics, the datatable, … And I can go back to modify the datatable (the menu) so it’s great.
Here’s my code now. I post it to help other people and, as a beginner, to have some advice to improve it.
import dash
from dash import Dash, dcc, html, dash_table, Output, Input
import dash_bootstrap_components as dbc
import pandas as pd
product_data = {
"product": ['Apple',"Banana","Chicken Breast", "Dunk leg","Egg"]
}
new_order_line = {"product": "","Transformation":"","Transformation Intensity":"", "quantity": 0, "Meal": 1}
df_product = pd.DataFrame(product_data)
df_new_order_line = pd.DataFrame(new_order_line, index=[0])
table = dash_table.DataTable(
id="table",
columns=[
{
"name": "Product",
"id": "product",
"editable": True,
"presentation": "dropdown",
},
{
"name": "Transformation",
"id": "transformation",
"editable": True,
"presentation": "dropdown",
},
{
"name": "Transformation Intensity",
"id": "transformation intensity",
"editable": True,
"presentation": "dropdown",
},
{
"name": "Quantity",
"id": "quantity",
"type": "numeric",
"format": {"specifier": ",.0f"},
"editable": True,
"on_change": {"failure": "default"},
"validation": {"default": 0},
},
{
"name": "Meal",
"id": "meal",
"editable": True,
"presentation": "dropdown",
},
],
data=df_new_order_line.to_dict("records"),
row_deletable=True,
dropdown={
"product": {
"options": [{"label": i, "value": i} for i in df_product["product"]]
},
"transformation": {
"options": [{"label": i, "value": i} for i in ["Row","Roasted","Boiled","Frozen", "Dried"]]
},
"transformation intensity": {
"options": [{"label": i, "value": i} for i in ["Low","Medium","High"]]
},
"meal": {
"options": [{"label": i, "value": i} for i in [1,2,3,4,5,6,7,8]]
},
},
)
app = Dash(__name__)
title = html.H4("Order Entry Table", style={"textAlign": "center", "margin": 30})
add_button = dbc.Button(children = "+", n_clicks=0, id="add-btn")
submit_button = dbc.Button(id="submit", n_clicks=0, children="Submit", className="ms-2")
app.layout = html.Div([title,
add_button,
table,
submit_button,
html.Br(),
html.Div(id='my-output', children = []),
], style={"margin": 30})
@app.callback(
Output("table", "data"),
Input("add-btn", "n_clicks"),
Input("table", "data"),
prevent_initial_call=True,
)
def add_row(add_btn, rows):
df_order = pd.DataFrame(rows)
ctx = dash.callback_context
ctrl_id = ctx.triggered[0]['prop_id'].split('.')[0]
# add a new line
if ctrl_id == "add-btn":
df_order = df_order.append(df_new_order_line, ignore_index=True)
return df_order.to_dict("records")
@app.callback(
Output(component_id='my-output', component_property='children'),
Input("submit", "n_clicks"),
Input("table", "data"),
)
def add_app(submit_btn, rows):
"""
"""
df_order = pd.DataFrame(rows)
ctx = dash.callback_context
ctrl_id = ctx.triggered[0]['prop_id'].split('.')[0]
if ctrl_id == "submit":
layout = dbc.Container([
dbc.Row([
html.Center(html.H1("Foodtracker"))
]),
html.Hr(),
dbc.Row([
html.H4("Table"),
dash_table.DataTable(
data=df_order.to_dict('records'),
columns=[
{"name": i, "id": i} for i in df_order.columns
],
)
])
])
return layout
app.run('localhost', 1000)