File size: 867 Bytes
2b4d75c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Chatbot
# Use this card for chat interactions.
# #chat
# ---
from h2o_wave import main, app, Q, ui, data


MAX_MESSAGES = 500


@app('/demo')
async def serve(q: Q):
    if not q.client.initialized:
        # Cyclic buffer drops oldest messages when full. Must have exactly 2 columns - msg and fromUser.
        cyclic_buffer = data(fields='msg fromUser', size=-MAX_MESSAGES)
        q.page['example'] = ui.chatbot_card(box='1 1 5 5', data=cyclic_buffer, name='chatbot')
        q.page['meta'] = ui.meta_card(box='', theme='h2o-dark')
        q.client.initialized = True

    # A new message arrived.
    if q.args.chatbot:
        # Append user message.
        q.page['example'].data[-1] = [q.args.chatbot, True]
        # Append bot response.
        q.page['example'].data[-1] = ['I am a fake chatbot. Sorry, I cannot help you.', False]

    await q.page.save()