Skip to content

Commit

Permalink
update-shortcut-syntax (#1234)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
omerXfaruq authored May 13, 2022
1 parent 1c3dffd commit 2de9ee8
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions demo/blocks_component_shortcut/run.py
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 0 additions & 14 deletions demo/blocks_textarea/run.py

This file was deleted.

3 changes: 3 additions & 0 deletions gradio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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,
Expand Down Expand Up @@ -39,6 +40,7 @@
component,
update,
)
from gradio.external import load_interface
from gradio.flagging import (
CSVLogger,
FlaggingCallback,
Expand All @@ -47,6 +49,7 @@
)
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
23 changes: 14 additions & 9 deletions gradio/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -3249,7 +3249,7 @@ def get_config(self):
@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,
Expand Down Expand Up @@ -3720,9 +3720,9 @@ def update(
}


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
"""
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion scripts/format_backend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_component_functions(self):
"""
component
"""
assert isinstance(gr.components.component("text")(), gr.templates.Text)
assert isinstance(gr.components.component("text"), gr.templates.Text)


class TestTextbox(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion test/test_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_translation_model(self):
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)
Expand Down

0 comments on commit 2de9ee8

Please sign in to comment.