-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
70 changed files
with
985 additions
and
4,010 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,51 @@ | ||
""" | ||
Demonstrates how to chain responses from a single message in the callback. | ||
Highlight: | ||
- The `respond` parameter in the `send` method is used to chain responses. | ||
- It's also possible to use `respond` as a method to chain responses. | ||
""" | ||
|
||
from asyncio import sleep | ||
|
||
import panel as pn | ||
|
||
pn.extension() | ||
|
||
PERSON_1 = "Happy User" | ||
PERSON_2 = "Excited User" | ||
PERSON_3 = "Passionate User" | ||
|
||
|
||
async def callback(contents: str, user: str, instance: pn.chat.ChatInterface): | ||
await sleep(2) | ||
if user == "User": | ||
instance.send( | ||
f"Hey, {PERSON_2}! Did you hear the user?", | ||
user=PERSON_1, | ||
avatar="😊", | ||
respond=True, # This is the default, but it's here for clarity | ||
) | ||
elif user == PERSON_1: | ||
user_message = instance.objects[-2] | ||
user_contents = user_message.object | ||
yield pn.chat.ChatMessage( | ||
f'Yeah, they said "{user_contents}"! Did you also hear {PERSON_3}?', | ||
user=PERSON_2, | ||
avatar="😄", | ||
) | ||
instance.respond() | ||
elif user == PERSON_2: | ||
instance.send( | ||
f"Yup, I heard!", | ||
user=PERSON_3, | ||
avatar="😆", | ||
respond=False, | ||
) | ||
|
||
|
||
chat_interface = pn.chat.ChatInterface( | ||
help_text="Send a message to start the conversation!", callback=callback | ||
) | ||
chat_interface.servable() |
58 changes: 58 additions & 0 deletions
58
docs/examples/feature/feature_control_callback_response.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,58 @@ | ||
""" | ||
Demonstrates how to precisely control the callback response. | ||
Highlights: | ||
- Use a placeholder text to display a message while waiting for the response. | ||
- Use a placeholder threshold to control when the placeholder text is displayed. | ||
- Use send instead of stream/yield/return to keep the placeholder text while still sending a message, ensuring respond=False to avoid a recursive loop. | ||
- Use yield to continuously update the response message. | ||
- Use pn.chat.ChatMessage or dict to send a message with a custom user and avatar. | ||
""" | ||
|
||
from asyncio import sleep | ||
from random import choice | ||
|
||
import panel as pn | ||
|
||
pn.extension() | ||
|
||
|
||
async def callback(contents: str, user: str, instance: pn.chat.ChatInterface): | ||
await sleep(0.5) | ||
# use send instead of stream/yield/return to keep the placeholder text | ||
# while still sending a message; ensure respond=False to avoid a recursive loop | ||
instance.send( | ||
"Let me flip the coin for you...", user="Game Master", avatar="🎲", respond=False | ||
) | ||
await sleep(1) | ||
|
||
characters = "/|\\_" | ||
index = 0 | ||
for _ in range(0, 28): | ||
index = (index + 1) % len(characters) | ||
# use yield to continuously update the response message | ||
# use pn.chat.ChatMessage to send a message with a custom user and avatar | ||
yield pn.chat.ChatMessage("\r" + characters[index], user="Coin", avatar="🪙") | ||
await sleep(0.005) | ||
|
||
result = choice(["heads", "tails"]) | ||
if result in contents.lower(): | ||
# equivalently, use a dict instead of a pn.chat.ChatMessage | ||
yield {"object": f"Woohoo, {result}! You win!", "user": "Coin", "avatar": "🎲"} | ||
else: | ||
yield {"object": f"Aw, got {result}. Try again!", "user": "Coin", "avatar": "🎲"} | ||
|
||
|
||
chat_interface = pn.chat.ChatInterface( | ||
widgets=[ | ||
pn.widgets.RadioButtonGroup( | ||
options=["Heads!", "Tails!"], button_type="primary", button_style="outline" | ||
) | ||
], | ||
callback=callback, | ||
help_text="Select heads or tails, then click send!", | ||
placeholder_text="Waiting for the result...", | ||
placeholder_threshold=0.1, | ||
) | ||
chat_interface.servable() |
Oops, something went wrong.