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

Fix programmatic tab selection #7916

Merged
merged 14 commits into from Apr 8, 2024
Merged

Fix programmatic tab selection #7916

merged 14 commits into from Apr 8, 2024

Conversation

aliabid94
Copy link
Collaborator

@aliabid94 aliabid94 commented Apr 2, 2024

selected was not being respected for tabitems, because the id property of a tabitem was being overwritten internally. Renamed internal use of id to _id.

Test with:

import gradio as gr

def select_c():
    return gr.Tabs(selected="c")

with gr.Blocks() as demo:
    with gr.Tabs(selected="b") as tabs:
        with gr.Tab("A", id="a"):
            gr.Markdown("## A")
        with gr.Tab("B", id="b"):
            gr.Markdown("## B")
        with gr.Tab("C", id="c"):
            gr.Markdown("## C")

    btn = gr.Button("Switch to tab C")
    btn.click(select_c, None, tabs)

demo.launch()

Fixes: #7726

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Apr 2, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detecting...

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/56c24885b35b84121678e006a5094952ca9d67d2/gradio-4.25.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@56c24885b35b84121678e006a5094952ca9d67d2#subdirectory=client/python"

Ali Abid and others added 2 commits April 2, 2024 15:06
@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Apr 2, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/app patch
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Fix programmatic tab selection

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nice catch. Thanks @aliabid94!

We should probably lint against 'shadowing' variables. I wonder if this fixes #7786 as well?

Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice @aliabid94 ! Can we add a test for this? It's regressed before

Ali Abid and others added 2 commits April 3, 2024 13:33
@aliabid94
Copy link
Collaborator Author

Added tests. While I was adding tests, I caught an error that wouldn't show up in dev mode for some reason: If a gr.Tabs() had selected= set to a value in its constructor, than any event listener trigger that would cause a redraw would reset the gr.Tabs to the selected tab. I made changes to Tab.svelte to fix this, @pngwn if you could just take a quick pass at it again.

@aliabid94
Copy link
Collaborator Author

actually my solution won't work in the case that an app programmatically tries to set the selected tab to the same tab a second time after a user moved away from that tab. Is there any way for gradio to know if the selected tab being passed is the result of an event listener output, or just a redraw resetting the prop?

@pngwn
Copy link
Member

pngwn commented Apr 3, 2024

What is the actually problem, sorry. In principle we shouldn't need to know the difference.

@hannahblair
Copy link
Collaborator

hannahblair commented Apr 5, 2024

Yep, when selecting away from Tab C, I'm unable to select it again: Kapture 2024-04-05 at 14 50 03

Sounds like the same problem you're referring to @aliabid94. However, removing the new logic that was added in Tabs.svelte seem to fix it. Do we need the old_selected_prop and _selected vars?

Kapture 2024-04-05 at 15 19 41

Edit: I realise you added these changes due to

Added tests. While I was adding tests, I caught an error that wouldn't show up in dev mode for some reason ...

@pngwn
Copy link
Member

pngwn commented Apr 5, 2024

This feels like the accordion thing tbh. Gonna take a closer look.

@aliabid94
Copy link
Collaborator Author

Sorry let me clarify. The issue I was trying to fix with selected, _selected, and old_selected_prop is that event listeners were causing the component to re-render, which was causing the props to be reset to their set values. So the selected tab would get reset to the constructor-set value. So if there was a gr.Tabs(selected="b") setting the default to "b", and a user clicked away to tab "a", when an event listener triggered a re-render, the tab would reset to "b".

@pngwn
Copy link
Member

pngwn commented Apr 6, 2024

I see. That does sound very similar to the accordion issues where it resets to the default.

@pngwn
Copy link
Member

pngwn commented Apr 8, 2024

@aliabid94 I have just opened #7963 to fix the accordion 'reset' issue, and this feels similar. When that is merged in i think if you merge main into this it may fix the remaining issue you are having.

@pngwn
Copy link
Member

pngwn commented Apr 8, 2024

#7963 is merged now, I've updated this branch with main. Could you retest now @aliabid94?

@aliabid94
Copy link
Collaborator Author

Works great, thanks @pngwn!

Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for fixing this and adding those extra tests @aliabid94!

@aliabid94 aliabid94 merged commit 7c9a964 into main Apr 8, 2024
8 checks passed
@aliabid94 aliabid94 deleted the fix_selected_tab branch April 8, 2024 18:34
@pngwn pngwn mentioned this pull request Apr 8, 2024
freddyaboulton pushed a commit that referenced this pull request Apr 11, 2024
* changes

* changes

* add changeset

* changes

* add changeset

* changes

* add changeset

---------

Co-authored-by: Ali Abid <aliabid94@gmail.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: pngwn <hello@pngwn.io>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
@arose26
Copy link

arose26 commented Apr 27, 2024

So, how do I change the active tab programmatically? (not from a button, just from code).
I've tried changing the tab instance's 'selected' property directly but that only works BEFORE calling launch. After launch, it doesn't do the job. gr.Tabs.update(selected=newid) doesn't work either.
I want to change selected tab in real time, from python code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Programmatically selecting tabs is broken
7 participants