Skip to content

Commit

Permalink
Support multi args
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 committed Jun 13, 2024
1 parent be7d6c0 commit 9765df5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
14 changes: 11 additions & 3 deletions panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

from enum import Enum
from inspect import (
isasyncgen, isasyncgenfunction, isawaitable, iscoroutinefunction,
isgenerator, isgeneratorfunction,
getfullargspec, isasyncgen, isasyncgenfunction, isawaitable,
iscoroutinefunction, isgenerator, isgeneratorfunction,
)
from io import BytesIO
from typing import (
Expand Down Expand Up @@ -430,7 +430,15 @@ def _gather_callback_args(self, message: ChatMessage) -> Any:
contents = value.value
else:
contents = value
return contents, message.user, self

callback_args = getfullargspec(self.callback).args
if len(callback_args) > 3:
raise ValueError('Function should have at most 3 arguments')
elif len(callback_args) == 0:
raise ValueError('Function should have at least one argument')

input_args = (contents, message.user, self)
return input_args[:len(callback_args)]

async def _serialize_response(self, response: Any) -> ChatMessage | None:
"""
Expand Down
53 changes: 53 additions & 0 deletions panel/tests/chat/test_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,59 @@ def callback(contents, user, instance):
assert feed.objects[-1].object == "helloooo"
assert chat_feed._placeholder not in chat_feed._chat_log

def test_callback_one_argument(self, chat_feed):
def callback(contents):
return contents

chat_feed.callback = callback
chat_feed.send("Message", respond=True)
wait_until(lambda: len(chat_feed.objects) == 2)
assert chat_feed.objects[1].object == "Message"

def test_callback_two_arguments(self, chat_feed):
def callback(contents, user):
return f"{user}: {contents}"

chat_feed.callback = callback
chat_feed.send("Message", respond=True)
wait_until(lambda: len(chat_feed.objects) == 2)
assert chat_feed.objects[1].object == "User: Message"

def test_callback_two_arguments_with_keyword(self, chat_feed):
def callback(contents, user=None):
return f"{user}: {contents}"

chat_feed.callback = callback
chat_feed.send("Message", respond=True)
wait_until(lambda: len(chat_feed.objects) == 2)
assert chat_feed.objects[1].object == "User: Message"

def test_callback_three_arguments_with_keyword(self, chat_feed):
def callback(contents, user=None, instance=None):
return f"{user}: {contents}"

chat_feed.callback = callback
chat_feed.send("Message", respond=True)
wait_until(lambda: len(chat_feed.objects) == 2)
assert chat_feed.objects[1].object == "User: Message"

def test_callback_two_arguments_yield(self, chat_feed):
def callback(contents, user):
yield f"{user}: {contents}"

chat_feed.callback = callback
chat_feed.send("Message", respond=True)
wait_until(lambda: len(chat_feed.objects) == 2)
assert chat_feed.objects[1].object == "User: Message"

def test_callback_two_arguments_async_yield(self, chat_feed):
async def callback(contents, user):
yield f"{user}: {contents}"

chat_feed.callback = callback
chat_feed.send("Message", respond=True)
wait_until(lambda: len(chat_feed.objects) == 2)
assert chat_feed.objects[1].object == "User: Message"

@pytest.mark.xdist_group("chat")
class TestChatFeedSerializeForTransformers:
Expand Down

0 comments on commit 9765df5

Please sign in to comment.