Avaiga / taipy-doc

Holds the documentation set for Taipy, not including the code documentation obviously.
https://docs.taipy.io/
Apache License 2.0
12 stars 14 forks source link

Add documentation on muti-user with shared variables, broadcast_callback, ... #891

Closed FlorianJacta closed 3 months ago

FlorianJacta commented 5 months ago

Description Add documentation on Shared variables and broadcasting mechanisms in the documentation.

Expected change An article could be created inside the Tutorials section as well as the User Manual.

AlexandreSajus commented 5 months ago

I propose we use this simple two-user chatbot that uses both broadcast and add_shared_variable:

multi_user

from taipy.gui import Gui, State
import pandas as pd

timer_status = "Timer stopped"

client_index = 1
user_name = ""
conversation = pd.DataFrame(
    {"User": ["Alex", "Doppler"], "Message": ["Hey!", "Whats'up?"]}
)
current_message = ""
new_message = ""
new_sender = ""
selected_row = [2]

def on_init(state: State):
    global client_index
    if client_index == 1:
        state.user_name = "Alex"
    else:
        state.user_name = "Doppler"
    client_index += 1

# Start or stop the timer when the button is pressed
def send_message(state):
    state.broadcast("new_message", state.current_message)
    state.broadcast("new_sender", state.user_name)
    state.current_message = ""

def on_change(state, var_name, value):
    if var_name == "new_message":
        # Check if the last row is not already the user's message
        if (
            state.conversation.iloc[-1]["User"] != state.user_name
            or state.conversation.iloc[-1]["Message"] != state.new_message
        ):
            state.conversation = pd.concat(
                [
                    state.conversation,
                    pd.DataFrame(
                        {"User": [state.user_name], "Message": [state.new_message]}
                    ),
                ],
                ignore_index=True,
            )
        state.selected_row = [len(state.conversation["User"]) + 1]

page = """# Multi-User **Chat**{: .color-primary}

Username: <|{user_name}|input|active=false|>

<|{conversation}|table|rebuild|show_all|class_name=table|selected={selected_row}|>

<|{current_message}|input|label=Write your message here...|on_action=send_message|class_name=fullwidth|change_delay=-1|>
"""

gui = Gui(page)
gui.add_shared_variable("conversation")
gui.run(dark_mode=True)

I could take point on this. Where should this be in the doc? Should this be a "Fundamentals" tutorial about Multi-User capabilities? The user manual on broadcast and add_shared_variable could point to this tutorial.

EDIT: the tutorial should introduce the shared variable approach first

jrobinAV commented 3 months ago

@AlexandreSajus Are you working on that? Otherwise please unassign it. Thank you for your understanding.

AlexandreSajus commented 3 months ago

Yes, I am working on that currently. I was waiting for https://github.com/Avaiga/taipy/issues/1080 to be fixed first

jrobinAV commented 3 months ago

Great. Thx