- Components
- Textbox
Building Demos
Interface ChatInterface TabbedInterface BlocksBlocks Layout
render Accordion Column Group Row TabComponents
Introduction AnnotatedImage Audio BarPlot Button Chatbot Checkbox CheckboxGroup ClearButton Code ColorPicker Dataframe Dataset DateTime DownloadButton Dropdown DuplicateButton File FileExplorer Gallery HighlightedText HTML Image ImageEditor JSON Label LinePlot LoginButton Markdown Model3D MultimodalTextbox Number ParamViewer Plot Radio ScatterPlot SimpleImage Slider State Textbox Timer UploadButton VideoHelpers
EventData DeletedFileData KeyUpData LikeData SelectData FileData Progress Examples Dependency load on set_static_pathsModals
Error Info WarningRoutes
mount_gradio_app RequestOther
Flagging Themes NO_RELOADNew to Gradio? Start here: Getting Started
See the Release History
Textbox
gradio.Textbox(···)
Description
Creates a textarea for user to enter string input or display string output.
Behavior
As input component
:
Passes text value as a
str
into the function.
Your function should accept one of these types:
def predict(
value: str | None
...
As output component
:
Expects a
str
returned from function and sets textarea value to it.
Your function should return one of these types:
def predict(···) -> str | None
...
return value
Initialization
value: str | Callable | None
value: str | Callable | None
=
None
default text to provide in textarea. If callable, the function will be called whenever the app loads to set the initial value of the component.
lines: int
lines: int
=
1
minimum number of line rows to provide in textarea.
max_lines: int
max_lines: int
=
20
maximum number of line rows to provide in textarea.
placeholder: str | None
placeholder: str | None
=
None
placeholder hint to provide behind textarea.
label: str | None
label: str | None
=
None
the label for this component, displayed above the component if `show_label` is `True` and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component corresponds to.
info: str | None
info: str | None
=
None
additional component description, appears below the label in smaller font. Supports markdown / HTML syntax.
every: Timer | float | None
every: Timer | float | None
=
None
Continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.
inputs: Component | list[Component] | set[Component] | None
inputs: Component | list[Component] | set[Component] | None
=
None
Components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.
show_label: bool | None
show_label: bool | None
=
None
if True, will display label. If False, the copy button is hidden as well as well as the label.
container: bool
container: bool
=
True
If True, will place the component in a container - providing some extra padding around the border.
scale: int | None
scale: int | None
=
None
relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.
min_width: int
min_width: int
=
160
minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
interactive: bool | None
interactive: bool | None
=
None
if True, will be rendered as an editable textbox; if False, editing will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
visible: bool
visible: bool
=
True
If False, component will be hidden.
elem_id: str | None
elem_id: str | None
=
None
An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
autofocus: bool
autofocus: bool
=
False
If True, will focus on the textbox when the page loads. Use this carefully, as it can cause usability issues for sighted and non-sighted users.
autoscroll: bool
autoscroll: bool
=
True
If True, will automatically scroll to the bottom of the textbox when the value changes, unless the user scrolls up. If False, will not scroll to the bottom of the textbox when the value changes.
elem_classes: list[str] | str | None
elem_classes: list[str] | str | None
=
None
An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
render: bool
render: bool
=
True
If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
key: int | str | None
key: int | str | None
=
None
if assigned, will be used to assume identity across a re-render. Components that have the same key across a re-render will have their value preserved.
type: Literal['text', 'password', 'email']
type: Literal['text', 'password', 'email']
=
"text"
The type of textbox. One of: 'text', 'password', 'email', Default is 'text'.
text_align: Literal['left', 'right'] | None
text_align: Literal['left', 'right'] | None
=
None
How to align the text in the textbox, can be: "left", "right", or None (default). If None, the alignment is left if `rtl` is False, or right if `rtl` is True. Can only be changed if `type` is "text".
rtl: bool
rtl: bool
=
False
If True and `type` is "text", sets the direction of the text to right-to-left (cursor appears on the left of the text). Default is False, which renders cursor on the right.
show_copy_button: bool
show_copy_button: bool
=
False
If True, includes a copy button to copy the text in the textbox. Only applies if show_label is True.
max_length: int | None
max_length: int | None
=
None
maximum number of characters (including newlines) allowed in the textbox. If None, there is no maximum length.
submit_btn: str | bool | None
submit_btn: str | bool | None
=
False
If False, will not show a submit button. If True, will show a submit button with an icon. If a string, will use that string as the submit button text. When the submit button is shown, the border of the textbox will be removed, which is useful for creating a chat interface.
stop_btn: str | bool | None
stop_btn: str | bool | None
=
False
Shortcuts
Class | Interface String Shortcut | Initialization |
---|---|---|
|
"textbox" |
Uses default values |
|
"textarea" |
Uses lines=7 |
Demos
import gradio as gr
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="textbox", outputs="textbox")
if __name__ == "__main__":
demo.launch()
from difflib import Differ
import gradio as gr
def diff_texts(text1, text2):
d = Differ()
return [
(token[2:], token[0] if token[0] != " " else None)
for token in d.compare(text1, text2)
demo = gr.Interface(
diff_texts,
gr.Textbox(
label="Text 1",
info="Initial text",
lines=3,
value="The quick brown fox jumped over the lazy dogs.",
gr.Textbox(
label="Text 2",
info="Text to compare",
lines=3,
value="The fast brown fox jumps over lazy dogs.",
gr.HighlightedText(
label="Diff",
combine_adjacent=True,
show_legend=True,
color_map={"+": "red", "-": "green"}),
theme=gr.themes.Base()
if __name__ == "__main__":
demo.launch()
from difflib import Differ
import gradio as gr
def diff_texts(text1, text2):
d = Differ()
return [
(token[2:], token[0] if token[0] != " " else None)
for token in d.compare(text1, text2)
demo = gr.Interface(
diff_texts,
gr.Textbox(
label="Text 1",
info="Initial text",
lines=3,
value="The quick brown fox jumped over the lazy dogs.",
gr.Textbox(
label="Text 2",
info="Text to compare",
lines=3,
value="The fast brown fox jumps over lazy dogs.",
gr.HighlightedText(
label="Diff",
combine_adjacent=True,
show_legend=True,
color_map={"+": "red", "-": "green"}),
theme=gr.themes.Base()
if __name__ == "__main__":
demo.launch()
import gradio as gr
def sentence_builder(quantity, animal, countries, place, activity_list, morning):
return f"""The {quantity} {animal}s from {" and ".join(countries)} went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}"""
demo = gr.Interface(
sentence_builder,
gr.Slider(2, 20, value=4, label="Count", info="Choose between 2 and 20"),
gr.Dropdown(
["cat", "dog", "bird"], label="Animal", info="Will add more animals later!"
gr.CheckboxGroup(["USA", "Japan", "Pakistan"], label="Countries", info="Where are they from?"),
gr.Radio(["park", "zoo", "road"], label="Location", info="Where did they go?"),
gr.Dropdown(
["ran", "swam", "ate", "slept"], value=["swam", "slept"], multiselect=True, label="Activity", info="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor, nisl eget ultricies aliquam, nunc nisl aliquet nunc, eget aliquam nisl nunc vel nisl."
gr.Checkbox(label="Morning", info="Did they do it in the morning?"),
"text",
examples=[
[2, "cat", ["Japan", "Pakistan"], "park", ["ate", "swam"], True],
[4, "dog", ["Japan"], "zoo", ["ate", "swam"], False],
[10, "bird", ["USA", "Pakistan"], "road", ["ran"], False],
[8, "cat", ["Pakistan"], "zoo", ["ate"], True],
if __name__ == "__main__":
demo.launch()
import gradio as gr
def sentence_builder(quantity, animal, countries, place, activity_list, morning):
return f"""The {quantity} {animal}s from {" and ".join(countries)} went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}"""
demo = gr.Interface(
sentence_builder,
gr.Slider(2, 20, value=4, label="Count", info="Choose between 2 and 20"),
gr.Dropdown(
["cat", "dog", "bird"], label="Animal", info="Will add more animals later!"
gr.CheckboxGroup(["USA", "Japan", "Pakistan"], label="Countries", info="Where are they from?"),
gr.Radio(["park", "zoo", "road"], label="Location", info="Where did they go?"),
gr.Dropdown(
["ran", "swam", "ate", "slept"], value=["swam", "slept"], multiselect=True, label="Activity", info="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor, nisl eget ultricies aliquam, nunc nisl aliquet nunc, eget aliquam nisl nunc vel nisl."
gr.Checkbox(label="Morning", info="Did they do it in the morning?"),
"text",
examples=[
[2, "cat", ["Japan", "Pakistan"], "park", ["ate", "swam"], True],
[4, "dog", ["Japan"], "zoo", ["ate", "swam"], False],
[10, "bird", ["USA", "Pakistan"], "road", ["ran"], False],
[8, "cat", ["Pakistan"], "zoo", ["ate"], True],
if __name__ == "__main__":
demo.launch()
Event Listeners
Description
Event listeners allow you to respond to user interactions with the UI
components you've defined in a Gradio Blocks app. When a user interacts with
an element, such as changing a slider value or uploading an image, a
function is called.
Supported Event Listeners
The
Textbox
component supports the following event listeners. Each event listener takes the
same parameters, which are listed in the