update-shortcut-syntax (#1234)

* update-shortcut-syntax

- fix&update gr.component
- create a demo introducing shortcuts within Blocks

* update-shortcut-syntax

- tweaks

* update-shortcut-syntax

- tweaks

* update-shortcut-syntax

- fix formatting

* update-shortcut-syntax

- tweaks
- fix tests

* update-shortcut-syntax

- tweaks
- fix tests

* update-shortcut-syntax

- tweaks
- fix tests
This commit is contained in:
Ömer Faruk Özdemir 2022-05-13 09:30:11 +03:00 committed by GitHub
parent 1c3dffd291
commit 2de9ee8bfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 49 additions and 27 deletions

View File

@ -55,7 +55,7 @@ jobs:
- run:
command: |
. venv/bin/activate
python -m flake8 --ignore=E731,E501,E722,W503,E126,F401,E203 gradio test
python -m flake8 --ignore=E731,E501,E722,W503,E126,F401,E203,F403 gradio test
- run:
command: |
. venv/bin/activate

View File

@ -0,0 +1,28 @@
import gradio as gr
def greet(str):
return str
with gr.Blocks() as demo:
"""
You can make use of str shortcuts you use in Interface within Blocks as well.
Interface shortcut example:
Interface(greet, "textarea", "textarea")
You can use
1. gr.component()
2. gr.templates.Template()
3. gr.Template()
All the templates are listed in gradio/templates.py
"""
with gr.Row():
text1 = gr.component("textarea")
text2 = gr.TextArea()
text3 = gr.templates.TextArea()
text1.change(greet, text1, text2)
text2.change(greet, text2, text3)
text3.change(greet, text3, text1)
demo.launch()

View File

@ -1,14 +0,0 @@
import gradio as gr
def greet(name):
return "Hello " + name + "!!"
demo = gr.Interface(
fn=greet, inputs=gr.component("textarea"), outputs=gr.component("textarea")
)
if __name__ == "__main__":
demo.launch()

View File

@ -4,6 +4,7 @@ import gradio.components as components
import gradio.inputs as inputs
import gradio.outputs as outputs
import gradio.processing_utils
import gradio.templates
from gradio.blocks import Blocks, Column, Row, TabItem, Tabs
from gradio.components import (
HTML,
@ -39,6 +40,7 @@ from gradio.components import (
component,
update,
)
from gradio.external import load_interface
from gradio.flagging import (
CSVLogger,
FlaggingCallback,
@ -47,6 +49,7 @@ from gradio.flagging import (
)
from gradio.interface import Interface, TabbedInterface, close_all
from gradio.mix import Parallel, Series
from gradio.templates import *
current_pkg_version = pkg_resources.require("gradio")[0].version
__version__ = current_pkg_version

View File

@ -12,7 +12,7 @@ import tempfile
import warnings
from copy import deepcopy
from types import ModuleType
from typing import Any, Callable, Dict, List, Optional, Tuple
from typing import Any, Callable, Dict, List, Optional, Tuple, Type
import matplotlib.figure
import numpy as np
@ -3212,7 +3212,7 @@ class Chatbot(Changeable, IOComponent):
def __init__(
self,
value="",
color_map: Tuple(str, str) = None,
color_map: Tuple[str, str] = None,
*,
label: Optional[str] = None,
show_label: bool = True,
@ -3249,7 +3249,7 @@ class Chatbot(Changeable, IOComponent):
@staticmethod
def update(
value: Optional[Any] = None,
color_map: Optional[Tuple(str, str)] = None,
color_map: Optional[Tuple[str, str]] = None,
label: Optional[str] = None,
show_label: Optional[bool] = None,
visible: Optional[bool] = None,
@ -3720,9 +3720,9 @@ class StatusTracker(Component):
}
def component(cls_name: str):
def component_class(cls_name: str) -> Type[Component]:
"""
Returns a component or template with the given class name, or raises a ValueError if not found.
Returns the component class with the given class name, or raises a ValueError if not found.
@param cls_name: lower-case string class name of a component
@return cls: the component class
"""
@ -3744,14 +3744,19 @@ def component(cls_name: str):
raise ValueError(f"No such Component: {cls_name}")
def component(cls_name: str) -> Component:
obj = component_class(cls_name)()
return obj
def get_component_instance(comp: str | dict | Component):
if isinstance(comp, str):
component_cls = component(comp)
return component_cls()
return component(comp)
elif isinstance(comp, dict):
name = comp.pop("name")
component_cls = component(name)
return component_cls(**comp)
component_cls = component_class(name)
component_obj = component_cls(**comp)
return component_obj
elif isinstance(comp, Component):
return comp
else:

View File

@ -6,5 +6,5 @@ else
echo "Formatting backend and tests with black and isort, also checking for standards with flake8"
python -m black gradio test
python -m isort --profile=black gradio test
python -m flake8 --ignore=E731,E501,E722,W503,E126,F401,E203 gradio test
python -m flake8 --ignore=E731,E501,E722,W503,E126,F401,E203,F403 gradio test
fi

View File

@ -27,7 +27,7 @@ class TestComponent(unittest.TestCase):
"""
component
"""
assert isinstance(gr.components.component("text")(), gr.templates.Text)
assert isinstance(gr.components.component("text"), gr.templates.Text)
class TestTextbox(unittest.TestCase):

View File

@ -206,7 +206,7 @@ class TestLoadInterface(unittest.TestCase):
self.assertEqual(output, "Mein Name ist Sarah und ich lebe in London")
def test_numerical_to_label_space(self):
interface_info = gr.external.load_interface("spaces/abidlabs/titanic-survival")
interface_info = gr.load_interface("spaces/abidlabs/titanic-survival")
io = gr.Interface(**interface_info)
io.api_mode = True
output = io("male", 77, 10)