Defining Programming Language for Code Output in Gradio
To define the programming language for code output in Gradio using the gr.Code component, you need to use the language parameter when creating the component. This parameter allows you to specify which syntax highlighting rules should be applied to the code displayed or entered in the component.
Basic Usage
Here's the basic syntax for creating a Code component with a specified language:
python
import gradio as gr
gr.Code(value="def hello_world():\n print('Hello, world!')", language="python")
Supported Languages
Gradio's Code component supports a wide variety of programming languages for syntax highlighting:
python
c
cpp (C++)
markdown
json
html
css
javascript
jinja2
typescript
yaml
dockerfile
shell
r
sql (and various SQL dialects)[1][15]
SQL dialects include:
sql-msSQL
sql-mySQL
sql-mariaDB
sql-sqlite
sql-cassandra
sql-plSQL
sql-hive
sql-pgSQL
sql-gql
sql-gpSQL
sql-sparkSQL
sql-esper[1]
Complete Examples
Example 1: Simple Code Display
python
import gradio as gr
with gr.Blocks() as demo:
gr.Code(
value="# Python example\ndef greet(name):\n return f'Hello, {name}!'",
language="python"
)
demo.launch()
Example 2: Code Input and Output
python
import gradio as gr
def process_code(code):
# Example function that processes code (here just adding a comment)
return code + "\n# This code was processed"
with gr.Blocks() as demo:
code_input = gr.Code(
value="function hello() {\n console.log('Hello world!');\n}",
language="javascript",
label="Input Code"
)
process_btn = gr.Button("Process Code")
code_output = gr.Code(
language="javascript",
label="Processed Code"
)
process_btn.click(fn=process_code, inputs=code_input, outputs=code_output)
demo.launch()
Example 3: Using Code in Interface
python
import gradio as gr
def highlight_syntax(code, language):
# This function just returns the code, but now it will be
# displayed with the correct syntax highlighting
return code
demo = gr.Interface(
fn=highlight_syntax,
inputs=[
gr.Code(label="Enter your code"),
gr.Dropdown(choices=["python", "javascript", "html", "css"], label="Select language")
],
outputs=gr.Code(label="Highlighted code"),
title="Code Syntax Highlighter"
)
demo.launch()
Additional Configuration Options
The gr.Code component also accepts other parameters to customize its behavior:
value: Default value to show in the code editor
max_lines: Maximum number of visible lines to show in the code editor
label: The label for the component
container: Whether to place the component in a container
scale: Scale factor for the component size[1]
Using with Other Languages
If you need to display code from languages not natively supported by Gradio, you can use the closest matching language for syntax highlighting or consider using "markdown" as a fallback for generic text with minimal highlighting[3][18].
Remember that the language parameter only affects how the code is displayed (syntax highlighting) but doesn't change how the underlying code works or runs when using Gradio with other programming languages through techniques like subprocess module integration.