|  | 
| 1 |  | -from shiny.express import expressify, ui | 
|  | 1 | +from shiny.express import ui | 
| 2 | 2 | 
 | 
|  | 3 | +# Set some Shiny page options | 
|  | 4 | +ui.page_opts( | 
|  | 5 | +    title="Hello Shiny Chat", | 
|  | 6 | +    fillable=True, | 
|  | 7 | +    fillable_mobile=True, | 
|  | 8 | +) | 
| 3 | 9 | 
 | 
| 4 |  | -@expressify | 
| 5 |  | -def card_suggestion(title: str, suggestion: str, img_src: str, img_alt: str): | 
| 6 |  | -    with ui.card(data_suggestion=suggestion): | 
| 7 |  | -        ui.card_header(title) | 
| 8 |  | -        ui.img( | 
| 9 |  | -            src=img_src, | 
| 10 |  | -            alt=img_alt, | 
| 11 |  | -            style="margin-top:auto; margin-bottom:auto;", | 
| 12 |  | -        ) | 
| 13 |  | - | 
| 14 |  | - | 
| 15 |  | -@expressify | 
| 16 |  | -def card_suggestions(): | 
| 17 |  | -    with ui.layout_column_wrap(): | 
| 18 |  | -        card_suggestion( | 
| 19 |  | -            title="Learn Python", | 
| 20 |  | -            suggestion="Teach me Python", | 
| 21 |  | -            img_src="https://upload.wikimedia.org/wikipedia/commons/c/c3/Python-logo-notext.svg", | 
| 22 |  | -            img_alt="Python logo", | 
| 23 |  | -        ) | 
| 24 |  | -        card_suggestion( | 
| 25 |  | -            title="Learn R", | 
| 26 |  | -            suggestion="Teach me R", | 
| 27 |  | -            img_src="https://upload.wikimedia.org/wikipedia/commons/1/1b/R_logo.svg", | 
| 28 |  | -            img_alt="R logo", | 
| 29 |  | -        ) | 
| 30 |  | - | 
| 31 |  | - | 
| 32 |  | -with ui.hold() as suggestions: | 
| 33 |  | -    card_suggestions() | 
| 34 |  | - | 
| 35 |  | -welcome = f""" | 
| 36 |  | -**Hello!** How can I help you today? | 
| 37 |  | -
 | 
| 38 |  | -Here are a couple suggestions: | 
| 39 |  | -
 | 
| 40 |  | -{suggestions[0]} | 
|  | 10 | +# Create a welcome message | 
|  | 11 | +welcome = """ | 
|  | 12 | +Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will | 
|  | 13 | +simply repeat it back to you. For more examples, see this | 
|  | 14 | +[folder of examples](https://github.com/posit-dev/py-shiny/tree/main/shiny/templates/chat). | 
| 41 | 15 | """ | 
| 42 | 16 | 
 | 
|  | 17 | +# Create a chat instance | 
| 43 | 18 | chat = ui.Chat( | 
| 44 | 19 |     id="chat", | 
| 45 | 20 |     messages=[welcome], | 
| 46 | 21 | ) | 
| 47 | 22 | 
 | 
|  | 23 | +# Display it | 
| 48 | 24 | chat.ui() | 
| 49 | 25 | 
 | 
| 50 | 26 | 
 | 
|  | 27 | +# Define a callback to run when the user submits a message | 
| 51 | 28 | @chat.on_user_submit | 
| 52 | 29 | async def handle_user_input(user_input: str): | 
|  | 30 | +    # Append a response to the chat | 
| 53 | 31 |     await chat.append_message(f"You said: {user_input}") | 
0 commit comments