Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท Interfaces and Components

Text and Number Inputs

Learn Gradio's text components: Textbox options, Number, Markdown, HTML, Code, JSON and Label. See which Python types they give your function and how to validate input.

Text and numbers are the simplest data a Gradio app handles, and the components for them hold many options that beginners miss: line counts, placeholders, copy buttons, input limits, number precision, formatted output. This lesson walks through each of them and shows what Python value your function receives. Real Gradio code is read-only; the stand-ins reproduce the conversion and validation logic.

Textbox

Python
import gradio as gr

box = gr.Textbox(
    label="Review",
    placeholder="Write a sentence...",
    lines=3,            # starting height
    max_lines=8,        # grows until this, then scrolls
    max_length=500,     # characters the user may type
    show_copy_button=True,
    info="Plain text only.",
    type="text",        # "text", "password" or "email"
)

The page shows a labelled three-line text area with a placeholder, a small helper line (info) under the label and a copy icon. Your function receives a str. If the user types nothing, it receives an empty string ("").

  • lines=1 renders a single-line input where Enter submits; lines>1 renders a text area.
  • type="password" hides the characters. It is only cosmetic; it does not encrypt anything.
  • As an output, set interactive=False (the default for outputs) so users cannot edit the result. Use autoscroll for streaming logs.

Number

Python
age = gr.Number(label="Age", value=30, minimum=0, maximum=120, precision=0)

precision=0 rounds to an integer and makes your function receive an int; precision=None (the default) gives a float. An empty field gives None, so test for it. minimum and maximum are enforced by Gradio, which raises an error before your function runs if the value is out of range.

For a number chosen with a handle instead of typing, use Slider.

Markdown, HTML and Label as outputs

Output components can format results:

Python
gr.Markdown("## Result\nYour **score** is 87.")
gr.HTML("<p style='color: green'>Passed</p>")
gr.Label(num_top_classes=3)
  • Markdown renders headings, lists, links, tables and LaTeX if enabled. It is also the usual way to put static text on a page.
  • HTML renders raw HTML. Do not put untrusted user text in it, because it can inject markup; escape with html.escape first.
  • Label shows a classification. Return a dictionary {"cat": 0.9, "dog": 0.1} and Gradio draws the top class as a headline with confidence bars. This is the standard output for classifiers.

Code and JSON

Python
gr.Code(language="python", label="Generated code")
gr.JSON(label="Raw response")

Code shows highlighted code with line numbers and a copy button; it can be an input as well. JSON shows an expandable tree and is perfect for debugging model output. Return a normal Python dict or list, or a JSON string.

Choosing the right text component

Need Component
Short or long plain text in or out Textbox
Numeric entry Number
Formatted explanation Markdown
Own styling HTML
Class probabilities Label
Program source Code
Structured data JSON

Preprocessing details that cause bugs

  • Numbers arrive as float unless precision=0; 3 and 3.0 are not the same type for range().
  • Text is not stripped. " yes " is not "yes"; normalize with .strip().lower() before comparing.
  • Components that are empty give None (numbers) or "" (text). Decide explicitly what empty means.
  • Output components convert for you. Returning a number to a Textbox shows its string form, but returning a NumPy float32 to a Number can fail in older versions; cast with float().

Stand-in 1: cleaning and validating text

Python Stand-in: normalize and validate a text input

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real Gradio enforces max_length in the browser for typing, but a client calling the API can send more, so the same check in the function is not wasted work.

Stand-in 2: what a Number component gives you

Python Stand-in: Number precision, range and empty values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Formatting a classifier output

A Label expects {class: confidence}. This stand-in builds one from raw scores with a softmax, which is the usual shape of a model's output:

Python Stand-in: scores to a Label dictionary

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The dictionary has the same shape a gr.Label shows: the largest entry becomes the headline and every entry gets a bar.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Number component give your function by default?

    Use precision=0 to receive an int. An empty field gives None.
  2. 2. Which output dictionary does gr.Label expect?

    Class names mapped to confidences. The top entry becomes the headline.
  3. 3. Is type="password" on a Textbox a security feature?

    It is cosmetic. The value is still sent as plain text to your function.
  4. 4. Why validate length inside the function even with max_length set?

    Browser limits help users, but the function must defend itself against any input.

Summary

  • Textbox gives str; Number gives float or int (with precision=0) or None.
  • Markdown, HTML, Label, Code and JSON format outputs.
  • Normalize and validate in the function; never trust browser-side limits.
  • A classifier returns {class: confidence} to a Label.

Next: Images, Audio, Video and Files