Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to remove line number using show_line_numbers in gr.Code() #10627

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/giant-dryers-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/code": minor
"gradio": minor
---

feat:Option to remove line number using show_line_numbers in gr.Code()
1 change: 1 addition & 0 deletions demo/code/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def code(language, code):
language="python",
label="Input",
value='def all_odd_elements(sequence):\n """Returns every odd element of the sequence."""',
show_line_numbers=False,
)
code_out = gr.Code(label="Output")
btn = gr.Button("Run")
Expand Down
7 changes: 6 additions & 1 deletion gradio/components/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def __init__(
render: bool = True,
key: int | str | None = None,
wrap_lines: bool = False,
show_line_numbers: bool = True,
):
"""
Parameters:
Expand Down Expand Up @@ -137,6 +138,7 @@ def __init__(
self.lines = lines
self.max_lines = max(lines, max_lines) if max_lines is not None else None
self.wrap_lines = wrap_lines
self.show_line_numbers = show_line_numbers
super().__init__(
label=label,
every=every,
Expand Down Expand Up @@ -180,7 +182,10 @@ def postprocess(self, value: str | None) -> None | str:
return value.strip()

def api_info(self) -> dict[str, Any]:
return {"type": "string"}
return {
"type": "string",
"show_line_numbers" : self.show_line_numbers,
}

def example_payload(self) -> Any:
return "print('Hello World')"
Expand Down
2 changes: 2 additions & 0 deletions js/code/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
export let scale: number | null = null;
export let min_width: number | undefined = undefined;
export let wrap_lines = false;
export let show_line_numbers : boolean;

export let interactive: boolean;

Expand Down Expand Up @@ -91,6 +92,7 @@
{max_lines}
{dark_mode}
{wrap_lines}
{show_line_numbers}
readonly={!interactive}
on:blur={() => gradio.dispatch("blur")}
on:focus={() => gradio.dispatch("focus")}
Expand Down
14 changes: 12 additions & 2 deletions js/code/shared/Code.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
EditorView,
ViewUpdate,
keymap,
lineNumbers,
placeholder as placeholderExt
} from "@codemirror/view";
import { StateEffect, EditorState, type Extension } from "@codemirror/state";
Expand All @@ -26,6 +27,7 @@
export let readonly = false;
export let placeholder: string | HTMLElement | null | undefined = undefined;
export let wrap_lines = false;
export let show_line_numbers : boolean = true;

const dispatch = createEventDispatcher<{
change: string;
Expand Down Expand Up @@ -131,7 +133,8 @@
use_tab,
placeholder,
readonly,
lang_extension
lang_extension,
show_line_numbers
),
FontTheme,
...get_theme(),
Expand Down Expand Up @@ -184,7 +187,8 @@
use_tab: boolean,
placeholder: string | HTMLElement | null | undefined,
readonly: boolean,
lang: Extension | null | undefined
lang: Extension | null | undefined,
show_line_number:boolean
): Extension[] {
const extensions: Extension[] = [
EditorView.editable.of(!readonly),
Expand All @@ -206,6 +210,12 @@
}

extensions.push(EditorView.updateListener.of(handle_change));

// Conditionally add line number
if(show_line_numbers){
extensions.push(lineNumbers())
}

if (wrap_lines) {
extensions.push(EditorView.lineWrapping);
}
Expand Down
Loading