Fix data model for gr.DataFrame (#6310)

* datamodel for df

* add changeset

* fix

* Add code

* reduce

* trigger ci

* typing

* union

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>
This commit is contained in:
Abubakar Abid 2023-11-06 19:04:23 -08:00 committed by GitHub
parent 0db1e2546a
commit dfdaf10926
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 4 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix:Fix data model for `gr.DataFrame`

View File

@ -19,9 +19,7 @@ from gradio.events import Events
class DataframeData(GradioModel):
headers: List[str]
data: List[List[Any]]
metadata: Optional[
Dict[str, List[Any]]
] = None # Optional[Dict[str, List[Any]]] = None
metadata: Optional[Dict[str, Optional[List[Any]]]] = None
set_documentation_group("component")
@ -216,7 +214,7 @@ class Dataframe(Component):
return DataframeData(
headers=list(df.columns),
data=df.to_dict(orient="split")["data"], # type: ignore
metadata=self.__extract_metadata(value),
metadata=self.__extract_metadata(value), # type: ignore
)
elif isinstance(value, (str, pd.DataFrame)):
df = pd.read_csv(value) if isinstance(value, str) else value # type: ignore

View File

@ -997,6 +997,14 @@ class TestDataframe:
dataframe_input = gr.Dataframe()
output = dataframe_input.preprocess(DataframeData(**x_data))
assert output["Age"][1] == 24
x_data = {
"data": [["Tim", 12, False], ["Jan", 24, True]],
"headers": ["Name", "Age", "Member"],
"metadata": {"display_value": None, "styling": None},
}
dataframe_input.preprocess(DataframeData(**x_data))
with pytest.raises(ValueError):
gr.Dataframe(type="unknown")