chore: import upstream snapshot with attribution
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import matplotlib.pyplot as plt
|
||||
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_postprocess_closes_matplotlib_figure(self):
|
||||
"""
|
||||
postprocess
|
||||
"""
|
||||
with utils.MatplotlibBackendMananger():
|
||||
plt.close("all")
|
||||
component = gr.Plot()
|
||||
fig = plt.figure()
|
||||
plt.plot([1, 2, 3], [1, 2, 3])
|
||||
component.postprocess(fig)
|
||||
assert not plt.get_fignums()
|
||||
|
||||
def test_postprocess_accepts_closed_figure(self):
|
||||
"""
|
||||
postprocess
|
||||
"""
|
||||
with utils.MatplotlibBackendMananger():
|
||||
component = gr.Plot(format="png")
|
||||
fig = plt.figure()
|
||||
plt.plot([1, 2, 3], [1, 2, 3])
|
||||
first = component.postprocess(fig)
|
||||
second = component.postprocess(fig)
|
||||
assert first and second and first.plot == second.plot
|
||||
|
||||
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")
|
||||
Reference in New Issue
Block a user