File size: 2,045 Bytes
0ad74ed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import pytest
import gradio as gr
from gradio import utils
class TestPlot:
@pytest.mark.asyncio
async def test_in_interface_as_output(self):
"""
Interface, process
"""
def plot(num):
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(range(num), range(num))
return fig
iface = gr.Interface(plot, "slider", "plot")
with utils.MatplotlibBackendMananger():
output = await iface.process_api(block_fn=0, inputs=[10])
assert output["data"][0]["type"] == "matplotlib"
assert output["data"][0]["plot"].startswith("data:image/webp;base64")
def test_static(self):
"""
postprocess
"""
with utils.MatplotlibBackendMananger():
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot([1, 2, 3], [1, 2, 3])
component = gr.Plot(fig)
assert component.get_config().get("value") is not None
component = gr.Plot(None)
assert component.get_config().get("value") is None
def test_postprocess_altair(self):
import altair as alt
from vega_datasets import data
cars = data.cars()
chart = (
alt.Chart(cars)
.mark_point()
.encode(
x="Horsepower",
y="Miles_per_Gallon",
color="Origin",
)
)
assert (out := gr.Plot().postprocess(chart))
out = out.model_dump()
assert isinstance(out["plot"], str)
assert out["plot"] == chart.to_json()
def test_plot_format_parameter(self):
"""
postprocess
"""
with utils.MatplotlibBackendMananger():
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot([1, 2, 3], [1, 2, 3])
component = gr.Plot(format="jpeg")
assert (result := component.postprocess(fig))
assert result.plot.startswith("data:image/jpeg")
|