- Components
- Code
New to Gradio? Start here: Getting Started
See the Release History
Code
gradio.Code(···)Description
Creates a code editor for viewing code (as an output component), or for entering and editing code (as an input component).
Behavior
As input component: Passes the code entered as a str.
Your function should accept one of these types:
def predict(
value: str | None
)
...As output component: Expects a str of code.
Your function should return one of these types:
def predict(···) -> tuple[str] | str | None
...
return valueInitialization
value: str | Callable | None
value: str | Callable | None= NoneDefault value to show in the code editor. If a function is provided, the function will be called each time the app loads to set the initial value of this component.
language: Literal['python', 'c', 'cpp', 'markdown', 'latex', 'json', 'html', 'css', 'javascript', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql', 'sql-msSQL', 'sql-mySQL', 'sql-mariaDB', 'sql-sqlite', 'sql-cassandra', 'sql-plSQL', 'sql-hive', 'sql-pgSQL', 'sql-gql', 'sql-gpSQL', 'sql-sparkSQL', 'sql-esper'] | None
language: Literal['python', 'c', 'cpp', 'markdown', 'latex', 'json', 'html', 'css', 'javascript', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql', 'sql-msSQL', 'sql-mySQL', 'sql-mariaDB', 'sql-sqlite', 'sql-cassandra', 'sql-plSQL', 'sql-hive', 'sql-pgSQL', 'sql-gql', 'sql-gpSQL', 'sql-sparkSQL', 'sql-esper'] | None= NoneThe language to display the code as. Supported languages listed in `gr.Code.languages`.
every: Timer | float | None
every: Timer | float | None= NoneContinously 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= NoneComponents 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.
max_lines: int | None
max_lines: int | None= NoneMaximum number of visible lines to show in the code editor. Defaults to None and will fill the height of the container.
label: str | I18nData | None
label: str | I18nData | None= Nonethe label for this component. Appears above the component 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 is assigned to.
container: bool
container: bool= TrueIf True, will place the component in a container - providing some extra padding around the border.
scale: int | None
scale: int | None= Nonerelative 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= 160minimum 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.
visible: bool | Literal['hidden']
visible: bool | Literal['hidden']= TrueIf False, component will be hidden. If "hidden", component will be visually hidden and not take up space in the layout but still exist in the DOM
elem_id: str | None
elem_id: str | None= NoneAn optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes: list[str] | str | None
elem_classes: list[str] | str | None= NoneAn 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= TrueIf 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 | tuple[int | str, ...] | None
key: int | str | tuple[int | str, ...] | None= Nonein a gr.render, Components with the same key across re-renders are treated as the same component, not a new component. Properties set in 'preserved_by_key' are not reset across a re-render.
preserved_by_key: list[str] | str | None
preserved_by_key: list[str] | str | None= "value"A list of parameters from this component's constructor. Inside a gr.render() function, if a component is re-rendered with the same key, these (and only these) parameters will be preserved in the UI (if they have been changed by the user or an event listener) instead of re-rendered based on the values provided during constructor.
wrap_lines: bool
wrap_lines: bool= FalseIf True, will wrap lines to the width of the container when overflow occurs. Defaults to False.
Shortcuts
| Class | Interface String Shortcut | Initialization |
|---|---|---|
| "code" | Uses default values |
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 Code component supports the following event listeners. Each event listener takes the same parameters, which are listed in the Event Parameters table below.
| Listener | Description |
|---|---|
| ['python', 'c', 'cpp', 'markdown', 'latex', 'json', 'html', 'css', 'javascript', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql', 'sql-msSQL', 'sql-mySQL', 'sql-mariaDB', 'sql-sqlite', 'sql-cassandra', 'sql-plSQL', 'sql-hive', 'sql-pgSQL', 'sql-gql', 'sql-gpSQL', 'sql-sparkSQL', 'sql-esper', None] |
| Triggered when the value of the Code changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See |
| This listener is triggered when the user changes the value of the Code. |
| This listener is triggered when the Code is focused. |
| This listener is triggered when the Code is unfocused/blurred. |