Put a Date in a Callback

I have the Date defined in answers.py and it works perfectly.

from dash import Dash, html
import dash_bootstrap_components as dbc
from . import ids
import datetime

def date(app: Dash) -> html.Div:
    return html.Div(
        [
            html.H4(datetime.datetime.now().strftime('%d-%m-%Y')),
        ],
        className="date",
        id=ids.DATE
    )

Then I import the code into questions.py. All Inputs besides the ids.Date works perfectly. I would assume the issue is the that in Input, template and output_string i dont know how to name the input: DATE

def render(app: Dash) -> html.Div:
    @app.callback(
        Output(ids.ANSWER_OUTPUT, "children"),
        [
            Input(ids.DATE, "date"),
            Input(ids.QUESTION_1, "value"),
            Input(ids.QUESTION_2, "value"),
            Input(ids.QUESTION_3, "value"),
            Input(ids.QUESTION_4, "value"),
        ],
    )
    def on_form_change(date, answer_1_value, answer_2_value, answer_3_value, answer_4_value):
        template = 'Date: {}, Question 1: {}, Question 2: {}, Question 3: {}, Question 4: {}'
        output_string = template.format(date, answer_1_value, answer_2_value, answer_3_value, answer_4_value)
        return output_string

in the code I have marked the problem with date in the code. How do i need to name it so it works

Hi @SebastianHeeg,

The prop name you are looking for is children . To make it more clear you could write your function like this:



def date(app: Dash) -> html.Div:
    return html.Div(
        children=[
            html.H4(children=datetime.datetime.now().strftime("%d-%m-%Y"), id=ids.DATE),
        ],
        className="date",
        id=ids.DATE_CONTAINER,
    )

Since children is the first prop in most Dash components, it’s a convention to just use it as a positional argument rather than the keyword argument - just to make things less verbose.

Also, you need to put the id in the container that has the date, rather than any outer container. If you aren’t referring to that outer container in any other callback you could omit the id: id=ids.DATE_CONTAINER

1 Like