gradio/test/components/test_json.py
Hannah 600c97c807
Allow viewing JSON as list or dict with show_indices param (#8932)
* add line numbers and collapse + expand logic

* add story test and style tweaks

* add changeset

* allow expanding via preview

* story tweaks

* remove mobile/desktop story tests

* remove unused thing

* add mode param to view json as list

* add story

* add changeset

* add open param

* amend test

* * add cm-like theme colors
* prevent copy + pasting line numbers and toggle
* a11y tweaks

* remove mode as param and default to list view

* Revert "remove mode as param and default to list view"

This reverts commit 9051f15c5f.

* Update gradio/components/json_component.py

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* add changeset

* tweak

* fix nit

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2024-07-30 23:43:22 +00:00

105 lines
3.1 KiB
Python

import json
import numpy as np
import pytest
import gradio as gr
class TestJSON:
def test_component_functions(self):
"""
Postprocess
"""
js_output = gr.JSON()
assert js_output.postprocess('{"a":1, "b": 2}'), '"{\\"a\\":1, \\"b\\": 2}"'
assert js_output.get_config() == {
"container": True,
"min_width": 160,
"scale": None,
"elem_id": None,
"elem_classes": [],
"visible": True,
"value": None,
"show_label": True,
"label": None,
"name": "json",
"proxy_url": None,
"_selectable": False,
"open": False,
"key": None,
"show_indices": False,
}
js_component = gr.Json(value={"a": 1, "b": 2})
assert js_component.get_config()["value"] == {"a": 1, "b": 2}
def test_chatbot_selectable_in_config(self):
with gr.Blocks() as demo:
cb = gr.Chatbot(label="Chatbot")
cb.like(lambda: print("foo"))
gr.Chatbot(label="Chatbot2")
assertion_count = 0
for component in demo.config["components"]:
if component["props"]["label"] == "Chatbot":
assertion_count += 1
assert component["props"]["likeable"]
elif component["props"]["label"] == "Chatbot2":
assertion_count += 1
assert not component["props"]["likeable"]
assert assertion_count == 2
@pytest.mark.asyncio
async def test_in_interface(self):
"""
Interface, process
"""
def get_avg_age_per_gender(data):
return {
"M": int(data[data["gender"] == "M"]["age"].mean()),
"F": int(data[data["gender"] == "F"]["age"].mean()),
"O": int(data[data["gender"] == "O"]["age"].mean()),
}
iface = gr.Interface(
get_avg_age_per_gender,
gr.Dataframe(headers=["gender", "age"]),
"json",
)
y_data = [
["M", 30],
["F", 20],
["M", 40],
["O", 20],
["F", 30],
]
assert (
await iface.process_api(0, [{"data": y_data, "headers": ["gender", "age"]}])
)["data"][0].model_dump() == {
"M": 35,
"F": 25,
"O": 20,
}
@pytest.mark.parametrize(
"value, expected",
[
(None, None),
(True, True),
([1, 2, 3], [1, 2, 3]),
([np.array([1, 2, 3])], [[1, 2, 3]]),
({"foo": [1, 2, 3]}, {"foo": [1, 2, 3]}),
({"foo": np.array([1, 2, 3])}, {"foo": [1, 2, 3]}),
],
)
def test_postprocess_returns_json_serializable_value(self, value, expected):
json_component = gr.JSON()
postprocessed_value = json_component.postprocess(value)
if postprocessed_value is None:
assert value is None
else:
assert postprocessed_value.model_dump() == expected
assert json.loads(json.dumps(postprocessed_value.model_dump())) == expected