Skip to content

Allow Blocks instances to be used like a Block in other Blocks#919

Merged
abidlabs merged 12 commits into
blocks-devfrom
blocks-as-block
Apr 5, 2022
Merged

Allow Blocks instances to be used like a Block in other Blocks#919
abidlabs merged 12 commits into
blocks-devfrom
blocks-as-block

Conversation

@abidlabs
Copy link
Copy Markdown
Member

@abidlabs abidlabs commented Apr 2, 2022

This PR does a number of things:

  • Handle Markdown better: cleans up indentation from markdown blocks so that if every line is indented a certain amount, that is removed uniformly. Before this, any indented Markdown would show up as codeblocks (try running demo/xray_blocks/run.py to see the difference)
  • Allow running multiple Gradio servers from one Python session / jupyter notebook / colab notebook
  • Allow using Blocks/Interfaces in other Blocks:

So that we can do things like this:

with gr.Blocks() as bl:
    with gr.Tabs():
        with gr.TabItem("Text"):
            gr.Interface(lambda x:x, "textbox", "textbox")
        with gr.TabItem("Image"):
            gr.Interface(lambda x:x, "image", "image")
    bl.launch()
  • Creates the TabbedInterface abstraction to make the above easier. Just a rough draft, feedback on naming and API welcome here
io = gr.Interface(lambda x:x, "textbox", "textbox")
io2 = gr.Interface(lambda x:x, "image", "image")            

t = gr.TabbedInterface([io, io2])
t.launch()

@abidlabs abidlabs marked this pull request as ready for review April 5, 2022 00:48
@abidlabs abidlabs requested review from aliabid94 and omerXfaruq April 5, 2022 00:48
Comment thread gradio/interface.py Outdated
flagging_dir=interface.flagging_dir,
)


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems not the right way to do this. Why can't you just call interface.render() instead of creating a new Interface?
Also I'm a little skeptical we should even create TabbedInterface, instead of allowing users to naturally explore Blocks as they want more customizability.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why can't you just call interface.render()

There's a few Interface kwargs that don't make sense to have within this context. For example, theme. So I just decided to pick the kwargs that make sense to include here.

Also I'm a little skeptical we should even create TabbedInterface, instead of allowing users to naturally explore Blocks as they want more customizability

Open to discussion about this. We should keep in mind that we've heard directly from several users that they want something like this rather than having to delve into the lower-level Blocks API. E.g. #690 (comment)

Comment thread gradio/blocks.py

class Block:
def __init__(self):
def __init__(self, without_rendering=False):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is it possible that instead of using "without_rendering", it just checks to see if its within a BlockContext and only calls render if that's the case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That doesn't work. Consider the following example:

with gr.Blocks() as bl:
    with gr.Tabs():
        with gr.TabItem("Text"):
            gr.Interface(lambda x:x, "textbox", "textbox")
        with gr.TabItem("Image"):
            gr.Interface(lambda x:x, "image", "image")
    bl.launch()

Inside the Interface class, you create Component objects once, and then render them later.

Create components:

self.input_components = [get_component_instance(i) for i in inputs]

Render:

component.render()

In this case, both statements are inside a BlockContext (the outer level Blocks bl), so the component would end up getting rendered twice. That's why we need to explicitly escape the context.

@abidlabs
Copy link
Copy Markdown
Member Author

abidlabs commented Apr 5, 2022

I'm going to remove TabbedInterface and will instead create a separate PR for these types of Interfaces. That'll be cleaner.

@aliabid94
Copy link
Copy Markdown
Contributor

Lgtm

@abidlabs abidlabs merged commit 651205a into blocks-dev Apr 5, 2022
@abidlabs abidlabs deleted the blocks-as-block branch April 5, 2022 15:27
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.

2 participants