From 212961ef6a0611cff0803ebf411f0d32a924405d Mon Sep 17 00:00:00 2001 From: space-nuko <24979496+space-nuko@users.noreply.github.com> Date: Mon, 27 Mar 2023 18:55:11 -0500 Subject: [PATCH] Support empty lists being used in `gr.Dataframe` (#3646) * Support empty lists being used in `gr.Dataframe` * Update changelog * Add empty dataframe test --------- Co-authored-by: Abubakar Abid --- CHANGELOG.md | 1 + gradio/components.py | 2 ++ test/test_components.py | 2 ++ 3 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d39800388a..b3fd8f5154 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Fixed bug where chatbot does not autoscroll inside of a tab, row or column by [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 3637](https://github.com/gradio-app/gradio/pull/3637) - Fixed bug where textbox shrinks when `lines` set to larger than 20 by [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 3637](https://github.com/gradio-app/gradio/pull/3637) - Ensure CSS has fully loaded before rendering the application, by [@pngwn](https://github.com/pngwn) in [PR 3573](https://github.com/gradio-app/gradio/pull/3573) +- Support using an empty list as `gr.Dataframe` value, by [@space-nuko](https://github.com/space-nuko) in [PR 3646](https://github.com/gradio-app/gradio/pull/3646) ## Documentation Changes: diff --git a/gradio/components.py b/gradio/components.py index 2a7695399f..2aa7f499a1 100644 --- a/gradio/components.py +++ b/gradio/components.py @@ -2878,6 +2878,8 @@ class Dataframe(Changeable, Selectable, IOComponent, JSONSerializable): ), } if isinstance(y, (np.ndarray, list)): + if len(y) == 0: + return self.postprocess([[]]) if isinstance(y, np.ndarray): y = y.tolist() assert isinstance(y, list), "output cannot be converted to list" diff --git a/test/test_components.py b/test/test_components.py index bfee1c769b..3fc3d9ad19 100644 --- a/test/test_components.py +++ b/test/test_components.py @@ -1096,6 +1096,8 @@ class TestDataframe: postprocess """ dataframe_output = gr.Dataframe() + output = dataframe_output.postprocess([]) + assert output == {"data": [[]], "headers": []} output = dataframe_output.postprocess(np.zeros((2, 2))) assert output == {"data": [[0, 0], [0, 0]], "headers": [1, 2]} output = dataframe_output.postprocess([[1, 3, 5]])