Hi community,
I am trying to build Manufacturing resource planning system with Python and Dash…
I am using SQLAlchemy to connect to MySQL and write to it. i am having all sorts of fields and type checking’s, but only one that is giving me a headache, starting from input filed formatting, to errors is Date input field…
Here is my input fields, callback and function:
html.H1(“MRP App”),
html.H2(“Master production Scheduling”),
dcc.Input(id=“mps-product-id-input”, type=“number”, placeholder=“Product ID”),
dcc.Input(id=“start-date-input”, type=“Date”, placeholder=“yyyy-mm-dd”),
dcc.Input(id=“end-date-input”, type=“Date”, placeholder=“yyyy-mm-dd”),
dcc.Input(id=“mps-quantity-input”, type=“number”, placeholder=“Quantity”),
dcc.Input(id=“resources-input”, type=“text”, placeholder=“Resources required”),
html.Button(“Create Production Schedule”, id=“create-schedule-button”),
html.Div(id=“schedule-output”),
@app.callback(
Output(“schedule-output”,“children”),
Input(“create-schedule-button”, “n_clicks”),
[
Input(“mps-product-id-input”, “value”),
Input(“start-date-input”, “value”),
Input(“end-date-input”, “value”),
Input(“mps-quantity-input”, “value”),
Input(“resources-input”, “value”)
],
)
def create_production_schedule(n_clicks, product_id, start_date, end_date, quantity, resources):
if n_clicks and product_id and start_date and end_date and quantity and resources:
try:
print(“Start Date:”, start_date)
print(“End Date:”, end_date)
start_date = datetime.strptime(start_date, ‘%Y-%m-%d’).date()
end_date = datetime.strptime(end_date, ‘%Y-%m-%d’).date()
print(“Parsed Start Date:”, start_date)
print(“Parsed End Date:”, end_date)
product_id = int(product_id)
quantity = int(quantity)
except ValueError as e:
print(“ValueError:”, str(e))
return html.Div(“Invalid input. Please provide valid values.”)
query = "INSERT INTO m_prod_schedule (product_id, start_date, end_date, quantity, resources_required) VALUES (%s, %s, %s, %s, %s)"
values = (product_id, start_date, end_date, quantity, resources)
execute_query(query, values)
return html.Div("Production schedule created successfully!")
else:
return html.Div("Please fill in all the input fields.")
Firstly, input field for date is not formatting as yyyy-mm-dd even when I enforce pattern
with date type:
dcc.Input(id=“end-date-input”, type=“Date”, placeholder=“yyyy-mm-dd”, pattern=r"\d{4}-\d{2}-\d{2}"),
Pattern shows when type is text, but that raise an error for my function…
Not to write here indefinitely, depending of what i change in my def, i get different errors with datetime, with date, with fromisoformat… or what ever else that i try…
AttributeError: module ‘datetime’ has no attribute ‘strptime’
TypeError: ‘<’ not supported between instances of ‘datetime.date’ and ‘int’
If you can help me with input formatting and how to fix this date type to pass to my database , it will be mostly appreciated!