Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataframe support in Chatbot #10225

Merged
merged 14 commits into from
Dec 23, 2024
6 changes: 6 additions & 0 deletions .changeset/long-months-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/chatbot": minor
"gradio": minor
---

feat:Dataframe support in Chatbot
2 changes: 1 addition & 1 deletion demo/chatbot_core_components/run.ipynb

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions demo/chatbot_core_components/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ def bot(history, response_type):
content = gr.Gallery(
[os.path.join("files", "avatar.png"), os.path.join("files", "avatar.png")]
)
elif response_type == "dataframe":
content = gr.Dataframe(
interactive=True,
headers=["One", "Two", "Three"],
col_count=(3, "fixed"),
row_count=(3, "fixed"),
value=[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
label="Dataframe",
)
elif response_type == "image":
content = gr.Image(os.path.join("files", "avatar.png"))
elif response_type == "video":
Expand Down Expand Up @@ -216,6 +225,7 @@ def bot(history, response_type):
"image",
"text",
"gallery",
"dataframe",
"video",
"audio",
"html",
Expand Down
8 changes: 4 additions & 4 deletions js/chatbot/shared/ChatBot.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,10 @@
}

/* table styles */
.message-wrap :global(.bot table),
.message-wrap :global(.bot tr),
.message-wrap :global(.bot td),
.message-wrap :global(.bot th) {
.message-wrap :global(.bot:not(:has(.table-wrap)) table),
.message-wrap :global(.bot:not(:has(.table-wrap)) tr),
.message-wrap :global(.bot:not(:has(.table-wrap)) td),
.message-wrap :global(.bot:not(:has(.table-wrap)) th) {
border: 1px solid var(--border-color-primary);
}

Expand Down
27 changes: 26 additions & 1 deletion js/chatbot/shared/Component.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<script lang="ts">
export let type: "gallery" | "plot" | "audio" | "video" | "image" | string;
export let type:
| "gallery"
| "plot"
| "audio"
| "video"
| "image"
| "dataframe"
| string;
export let components;
export let value;
export let target;
Expand Down Expand Up @@ -27,6 +34,24 @@
fixed_height={1}
on:load
/>
{:else if type === "dataframe"}
<svelte:component
this={components[type]}
{value}
show_label={false}
{i18n}
label=""
interactive={false}
line_breaks={props.line_breaks}
wrap={true}
root=""
gradio={{ dispatch: () => {} }}
datatype={props.datatype}
latex_delimiters={props.latex_delimiters}
col_count={props.col_count}
row_count={props.row_count}
on:load
/>
{:else if type === "plot"}
<svelte:component
this={components[type]}
Expand Down
6 changes: 6 additions & 0 deletions js/chatbot/shared/Message.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@
text-align: right;
}

.bot:has(.table-wrap) {
border: none;
box-shadow: none;
background: none;
}

.panel .user :global(*) {
text-align: right;
}
Expand Down
5 changes: 2 additions & 3 deletions js/chatbot/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,12 @@ export async function load_components(
if (_components[component_name] || component_name === "file") {
return;
}

const { name, component } = load_component(component_name, "base");
const variant = component_name === "dataframe" ? "component" : "base";
const { name, component } = load_component(component_name, variant);
names.push(name);
components.push(component);
component_name;
});

const loaded_components: LoadedComponent[] = await Promise.all(components);
loaded_components.forEach((component, i) => {
_components[names[i]] = component.default;
Expand Down
Loading