-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get
ui.Chat()
working inside Shiny modules (#1582)
- Loading branch information
Showing
4 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from htmltools import Tag | ||
|
||
from shiny import App, Inputs, Outputs, Session, module, ui | ||
|
||
|
||
@module.ui | ||
def chat_mod_ui() -> Tag: | ||
return ui.chat_ui(id="chat") | ||
|
||
|
||
@module.server | ||
def chat_mod_server(input: Inputs, output: Outputs, session: Session): | ||
chat = ui.Chat(id="chat") | ||
|
||
@chat.on_user_submit | ||
async def _(): | ||
user = chat.user_input() | ||
await chat.append_message(f"You said: {user}") | ||
|
||
|
||
app_ui = ui.page_fillable( | ||
ui.panel_title("Hello Shiny Chat"), | ||
chat_mod_ui("foo"), | ||
fillable_mobile=True, | ||
) | ||
|
||
|
||
def server(input: Inputs, output: Outputs, session: Session): | ||
chat_mod_server("foo") | ||
|
||
|
||
app = App(app_ui, server) |
18 changes: 18 additions & 0 deletions
18
tests/playwright/shiny/components/chat/module/test_chat_module.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from playwright.sync_api import Page, expect | ||
from utils.deploy_utils import skip_on_webkit | ||
|
||
from shiny.playwright import controller | ||
from shiny.run import ShinyAppProc | ||
|
||
|
||
@skip_on_webkit | ||
def test_validate_chat_append_user_message(page: Page, local_app: ShinyAppProc) -> None: | ||
page.goto(local_app.url) | ||
|
||
chat = controller.Chat(page, "foo-chat") | ||
|
||
# Verify starting state | ||
expect(chat.loc).to_be_visible(timeout=30 * 1000) | ||
chat.set_user_input("A user message") | ||
chat.send_user_input() | ||
chat.expect_latest_message("You said: A user message", timeout=30 * 1000) |