-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Refactor gr.Dataframe
#10631
Draft
hannahblair
wants to merge
19
commits into
main
Choose a base branch
from
improve-df
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,617
−1,287
Draft
Refactor gr.Dataframe
#10631
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e9eb015
allow showing image sin dataframe with image component
hannahblair 3370ebf
add scroll to top button
hannahblair f403365
add changeset
gradio-pr-bot 13ae83e
Merge branch 'main' into improve-df
hannahblair 1e3e7a2
fix truncated text issue + prevent truncating non-text data
hannahblair 0b2e39b
Merge branch 'improve-df' of github.com:gradio-app/gradio into improv…
hannahblair a237d3b
refactor with state mgmnt
hannahblair 2a9efcb
Merge branch 'main' into improve-df
hannahblair 92fa6c1
add changeset
gradio-pr-bot ca8b7e3
formatting
hannahblair 05198ca
Merge branch 'improve-df' of github.com:gradio-app/gradio into improv…
hannahblair a97556f
tweaks
hannahblair ef80b12
tweak
hannahblair 90a3de1
add e2e tests
hannahblair 758c208
notebook
hannahblair 3abef78
more component extraction, move css to components
hannahblair 16bc197
Merge branch 'main' into improve-df
hannahblair 07480a0
type fixes
hannahblair ddb8c88
Merge branch 'improve-df' of github.com:gradio-app/gradio into improv…
hannahblair File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@gradio/dataframe": minor | ||
"gradio": minor | ||
--- | ||
|
||
feat:Refactor `gr.Dataframe` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: dataframe_events"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import pandas as pd\n", "import numpy as np\n", "\n", "def update_dataframe():\n", " regular_df = pd.DataFrame(np.random.randint(1, 10, size=(5, 5)))\n", " regular_df.columns = [str(col) for col in regular_df.columns]\n", "\n", " wide_df = pd.DataFrame(np.random.randint(1, 10, size=(5, 15)))\n", " wide_df.columns = [f\"col_{col}\" for col in wide_df.columns]\n", "\n", " tall_df = pd.DataFrame(np.random.randint(1, 10, size=(50, 3)))\n", " tall_df.columns = [\"A\", \"B\", \"C\"]\n", "\n", " return regular_df, wide_df, tall_df\n", "\n", "def clear_dataframes():\n", " regular_empty_df = pd.DataFrame(columns=[str(i) for i in range(5)])\n", " wide_empty_df = pd.DataFrame(columns=[f\"col_{i}\" for i in range(15)])\n", " tall_empty_df = pd.DataFrame(columns=[\"A\", \"B\", \"C\"])\n", " return regular_empty_df, wide_empty_df, tall_empty_df\n", "\n", "def increment_select_counter(evt: gr.SelectData, count):\n", " count_val = 1 if count is None else count + 1\n", " return count_val, evt.index, evt.value\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " with gr.Column(scale=1):\n", " initial_regular_df = pd.DataFrame(np.zeros((5, 5), dtype=int))\n", " initial_regular_df.columns = [str(col) for col in initial_regular_df.columns]\n", "\n", " df = gr.Dataframe(\n", " value=initial_regular_df,\n", " interactive=True,\n", " label=\"Interactive Dataframe\",\n", " show_label=True,\n", " elem_id=\"dataframe\",\n", " show_search=\"filter\",\n", " show_copy_button=True,\n", " show_row_numbers=True\n", " )\n", "\n", " with gr.Column(scale=1):\n", " initial_wide_df = pd.DataFrame(np.zeros((5, 15), dtype=int))\n", " initial_wide_df.columns = [f\"col_{col}\" for col in initial_wide_df.columns]\n", "\n", " df_view = gr.Dataframe(\n", " value=initial_wide_df,\n", " interactive=False,\n", " label=\"Non-Interactive View (Scroll Horizontally)\",\n", " show_label=True,\n", " show_search=\"search\",\n", " elem_id=\"non-interactive-dataframe\",\n", " show_copy_button=True,\n", " show_row_numbers=True,\n", " show_fullscreen_button=True\n", " )\n", "\n", " with gr.Row():\n", " initial_tall_df = pd.DataFrame(np.zeros((50, 3), dtype=int))\n", " initial_tall_df.columns = [\"A\", \"B\", \"C\"]\n", "\n", " df_tall = gr.Dataframe(\n", " value=initial_tall_df,\n", " interactive=False,\n", " label=\"Tall Dataframe (Scroll Vertically)\",\n", " show_label=True,\n", " elem_id=\"dataframe_tall\",\n", " show_copy_button=True,\n", " show_row_numbers=True,\n", " max_height=300\n", " )\n", "\n", " with gr.Row():\n", " with gr.Column():\n", " update_btn = gr.Button(\"Update dataframe\", elem_id=\"update_btn\")\n", " clear_btn = gr.Button(\"Clear dataframe\", elem_id=\"clear_btn\")\n", "\n", " with gr.Row():\n", " change_events = gr.Number(value=0, label=\"Change events\", elem_id=\"change_events\")\n", " input_events = gr.Number(value=0, label=\"Input events\", elem_id=\"input_events\")\n", " select_events = gr.Number(value=0, label=\"Select events\", elem_id=\"select_events\")\n", "\n", " with gr.Row():\n", " selected_cell_index = gr.Textbox(label=\"Selected cell index\", elem_id=\"selected_cell_index\")\n", " selected_cell_value = gr.Textbox(label=\"Selected cell value\", elem_id=\"selected_cell_value\")\n", "\n", " update_btn.click(\n", " fn=update_dataframe,\n", " outputs=[df, df_view, df_tall]\n", " )\n", "\n", " clear_btn.click(\n", " fn=clear_dataframes,\n", " outputs=[df, df_view, df_tall]\n", " )\n", "\n", " df.change(\n", " fn=lambda x: x + 1,\n", " inputs=[change_events],\n", " outputs=[change_events]\n", " )\n", "\n", " df.input(\n", " fn=lambda x: x + 1,\n", " inputs=[input_events],\n", " outputs=[input_events]\n", " )\n", "\n", " df.select(\n", " fn=increment_select_counter,\n", " inputs=[select_events],\n", " outputs=[select_events, selected_cell_index, selected_cell_value]\n", " )\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import gradio as gr | ||
import pandas as pd | ||
import numpy as np | ||
|
||
def update_dataframe(): | ||
regular_df = pd.DataFrame(np.random.randint(1, 10, size=(5, 5))) | ||
regular_df.columns = [str(col) for col in regular_df.columns] | ||
|
||
wide_df = pd.DataFrame(np.random.randint(1, 10, size=(5, 15))) | ||
wide_df.columns = [f"col_{col}" for col in wide_df.columns] | ||
|
||
tall_df = pd.DataFrame(np.random.randint(1, 10, size=(50, 3))) | ||
tall_df.columns = ["A", "B", "C"] | ||
|
||
return regular_df, wide_df, tall_df | ||
|
||
def clear_dataframes(): | ||
regular_empty_df = pd.DataFrame(columns=[str(i) for i in range(5)]) | ||
wide_empty_df = pd.DataFrame(columns=[f"col_{i}" for i in range(15)]) | ||
tall_empty_df = pd.DataFrame(columns=["A", "B", "C"]) | ||
return regular_empty_df, wide_empty_df, tall_empty_df | ||
|
||
def increment_select_counter(evt: gr.SelectData, count): | ||
count_val = 1 if count is None else count + 1 | ||
return count_val, evt.index, evt.value | ||
|
||
with gr.Blocks() as demo: | ||
with gr.Row(): | ||
with gr.Column(scale=1): | ||
initial_regular_df = pd.DataFrame(np.zeros((5, 5), dtype=int)) | ||
initial_regular_df.columns = [str(col) for col in initial_regular_df.columns] | ||
|
||
df = gr.Dataframe( | ||
value=initial_regular_df, | ||
interactive=True, | ||
label="Interactive Dataframe", | ||
show_label=True, | ||
elem_id="dataframe", | ||
show_search="filter", | ||
show_copy_button=True, | ||
show_row_numbers=True | ||
) | ||
|
||
with gr.Column(scale=1): | ||
initial_wide_df = pd.DataFrame(np.zeros((5, 15), dtype=int)) | ||
initial_wide_df.columns = [f"col_{col}" for col in initial_wide_df.columns] | ||
|
||
df_view = gr.Dataframe( | ||
value=initial_wide_df, | ||
interactive=False, | ||
label="Non-Interactive View (Scroll Horizontally)", | ||
show_label=True, | ||
show_search="search", | ||
elem_id="non-interactive-dataframe", | ||
show_copy_button=True, | ||
show_row_numbers=True, | ||
show_fullscreen_button=True | ||
) | ||
|
||
with gr.Row(): | ||
initial_tall_df = pd.DataFrame(np.zeros((50, 3), dtype=int)) | ||
initial_tall_df.columns = ["A", "B", "C"] | ||
|
||
df_tall = gr.Dataframe( | ||
value=initial_tall_df, | ||
interactive=False, | ||
label="Tall Dataframe (Scroll Vertically)", | ||
show_label=True, | ||
elem_id="dataframe_tall", | ||
show_copy_button=True, | ||
show_row_numbers=True, | ||
max_height=300 | ||
) | ||
|
||
with gr.Row(): | ||
with gr.Column(): | ||
update_btn = gr.Button("Update dataframe", elem_id="update_btn") | ||
clear_btn = gr.Button("Clear dataframe", elem_id="clear_btn") | ||
|
||
with gr.Row(): | ||
change_events = gr.Number(value=0, label="Change events", elem_id="change_events") | ||
input_events = gr.Number(value=0, label="Input events", elem_id="input_events") | ||
select_events = gr.Number(value=0, label="Select events", elem_id="select_events") | ||
|
||
with gr.Row(): | ||
selected_cell_index = gr.Textbox(label="Selected cell index", elem_id="selected_cell_index") | ||
selected_cell_value = gr.Textbox(label="Selected cell value", elem_id="selected_cell_value") | ||
|
||
update_btn.click( | ||
fn=update_dataframe, | ||
outputs=[df, df_view, df_tall] | ||
) | ||
|
||
clear_btn.click( | ||
fn=clear_dataframes, | ||
outputs=[df, df_view, df_tall] | ||
) | ||
|
||
df.change( | ||
fn=lambda x: x + 1, | ||
inputs=[change_events], | ||
outputs=[change_events] | ||
) | ||
|
||
df.input( | ||
fn=lambda x: x + 1, | ||
inputs=[input_events], | ||
outputs=[input_events] | ||
) | ||
|
||
df.select( | ||
fn=increment_select_counter, | ||
inputs=[select_events], | ||
outputs=[select_events, selected_cell_index, selected_cell_value] | ||
) | ||
|
||
if __name__ == "__main__": | ||
demo.launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<script lang="ts"> | ||
export let on_click: (event: MouseEvent) => void; | ||
</script> | ||
|
||
<button | ||
class="cell-menu-button" | ||
on:click={on_click} | ||
on:touchstart={(event) => { | ||
event.preventDefault(); | ||
const touch = event.touches[0]; | ||
const mouseEvent = new MouseEvent("click", { | ||
clientX: touch.clientX, | ||
clientY: touch.clientY, | ||
bubbles: true, | ||
cancelable: true, | ||
view: window | ||
}); | ||
on_click(mouseEvent); | ||
}} | ||
> | ||
⋮ | ||
</button> | ||
|
||
<style> | ||
.cell-menu-button { | ||
flex-shrink: 0; | ||
display: none; | ||
align-items: center; | ||
justify-content: center; | ||
background-color: var(--block-background-fill); | ||
border: 1px solid var(--border-color-primary); | ||
border-radius: var(--block-radius); | ||
width: var(--size-5); | ||
height: var(--size-5); | ||
min-width: var(--size-5); | ||
padding: 0; | ||
margin-right: var(--spacing-sm); | ||
z-index: var(--layer-1); | ||
position: absolute; | ||
right: var(--size-1); | ||
top: 50%; | ||
transform: translateY(-50%); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need this?