Skip to content

Commit

Permalink
ensure @gradio/client always returns the correct data (#8716)
Browse files Browse the repository at this point in the history
* ensure data is alwat returned as expected

* add changeset

* update test data

* fix test

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
pngwn and gradio-pr-bot committed Jul 11, 2024
1 parent a6b3c6c commit e834d30
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 37 deletions.
6 changes: 6 additions & 0 deletions .changeset/rare-frogs-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/client": patch
"gradio": patch
---

fix:ensure `@gradio/client` always returns the correct data
7 changes: 4 additions & 3 deletions client/js/src/helpers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,15 @@ export function handle_payload(

let updated_payload: unknown[] = [];
let payload_index = 0;
for (let i = 0; i < dependency.inputs.length; i++) {
const input_id = dependency.inputs[i];
const deps = type === "input" ? dependency.inputs : dependency.outputs;
for (let i = 0; i < deps.length; i++) {
const input_id = deps[i];
const component = components.find((c) => c.id === input_id);

if (component?.type === "state") {
// input + with_null_state needs us to fill state with null values
if (with_null_state) {
if (resolved_payload.length === dependency.inputs.length) {
if (resolved_payload.length === deps.length) {
const value = resolved_payload[payload_index];
updated_payload.push(value);
payload_index++;
Expand Down
7 changes: 4 additions & 3 deletions client/js/src/test/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ describe("handle_payload", () => {
it("should return an output payload without the state component value when with_null_state is false", () => {
const resolved_payload = ["hello", null];
const dependency = {
inputs: [2, 3]
outputs: [2, 3]
};
const components = [
{ id: 2, type: "textbox" },
Expand All @@ -383,7 +383,7 @@ describe("handle_payload", () => {
it("should return an ouput payload without the two state component values when with_null_state is false", () => {
const resolved_payload = ["hello", null, "world", null];
const dependency = {
inputs: [2, 3, 4, 5]
outputs: [2, 3, 4, 5]
};
const components = [
{ id: 2, type: "textbox" },
Expand All @@ -406,7 +406,7 @@ describe("handle_payload", () => {
it("should return an ouput payload with the two state component values when with_null_state is true", () => {
const resolved_payload = ["hello", null, "world", null];
const dependency = {
inputs: [2, 3, 4, 5]
outputs: [2, 3, 4, 5]
};
const components = [
{ id: 2, type: "textbox" },
Expand Down Expand Up @@ -441,6 +441,7 @@ describe("handle_payload", () => {
// @ts-ignore
dependency,
components,
"input",
with_null_state
);
expect(result).toEqual(["hello", "world"]);
Expand Down
5 changes: 4 additions & 1 deletion client/js/src/utils/handle_blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ export async function handle_blob(
path,
file_url,
type,
name: blob instanceof File ? blob?.name : undefined
name:
typeof File !== "undefined" && blob instanceof File
? blob?.name
: undefined
};
})
);
Expand Down
30 changes: 0 additions & 30 deletions js/app/test/image_remote_url.spec.ts

This file was deleted.

0 comments on commit e834d30

Please sign in to comment.