Repository: Avaiga/taipy
File: doc/gui/examples/controls/chat-streaming.py (Python)

Code before:
        msg = state.messages[idx]
        state.messages[idx] = (msg[0], message, msg[2])
    else:
        idx = len(state.messages)
        state.messages.append((f"{len(state.messages)}", message, users[0][0]))
    state.refresh("messages")
    return idx


# append to an existing message
def append_to_message(state: State, message: str, idx: int):
    if idx < len(state.messages):
        msg = state.messages[idx]
        state.messages[idx] = (msg[0], f"{msg[1]}{message}", msg[2])
        state.refresh("messages")
    return idx


# request the wikipedia API, invoked by send_message
def request_wikipedia(gui: Gui, state_id: str, event_type: str, month: str, day: str):
    # inform the user a request is posted
    idx = invoke_callback(
        gui,
        state_id,
        set_message,
        ["requesting Wikipedia ..."],
    )
    request = wiki_url.format(type=event_type, month=month, day=day)
    req = requests.get(request, headers={"accept": "application/json; charset=utf-8;", "User-Agent": user_agent})
    # handle the response
    if req.status_code == 200:
        # display response
        invoke_callback(
            gui,
            state_id,
            update_message,
            [req.json(), event_type, f"{day}/{month}", idx],
        )
    else:
        # display error
        invoke_callback(
            gui,
            state_id,
            set_message,
            [f"requesting Wikipedia failed: {req.status_code}", idx],
        )


# invoked by the on_action on the chat control when the user press the send button
def send_message(state: State, id: str, payload: dict):
    args = payload.get("args", [])

Review comment (suggestion):
```suggestion
        ["Fetching information from Wikipedia ..."],
```

Code after:
        msg = state.messages[idx]
        state.messages[idx] = (msg[0], message, msg[2])
    else:
        idx = len(state.messages)
        state.messages.append((f"{len(state.messages)}", message, users[0][0]))
    state.refresh("messages")
    return idx


# Append text to an existing message
def append_to_message(state: State, message: str, idx: int):
    if idx < len(state.messages):
        msg = state.messages[idx]
        state.messages[idx] = (msg[0], f"{msg[1]}{message}", msg[2])
        state.refresh("messages")
    return idx


# Invoke the Wikipedia API. This is invoked by send_message()
def request_wikipedia(gui: Gui, state_id: str, event_type: str, month: str, day: str):
    # Let the user known that a query was sent
    idx = invoke_callback(
        gui,
        state_id,
        set_message,
        ["Fetching information from Wikipedia ..."],
    )
    request = wiki_url.format(type=event_type, month=month, day=day)
    req = requests.get(request, headers={"accept": "application/json; charset=utf-8;", "User-Agent": user_agent})
    # Handle the response
    if req.status_code == 200:
        # Display the response
        invoke_callback(
            gui,
            state_id,
            update_message,
            [req.json(), event_type, f"{day}/{month}", idx],
        )
    else:
        # Display the error
        invoke_callback(
            gui,
            state_id,
            set_message,
            [f"Wikipedia API call failed: {req.status_code}", idx],
        )


# Invoked by the 'on_action' callback of the chat control when the user presses the Send button
def send_message(state: State, id: str, payload: dict):
    args = payload.get("args", [])